Skip to content

fix(core): skip removing foreign keys in dropAllTables when dropping with cascade - #18497

Merged
WikiRik merged 5 commits into
mainfrom
wikirik-agent/drop-all-tables-cascade
Oct 21, 2026
Merged

WikiRik merged 5 commits into
mainfrom
wikirik-agent/drop-all-tables-cascade

Conversation

@wikirik-agent

@wikirik-agent wikirik-agent commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Description of Changes

queryInterface.dropAllTables() looks up and removes every table's foreign keys before dropping the tables. For dialects that drop with cascade (Postgres, Oracle and Snowflake, where cascade is enabled by default), this is redundant: DROP TABLE ... CASCADE (CASCADE CONSTRAINTS on Oracle) already drops the foreign keys that reference the dropped table.

This PR skips the foreign-key pass when the tables are dropped with cascade. Dialects without cascade, and calls with an explicit cascade: false, keep the current behaviour.

The integration test suite calls dropAllTables after almost every test, and on Oracle the foreign-key lookup is a slow dictionary query (see #18498). Locally, running test/integration/associations/** on main spends 6.5 s of its 26 s on Postgres in the foreign-key lookups of the database reset hook, all of which this PR removes. On Oracle 23 the same run issued 610 fewer foreign-key lookups, saving ~15 s of SQL time. The existing dropAllTables integration tests (foreign keys, schemas, skip) pass on Postgres and Oracle.

The foreign-key pass no longer runs by default on these dialects, so this PR adds a test that calls dropAllTables({ cascade: false }) on tables with cyclic foreign keys, which only succeeds if the foreign keys are removed first. It fails on Postgres when the pass is disabled. On Oracle that test can't fail, because Oracle's dropTableQuery always uses CASCADE CONSTRAINTS. The cyclic test also runs with the default options on every dialect. That covers the new path on Postgres and Oracle, and gives the other dialects their first cyclic-FK coverage of the foreign-key pass.

CI, after rebasing onto main (6995f15). Median duration of the "Integration Tests" step, compared with 30 recent runs on the same main:

main this PR
Postgres (all 8 variants) 104–116 s 83–93 s (−15 to −26%)
Oracle oldest 326 s 287 s (−12%)
MySQL, MariaDB, MSSQL, Db2, SQLite unchanged within noise. They don't support dropTable.cascade, so they still run the pass.

Related: #18572 fixes Sequelize#drop dropping every model two or three times, which also affects sync({ force: true }).

List of Breaking Changes

None.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when dropping multiple tables without cascade. Foreign-key constraints are now removed before the tables, helping prevent failures when tables depend on one another, including when dependencies form cycles.
    • When cascade is enabled, table removal continues without separately removing those constraints. This preserves the cascade behavior while avoiding an unnecessary constraint-removal step.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 311df20a-6bd7-4a1a-9245-5ccba12b9cb2
📥 Commits

Reviewing files that changed from the base of the PR and between 52d04b2 and d8300f1.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7239563a-f0e2-438e-9e3b-48b56e33f329
📥 Commits

Reviewing files that changed from the base of the PR and between 183f1fc and 8f3f45c.

📒 Files selected for processing: 1
  • packages/core/test/integration/query-interface/drop-all-tables.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

dropAllTables now removes foreign keys only when cascade is disabled. Integration coverage verifies table removal for cyclic foreign keys with default and supported non-cascade options.

Changes: Table drop cleanup

Layer / File(s) Summary
Conditional foreign-key cleanup
packages/core/src/abstract-dialect/query-interface-typescript.ts, packages/core/test/integration/query-interface/drop-all-tables.test.ts
When cascade is disabled, dropAllTables removes foreign keys before dropping tables. Removal is concurrent when supported and sequential otherwise. When cascade is enabled, it skips foreign-key discovery and removal. Integration tests cover cyclic foreign keys and verify that no tables remain.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 8f3f4

The change is mergeable based on the available source evidence.

Architecture risk: 🔵 Low · up to 85015

The change affects 1 system.

Changed systems: packages/core

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/core (library) was modified; 2 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/core/src/abstract-dialect/query-interface-typescript.ts: dropAllTables now skips foreign-key discovery and removal when cascade is enabled. When cascade is disabled, it removes each table’s foreign keys, concurrently if the dialect supports concurrent constraint drops and sequentially otherwise; previously it always attempted this cleanup.
  • observed — Modified behavior in packages/core/test/integration/query-interface/drop-all-tables.test.ts: When the dialect supports cascading table drops, adds a test that creates a cyclic foreign-key relationship between levels and actors, calls dropAllTables with cascade: false, and expects no tables to remain.
Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: skipping foreign-key removal when dropAllTables uses cascade.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@wikirik-agent wikirik-agent changed the title perf(core): skip removing foreign keys in dropAllTables when dropping with cascade fix(core): skip removing foreign keys in dropAllTables when dropping with cascade Sep 26, 2026
@WikiRik
WikiRik marked this pull request as ready for review October 5, 2026 14:06
@WikiRik
WikiRik requested a review from a team as a code owner October 5, 2026 14:06
@WikiRik
WikiRik requested review from SippieCup and sdepold October 5, 2026 14:06
wikirik-agent and others added 2 commits October 21, 2026 03:31
… with cascade

DROP TABLE ... CASCADE already drops the foreign keys that reference the
table, so looking up and removing each table's foreign keys first is
redundant for dialects that drop with cascade (postgres, oracle,
snowflake). On Oracle, the foreign key lookup is a slow dictionary query.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Uses tables with cyclic foreign keys, so the test fails if the foreign
keys are not removed before the tables are dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wikirik-agent
wikirik-agent force-pushed the wikirik-agent/drop-all-tables-cascade branch from 85015e0 to 183f1fc Compare October 21, 2026 03:39
@WikiRik
WikiRik enabled auto-merge (squash) October 21, 2026 06:03
@WikiRik
WikiRik merged commit 1fbfad1 into main Oct 21, 2026
@WikiRik
WikiRik deleted the wikirik-agent/drop-all-tables-cascade branch October 21, 2026 07:34
papandreou added a commit to papandreou/sequelize that referenced this pull request Oct 23, 2026
* origin/main: (54 commits)
  fix: use bind parameters in the Oracle catalog queries (sequelize#18498)
  meta: update mysql docker tag to v8.4.13 (sequelize#18577)
  meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575)
  meta: update dependency node to v24.22.1 (sequelize#18574)
  meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573)
  fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497)
  fix(core): drop each model only once in Sequelize#drop (sequelize#18572)
  fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554)
  fix(core): avoid lodash omit in the Model constructor (sequelize#18556)
  meta(core): support async functions in expectPerDialect (sequelize#18569)
  meta: update dependency ibm_db to ^4.1.0 (sequelize#18571)
  fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557)
  fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565)
  meta(core): convert unit/model tests to TypeScript (sequelize#18564)
  meta(core): convert unit/instance tests to TypeScript (sequelize#18563)
  meta: update sequelize AUTHORS (sequelize#18549)
  feat(mssql): add useUnicodeStrings dialect option (sequelize#18548)
  meta: retry image pulls in the database start scripts (sequelize#18340)
  meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559)
  meta: update dependency uuid to v14 (sequelize#18544)
  ...

# Conflicts:
#	packages/postgres/src/query-interface.js
papandreou added a commit to papandreou/sequelize that referenced this pull request Oct 23, 2026
* origin/main: (26 commits)
  fix: use bind parameters in the Oracle catalog queries (sequelize#18498)
  meta: update mysql docker tag to v8.4.13 (sequelize#18577)
  meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575)
  meta: update dependency node to v24.22.1 (sequelize#18574)
  meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573)
  fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497)
  fix(core): drop each model only once in Sequelize#drop (sequelize#18572)
  fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554)
  fix(core): avoid lodash omit in the Model constructor (sequelize#18556)
  meta(core): support async functions in expectPerDialect (sequelize#18569)
  meta: update dependency ibm_db to ^4.1.0 (sequelize#18571)
  fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557)
  fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565)
  meta(core): convert unit/model tests to TypeScript (sequelize#18564)
  meta(core): convert unit/instance tests to TypeScript (sequelize#18563)
  meta: update sequelize AUTHORS (sequelize#18549)
  feat(mssql): add useUnicodeStrings dialect option (sequelize#18548)
  meta: retry image pulls in the database start scripts (sequelize#18340)
  meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559)
  meta: update dependency uuid to v14 (sequelize#18544)
  ...

# Conflicts:
#	packages/core/src/abstract-dialect/query-generator.js
#	packages/core/src/abstract-dialect/query-interface-typescript.ts
#	packages/core/test/unit/model/bulk-create.test.js
#	packages/core/test/unit/sql/insert.test.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants