diff --git a/CHANGELOG.md b/CHANGELOG.md index 799897b..ea53d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix syntax error with python3.8.10. (#265) - Fix sql generate error. (#263) +- Fix initialize an empty database. (#267) ### 0.7.1rc1 diff --git a/aerich/__init__.py b/aerich/__init__.py index 67ef7e3..75f4e05 100644 --- a/aerich/__init__.py +++ b/aerich/__init__.py @@ -50,7 +50,7 @@ async def upgrade(self): file_path = Path(Migrate.migrate_location, version_file) m = import_py_file(file_path) upgrade = getattr(m, "upgrade") - await upgrade(conn) + await conn.execute_script(await upgrade(conn)) await Aerich.create( version=version_file, app=self.app, @@ -80,10 +80,11 @@ async def downgrade(self, version: int, delete: bool): ) as conn: file_path = Path(Migrate.migrate_location, file) m = import_py_file(file_path) - downgrade = getattr(m, "downgrade", None) - if not downgrade: + downgrade = getattr(m, "downgrade") + downgrade_sql = await downgrade(conn) + if not downgrade_sql.strip(): raise DowngradeError("No downgrade items found") - await downgrade(conn) + await conn.execute_script(downgrade_sql) await version.delete() if delete: os.unlink(file_path) @@ -138,6 +139,6 @@ async def init_db(self, safe: bool): content=get_models_describe(app), ) version_file = Path(dirname, version) - content = MIGRATE_TEMPLATE.format(upgrade_sql=f'"""{schema}"""', downgrade_sql="") + content = MIGRATE_TEMPLATE.format(upgrade_sql=schema, downgrade_sql="") with open(version_file, "w", encoding="utf-8") as f: f.write(content)