Skip to content

Commit

Permalink
Fixed two problems when using under windows (#286)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
tufbel and waketzheng authored Dec 10, 2024
1 parent 9c81bc6 commit accceef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 6 additions & 1 deletion aerich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ async def init_db(self, safe: bool) -> None:
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)
Expand Down
2 changes: 1 addition & 1 deletion aerich/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
23 changes: 19 additions & 4 deletions tests/test_sqlite_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

0 comments on commit accceef

Please sign in to comment.