Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

renumber-migrations: Restrict dep rewrite to in-app tuples.

Alex Vandiver60f644016c06
diff --git a/tools/renumber-migrations b/tools/renumber-migrations
index 0522ab0a914..f216ab76a01 100755
--- a/tools/renumber-migrations
+++ b/tools/renumber-migrations
@@ -19,33 +19,49 @@ def validate_order(order: list[int], length: int) -> None:
def renumber_migration(
- conflicts: list[str], order: list[int], last_correct_migration: str, migrations_dir: str
+ conflicts: list[str],
+ order: list[int],
+ last_correct_migration: str,
+ migrations_dir: str,
+ app_label: str,
) -> None:
+ # Match only ("<app_label>", "NNNN_xxx") tuples, not things like
+ # `1_000` or cross-app deps. ruff-format always puts `, ` between
+ # the two tuple elements, so the replacement hardcodes that.
+ dep_pattern = re.compile(rf'"{re.escape(app_label)}"\s*,\s*"\d+(?:_[a-z0-9]+)+"')
stack: list[str] = []
for i in order:
if conflicts[i - 1][0:4] not in stack:
stack.append(conflicts[i - 1][0:4])
else:
- # Replace dependencies with the last correct migration
- with fileinput.FileInput(
- os.path.join(migrations_dir, conflicts[i - 1]), inplace=True
- ) as file:
- for line in file:
- print(re.sub(r"[\d]+(_[a-z0-9]+)+", last_correct_migration, line), end="")
+ path = os.path.join(migrations_dir, conflicts[i - 1])
+ # Don't touch migration-merge files: with two in-app deps in
+ # the same file, the substitution would collapse both to the
+ # same predecessor and produce a self-referential migration.
+ with open(path) as f:
+ if len(dep_pattern.findall(f.read())) > 1:
+ print(
+ f"Refusing to renumber {conflicts[i - 1]}: it has multiple "
+ f"in-app dependencies (a migration merge). Renumber it by hand."
+ )
+ sys.exit(1)
+ replacement = f'"{app_label}", "{last_correct_migration}"'
+ with fileinput.FileInput(path, inplace=True) as f:
+ for line in f:
+ print(dep_pattern.sub(replacement, line), end="")
# Rename the migration indexing at the end
new_name = conflicts[i - 1].replace(
conflicts[i - 1][0:4], f"{int(last_correct_migration[0:4]) + 1:04}"
)
- os.rename(
- os.path.join(migrations_dir, conflicts[i - 1]),
- os.path.join(migrations_dir, new_name),
- )
+ os.rename(path, os.path.join(migrations_dir, new_name))
last_correct_migration = new_name.replace(".py", "")
-def resolve_conflicts(conflicts: list[str], files_list: list[str], migrations_dir: str) -> None:
+def resolve_conflicts(
+ conflicts: list[str], files_list: list[str], migrations_dir: str, app_label: str
+) -> None:
print("Conflicting migrations:")
for i in range(len(conflicts)):
print(str(i + 1) + ". " + conflicts[i])
@@ -57,7 +73,7 @@ def resolve_conflicts(conflicts: list[str], files_list: list[str], migrations_di
last_correct_migration = conflicts[order[0] - 1]
last_correct_migration = last_correct_migration.replace(".py", "")
- renumber_migration(conflicts, order, last_correct_migration, migrations_dir)
+ renumber_migration(conflicts, order, last_correct_migration, migrations_dir, app_label)
if __name__ == "__main__":
@@ -104,7 +120,7 @@ if __name__ == "__main__":
stack.append(migration_number)
if len(conflicts) > 0:
- resolve_conflicts(conflicts, files_list, migrations_dir)
+ resolve_conflicts(conflicts, files_list, migrations_dir, args.app_label)
else:
break