Skip to content

Commit e7d8ab5

Browse files
authored
fix: aerich migrate crashes without init-db & aerich init-db should create folder after validating app (#443)
* fix: aerich init-db should create folder after get_app_connection * fix: migrate without init-db will raise error * aerich init-db should create folder after validating app * fix: catch empty old model exception and raise with a tip.
1 parent a333605 commit e7d8ab5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

aerich/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ async def migrate(self, name: str = "update", empty: bool = False) -> str:
256256
async def init_db(self, safe: bool) -> None:
257257
location = self.location
258258
app = self.app
259+
260+
await Tortoise.init(config=self.tortoise_config)
261+
connection = get_app_connection(self.tortoise_config, app)
262+
259263
dirname = Path(location, app)
260264
if not dirname.exists():
261265
dirname.mkdir(parents=True)
@@ -264,8 +268,6 @@ async def init_db(self, safe: bool) -> None:
264268
for unexpected_file in dirname.glob("*"):
265269
raise FileExistsError(str(unexpected_file))
266270

267-
await Tortoise.init(config=self.tortoise_config)
268-
connection = get_app_connection(self.tortoise_config, app)
269271
await generate_schema_for_client(connection, safe)
270272

271273
schema = get_schema_sql(connection, safe)

aerich/migrate.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,13 @@ def diff_models(
405405
:return:
406406
"""
407407
_aerich = f"{cls.app}.{cls._aerich}"
408-
old_models.pop(_aerich, None)
408+
try:
409+
old_models.pop(_aerich, None)
410+
except AttributeError:
411+
# Invalid use when app migration directory exists but aerich table not exist
412+
raise click.UsageError(
413+
"You may need to run `aerich init-db` first to initialize the database."
414+
)
409415
new_models.pop(_aerich, None)
410416
models_with_rename_field: set[str] = set() # models that trigger the click.prompt
411417

0 commit comments

Comments
 (0)