From ad54b5e9dd5c14952b8d020dad6a088e2f752bcf Mon Sep 17 00:00:00 2001 From: ar0ne Date: Tue, 26 Dec 2023 23:12:11 +0530 Subject: [PATCH] remove redundant comma for empty migration --- aerich/migrate.py | 20 ++++++++++++++++---- tests/test_migrate.py | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/aerich/migrate.py b/aerich/migrate.py index e5c3cec..27fba58 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -120,10 +120,7 @@ class Migrate: os.unlink(Path(cls.migrate_location, version_file)) version_file = Path(cls.migrate_location, version) - content = MIGRATE_TEMPLATE.format( - upgrade_sql=";\n ".join(cls.upgrade_operators) + ";", - downgrade_sql=";\n ".join(cls.downgrade_operators) + ";", - ) + content = cls._get_diff_file_content() with open(version_file, "w", encoding="utf-8") as f: f.write(content) @@ -151,6 +148,21 @@ class Migrate: return await cls._generate_diff_py(name) + @classmethod + def _get_diff_file_content(cls) -> str: + """ + builds content for diff file from template + """ + def join_lines(lines: List[str]) -> str: + if not lines: + return "" + return ";\n ".join(lines) + ";" + + return MIGRATE_TEMPLATE.format( + upgrade_sql=join_lines(cls.upgrade_operators), + downgrade_sql=join_lines(cls.downgrade_operators) + ) + @classmethod def _add_operator(cls, operator: str, upgrade=True, fk_m2m_index=False): """ diff --git a/tests/test_migrate.py b/tests/test_migrate.py index c3cca03..a94d73f 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -973,7 +973,7 @@ def test_sort_all_version_files(mocker): async def test_empty_migration(mocker) -> None: mocker.patch("os.listdir", return_value=[]) Migrate.app = "foo" - expected_content = MIGRATE_TEMPLATE.format(upgrade_sql=";", downgrade_sql=";") + expected_content = MIGRATE_TEMPLATE.format(upgrade_sql="", downgrade_sql="") with tempfile.TemporaryDirectory() as temp_dir: Migrate.migrate_location = temp_dir