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: Extract a renumber_one helper.

Alex Vandiverac8399ce34e9
diff --git a/tools/renumber-migrations b/tools/renumber-migrations
index a52f777b89e..2f22b40b4ee 100755
--- a/tools/renumber-migrations
+++ b/tools/renumber-migrations
@@ -19,6 +19,38 @@ def validate_order(order: list[int], length: int) -> None:
sys.exit(1)
+def renumber_one(
+ filename: str,
+ new_number: int,
+ new_predecessor: str,
+ migrations_dir: str,
+ app_label: str,
+) -> str:
+ """Rewrite the file's in-app dep tuples to point at new_predecessor, then rename it."""
+ path = os.path.join(migrations_dir, filename)
+ # 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]+)+"')
+ # 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 {filename}: it has multiple "
+ f"in-app dependencies (a migration merge). Renumber it by hand."
+ )
+ sys.exit(1)
+ replacement = f'"{app_label}", "{new_predecessor}"'
+ with fileinput.FileInput(path, inplace=True) as f:
+ for line in f:
+ print(dep_pattern.sub(replacement, line), end="")
+ new_name = f"{new_number:04}{filename[4:]}"
+ os.rename(path, os.path.join(migrations_dir, new_name))
+ return new_name
+
+
def renumber_migration(
conflicts: list[str],
order: list[int],
@@ -26,34 +58,16 @@ def renumber_migration(
migrations_dir: str,
app_label: str,
) -> None:
- # `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]+)+"')
seen_prefixes: set[str] = set()
for i in order:
prefix = conflicts[i - 1][0:4]
if prefix not in seen_prefixes:
seen_prefixes.add(prefix)
continue
- 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="")
-
- new_name = conflicts[i - 1].replace(prefix, f"{int(last_correct_migration[0:4]) + 1:04}")
- os.rename(path, os.path.join(migrations_dir, new_name))
-
+ new_number = int(last_correct_migration[0:4]) + 1
+ new_name = renumber_one(
+ conflicts[i - 1], new_number, last_correct_migration, migrations_dir, app_label
+ )
last_correct_migration = new_name.removesuffix(".py")