Skip to content

Fix offline --sql mode incorrectly emitting DROP TABLE for alembic_version#1827

Open
Suchethan021 wants to merge 1 commit into
sqlalchemy:mainfrom
Suchethan021:fix/1822-sql-mode-drop-version-table
Open

Fix offline --sql mode incorrectly emitting DROP TABLE for alembic_version#1827
Suchethan021 wants to merge 1 commit into
sqlalchemy:mainfrom
Suchethan021:fix/1822-sql-mode-drop-version-table

Conversation

@Suchethan021

Copy link
Copy Markdown

Summary

Running alembic stamp base --sql or alembic downgrade <rev>:base --sql incorrectly emits a DROP TABLE alembic_version statement. Online mode never drops the version table — it only deletes rows from it — so this was an inconsistency introduced in a very early commit (2da7f004) before the 0.1 release.

The maintainer confirmed this is a bug:

"Alembic creates the version table if it does not exist but never drops it. In this case the DROP here is a specific rule added just for --sql mode and appears to be a coding mistake."

Changes

alembic/runtime/migration.py
Removed the block in MigrationContext.run_migrations() that conditionally dropped the version table when as_sql=True and no heads remained. Added an explanatory comment for future readers.

tests/test_command.py

  • Updated test_version_to_none assertion to expect DROP TABLE alembic_version is not present in --sql output.
  • Added test_sql_stamp_to_base_no_drop to explicitly cover the stamp base --sql path.

docs/build/unreleased/1822.rst
Added changelog entry following Alembic'''s per-file changelog convention.

Test results

1515 passed, 132 skipped in 4.27s

(Python 3.13.3 / SQLite; DB-specific and test_post_write skipped as expected)

Fixes #1822

…rsion

Running 'stamp base --sql' or 'downgrade <rev>:base --sql' would emit a
DROP TABLE alembic_version statement. Online mode never drops the version
table (it only deletes rows), so this was an inconsistency introduced in
the very early commits of the project.

Remove the block from MigrationContext.run_migrations() that emitted the
table drop exclusively in --sql mode when all heads were removed. Add an
explanatory comment and an additional test covering 'stamp base --sql'.

Fixes: sqlalchemy#1822
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.

Stamp command with "base" emits a DROP if --sql mode is present and no head revisions are present

1 participant