| 25 | |
| 26 | |
| 27 | def norm(s: str) -> dict[str, str]: |
| 28 | dbs: dict[str, list[str]] |
| 29 | dbs = collections.defaultdict(list) |
| 30 | |
| 31 | db = "ROOT" |
| 32 | last = "" |
| 33 | create_table_parts: list[str] = [] |
| 34 | |
| 35 | s = _remove_migrations_tables(s) |
| 36 | |
| 37 | for line in s.splitlines(True): |
| 38 | if line.startswith(r"\connect "): |
| 39 | _, db = line.split() |
| 40 | if line.startswith("--"): |
| 41 | continue |
| 42 | if last == "\n" and line == "\n": |
| 43 | continue |
| 44 | else: |
| 45 | last = line |
| 46 | line = CONSTRAINT_RE.sub(_constraint_replacement, line) |
| 47 | if create_table_parts: |
| 48 | if line == ");\n": |
| 49 | dbs[db].append(create_table_parts[0]) |
| 50 | create_table_parts[-1] = create_table_parts[-1].replace("\n", ",\n") |
| 51 | for part in sorted(create_table_parts[1:]): |
| 52 | dbs[db].append(part) |
| 53 | create_table_parts = [] |
| 54 | dbs[db].append(line) |
| 55 | else: |
| 56 | create_table_parts.append(line) |
| 57 | elif line.startswith("CREATE TABLE "): |
| 58 | create_table_parts.append(line) |
| 59 | else: |
| 60 | dbs[db].append(line) |
| 61 | |
| 62 | return {k: "\n\n".join(sorted("".join(v).split("\n\n"))).strip() for k, v in dbs.items()} |
| 63 | |
| 64 | |
| 65 | def main() -> int: |