Skip to content

Commit

Permalink
Respects ResultProxy.supports_sane_rowcount()
Browse files Browse the repository at this point in the history
The check for matched rowcount when the alembic_version table is updated or
deleted from is now conditional based on whether or not the dialect
supports the concept of "rowcount" for UPDATE or DELETE rows matched.  Some
third party dialects do not support this concept.  Pull request courtesy Ke
Zhu.

Closes: #667
Pull-request: #667
Pull-request-sha: a0d45ed

Change-Id: I09c9b540d8e21a94728085270eb20ba2273cbdb1
  • Loading branch information
Ke Zhu authored and zzzeek committed Mar 1, 2020
1 parent dfc4c55 commit 5287da2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions alembic/runtime/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,11 @@ def _delete_version(self, version):
== literal_column("'%s'" % version)
)
)
if not self.context.as_sql and ret.rowcount != 1:
if (
not self.context.as_sql
and self.context.dialect.supports_sane_rowcount
and ret.rowcount != 1
):
raise util.CommandError(
"Online migration expected to match one "
"row when deleting '%s' in '%s'; "
Expand All @@ -697,7 +701,11 @@ def _update_version(self, from_, to_):
== literal_column("'%s'" % from_)
)
)
if not self.context.as_sql and ret.rowcount != 1:
if (
not self.context.as_sql
and self.context.dialect.supports_sane_rowcount
and ret.rowcount != 1
):
raise util.CommandError(
"Online migration expected to match one "
"row when updating '%s' to '%s' in '%s'; "
Expand Down
8 changes: 8 additions & 0 deletions docs/build/unreleased/rowcount.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, environment

The check for matched rowcount when the alembic_version table is updated or
deleted from is now conditional based on whether or not the dialect
supports the concept of "rowcount" for UPDATE or DELETE rows matched. Some
third party dialects do not support this concept. Pull request courtesy Ke
Zhu.
37 changes: 37 additions & 0 deletions tests/test_version_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ def test_update_no_match(self):
_up("x", "b"),
)

def test_update_no_match_no_sane_rowcount(self):
self.updater.update_to_step(_up(None, "a", True))
self.updater.heads.add("x")
with mock.patch.object(
self.connection.dialect, "supports_sane_rowcount", False
):
self.updater.update_to_step(_up("x", "b"))

def test_update_multi_match(self):
self.connection.execute(version_table.insert(), version_num="a")
self.connection.execute(version_table.insert(), version_num="a")
Expand All @@ -275,6 +283,16 @@ def test_update_multi_match(self):
_up("a", "b"),
)

def test_update_multi_match_no_sane_rowcount(self):
self.connection.execute(version_table.insert(), version_num="a")
self.connection.execute(version_table.insert(), version_num="a")

self.updater.heads.add("a")
with mock.patch.object(
self.connection.dialect, "supports_sane_rowcount", False
):
self.updater.update_to_step(_up("a", "b"))

def test_delete_no_match(self):
self.updater.update_to_step(_up(None, "a", True))

Expand All @@ -287,6 +305,15 @@ def test_delete_no_match(self):
_down("x", None, True),
)

def test_delete_no_matchno_sane_rowcount(self):
self.updater.update_to_step(_up(None, "a", True))

self.updater.heads.add("x")
with mock.patch.object(
self.connection.dialect, "supports_sane_rowcount", False
):
self.updater.update_to_step(_down("x", None, True))

def test_delete_multi_match(self):
self.connection.execute(version_table.insert(), version_num="a")
self.connection.execute(version_table.insert(), version_num="a")
Expand All @@ -299,3 +326,13 @@ def test_delete_multi_match(self):
self.updater.update_to_step,
_down("a", None, True),
)

def test_delete_multi_match_no_sane_rowcount(self):
self.connection.execute(version_table.insert(), version_num="a")
self.connection.execute(version_table.insert(), version_num="a")

self.updater.heads.add("a")
with mock.patch.object(
self.connection.dialect, "supports_sane_rowcount", False
):
self.updater.update_to_step(_down("a", None, True))

0 comments on commit 5287da2

Please sign in to comment.