From 10867ecbc63e2b09a54cd5dd027ea6acd7cb948a Mon Sep 17 00:00:00 2001 From: w-Jessamine <148705640+w-Jessamine@users.noreply.github.com> Date: Mon, 29 Jun 2026 05:56:57 +0800 Subject: [PATCH] Avoid dropping version table in offline SQL Fixes: #1822 --- alembic/runtime/migration.py | 4 ---- docs/build/unreleased/1822.rst | 8 ++++++++ tests/test_command.py | 7 ++++++- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 docs/build/unreleased/1822.rst diff --git a/alembic/runtime/migration.py b/alembic/runtime/migration.py index 3fccf22a6..0bc781b1c 100644 --- a/alembic/runtime/migration.py +++ b/alembic/runtime/migration.py @@ -639,10 +639,6 @@ def run_migrations(self, **kw: Any) -> None: run_args=kw, ) - if self.as_sql and not head_maintainer.heads: - assert self.connection is not None - self._version.drop(self.connection) - def _in_connection_transaction(self) -> bool: try: meth = self.connection.in_transaction # type:ignore[union-attr] diff --git a/docs/build/unreleased/1822.rst b/docs/build/unreleased/1822.rst new file mode 100644 index 000000000..3bf3963c9 --- /dev/null +++ b/docs/build/unreleased/1822.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, commands + :tickets: 1822 + + Fixed SQL output for commands such as ``stamp base --sql`` and + ``downgrade ...:base --sql`` to no longer emit ``DROP TABLE`` for the + Alembic version table. This now matches online mode, where Alembic + updates the version rows but does not drop the version table. diff --git a/tests/test_command.py b/tests/test_command.py index 391a19d00..4fe354195 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -988,7 +988,7 @@ def test_version_to_none(self): command.downgrade(self.cfg, "%s:base" % self.c, sql=True) assert "CREATE TABLE alembic_version" not in buf.getvalue() assert "INSERT INTO alembic_version" not in buf.getvalue() - assert "DROP TABLE alembic_version" in buf.getvalue() + assert "DROP TABLE alembic_version" not in buf.getvalue() assert "DROP STEP 3" in buf.getvalue() assert "DROP STEP 2" in buf.getvalue() assert "DROP STEP 1" in buf.getvalue() @@ -1047,6 +1047,11 @@ def test_sql_stamp_revision_as_kw(self): in buf.getvalue() ) + def test_sql_stamp_base_does_not_drop_version_table(self): + with capture_context_buffer() as buf: + command.stamp(self.cfg, "base", sql=True) + assert "DROP TABLE alembic_version" not in buf.getvalue() + def test_stamp_argparser_single_rev(self): cmd = config.CommandLine() options = cmd.parser.parse_args(["stamp", self.c, "--sql"])