Skip to content

Commit

Permalink
Adjust for immediate dependencies that are still ancestors
Browse files Browse the repository at this point in the history
Fixed bug in versioning model where a downgrade across a revision with a
dependency on another branch, yet an ancestor is also dependent on that
branch, would produce an erroneous state in the alembic_version table,
making upgrades impossible without manually repairing the table.

Change-Id: Ic755f8c3f78845bb80f4f5b3d172caf77cb51132
Fixes: sqlalchemy#789
  • Loading branch information
zzzeek committed Feb 2, 2021
1 parent 6f9d0b3 commit 63da357
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 97 deletions.
22 changes: 12 additions & 10 deletions alembic/runtime/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def doc(self):
@property
def from_revisions(self):
if self.is_upgrade:
return self.revision._all_down_revisions
return self.revision._normalized_down_revisions
else:
return (self.revision.revision,)

Expand All @@ -996,7 +996,7 @@ def to_revisions(self):
if self.is_upgrade:
return (self.revision.revision,)
else:
return self.revision._all_down_revisions
return self.revision._normalized_down_revisions

@property
def to_revisions_no_deps(self):
Expand All @@ -1007,7 +1007,7 @@ def to_revisions_no_deps(self):

@property
def _has_scalar_down_revision(self):
return len(self.revision._all_down_revisions) == 1
return len(self.revision._normalized_down_revisions) == 1

def should_delete_branch(self, heads):
"""A delete is when we are a. in a downgrade and b.
Expand All @@ -1021,7 +1021,7 @@ def should_delete_branch(self, heads):
if self.revision.revision not in heads:
return False

downrevs = self.revision._all_down_revisions
downrevs = self.revision._normalized_down_revisions

if not downrevs:
# is a base
Expand Down Expand Up @@ -1082,7 +1082,7 @@ def should_create_branch(self, heads):
if not self.is_upgrade:
return False

downrevs = self.revision._all_down_revisions
downrevs = self.revision._normalized_down_revisions

if not downrevs:
# is a base
Expand All @@ -1101,7 +1101,7 @@ def should_merge_branches(self, heads):
if not self.is_upgrade:
return False

downrevs = self.revision._all_down_revisions
downrevs = self.revision._normalized_down_revisions

if len(downrevs) > 1 and len(heads.intersection(downrevs)) > 1:
return True
Expand All @@ -1112,7 +1112,7 @@ def should_unmerge_branches(self, heads):
if not self.is_downgrade:
return False

downrevs = self.revision._all_down_revisions
downrevs = self.revision._normalized_down_revisions

if self.revision.revision in heads and len(downrevs) > 1:
return True
Expand All @@ -1121,13 +1121,15 @@ def should_unmerge_branches(self, heads):

def update_version_num(self, heads):
if not self._has_scalar_down_revision:
downrev = heads.intersection(self.revision._all_down_revisions)
downrev = heads.intersection(
self.revision._normalized_down_revisions
)
assert (
len(downrev) == 1
), "Can't do an UPDATE because downrevision is ambiguous"
down_revision = list(downrev)[0]
else:
down_revision = self.revision._all_down_revisions[0]
down_revision = self.revision._normalized_down_revisions[0]

if self.is_upgrade:
return down_revision, self.revision.revision
Expand All @@ -1147,7 +1149,7 @@ def info(self):
return MigrationInfo(
revision_map=self.revision_map,
up_revisions=self.revision.revision,
down_revisions=self.revision._all_down_revisions,
down_revisions=self.revision._normalized_down_revisions,
is_upgrade=self.is_upgrade,
is_stamp=False,
)
Expand Down
Loading

0 comments on commit 63da357

Please sign in to comment.