Skip to content

Commit

Permalink
fix: initialize an empty database. (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Sep 27, 2022
1 parent ab31445 commit f8e1f9f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions aerich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit f8e1f9f

Please sign in to comment.