From accceef24fb8dc21e032f05ea3b1eabbaba7591e Mon Sep 17 00:00:00 2001 From: Tuffy_ Date: Tue, 10 Dec 2024 23:02:49 +0800 Subject: [PATCH] Fixed two problems when using under windows (#286) * fix: Fixed an issue where an error would occur when using aerich in windows if the profile contained Chinese characters * fix: Automatically delete the empty migration directory of the app if the init-db operation fails * feat: generate migration file in empty directory instead of abort with warning * tests: fix test fail in ci --------- Co-authored-by: Waket Zheng --- aerich/__init__.py | 7 ++++++- aerich/cli.py | 2 +- tests/test_sqlite_migrate.py | 23 +++++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/aerich/__init__.py b/aerich/__init__.py index 6035a46..d552bf8 100644 --- a/aerich/__init__.py +++ b/aerich/__init__.py @@ -133,7 +133,12 @@ class Command: location = self.location app = self.app dirname = Path(location, app) - dirname.mkdir(parents=True) + if not dirname.exists(): + dirname.mkdir(parents=True) + else: + # If directory is empty, go ahead, otherwise raise FileExistsError + for unexpected_file in dirname.glob("*"): + raise FileExistsError(str(unexpected_file)) await Tortoise.init(config=self.tortoise_config) connection = get_app_connection(self.tortoise_config, app) diff --git a/aerich/cli.py b/aerich/cli.py index 09035fd..25aeb8c 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -40,7 +40,7 @@ async def cli(ctx: Context, config, app) -> None: raise UsageError( "You need to run `aerich init` first to create the config file.", ctx=ctx ) - content = config_path.read_text() + content = config_path.read_text("utf-8") doc: dict = tomlkit.parse(content) try: tool = cast(Dict[str, str], doc["tool"]["aerich"]) diff --git a/tests/test_sqlite_migrate.py b/tests/test_sqlite_migrate.py index c14a4dd..e1478a0 100644 --- a/tests/test_sqlite_migrate.py +++ b/tests/test_sqlite_migrate.py @@ -156,6 +156,8 @@ def test_sqlite_migrate(tmp_path: Path) -> None: settings_py.write_text(SETTINGS) test_py.write_text(TESTS) Path("conftest.py").write_text(CONFTEST) + if (db_file := Path("db.sqlite3")).exists(): + db_file.unlink() run_aerich("aerich init -t settings.TORTOISE_ORM") run_aerich("aerich init-db") r = run_shell("pytest _test.py::test_allow_duplicate") @@ -199,19 +201,32 @@ def test_sqlite_migrate(tmp_path: Path) -> None: assert r.returncode == 0 # Initial with indexed field and then drop it - shutil.rmtree("migrations") - Path("db.sqlite3").unlink() + migrations_dir = Path("migrations/models") + shutil.rmtree(migrations_dir) + db_file.unlink() models_py.write_text(MODELS + " age = fields.IntField(db_index=True)") run_aerich("aerich init -t settings.TORTOISE_ORM") run_aerich("aerich init-db") - migration_file = list(Path("migrations/models").glob("0_*.py"))[0] + migration_file = list(migrations_dir.glob("0_*.py"))[0] assert "CREATE INDEX" in migration_file.read_text() r = run_shell("pytest _test.py::test_with_age_field") assert r.returncode == 0 models_py.write_text(MODELS) run_aerich("aerich migrate") run_aerich("aerich upgrade") - migration_file_1 = list(Path("migrations/models").glob("1_*.py"))[0] + migration_file_1 = list(migrations_dir.glob("1_*.py"))[0] assert "DROP INDEX" in migration_file_1.read_text() r = run_shell("pytest _test.py::test_without_age_field") assert r.returncode == 0 + + # Generate migration file in emptry directory + db_file.unlink() + run_aerich("aerich init-db") + assert not db_file.exists() + for p in migrations_dir.glob("*"): + if p.is_dir(): + shutil.rmtree(p) + else: + p.unlink() + run_aerich("aerich init-db") + assert db_file.exists()