Skip to content

Commit

Permalink
Fix upgrade in new db. (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Nov 30, 2020
1 parent bfa66f6 commit 4e91749
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 0.4.2

- Use `pathlib` for path resolving. (#89)
- Fix upgrade in new db. (#96)

### 0.4.1

Expand Down
4 changes: 3 additions & 1 deletion aerich/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ async def cli(ctx: Context, config, app, name):
ctx.obj["app"] = app
Migrate.app = app
if invoked_subcommand != "init-db":
if not Path(location, app).exists():
raise UsageError("You must exec init-db first", ctx=ctx)
await Migrate.init_with_old_models(tortoise_config, app, location)


Expand Down Expand Up @@ -123,7 +125,7 @@ async def upgrade(ctx: Context):
click.secho(f"Success upgrade {version_file}", fg=Color.green)
migrated = True
if not migrated:
click.secho("No migrate items", fg=Color.yellow)
click.secho("No items to be migrated", fg=Color.yellow)


@cli.command(help="Downgrade to specified version.")
Expand Down
12 changes: 9 additions & 3 deletions aerich/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ def get_version_content_from_file(version_file: str) -> Dict:
with open(version_file, "r", encoding="utf-8") as f:
content = f.read()
first = content.index(_UPGRADE)
second = content.index(_DOWNGRADE)
try:
second = content.index(_DOWNGRADE)
except ValueError:
second = len(content) - 1
upgrade_content = content[first + len(_UPGRADE) : second].strip() # noqa:E203
downgrade_content = content[second + len(_DOWNGRADE) :].strip() # noqa:E203
ret = {"upgrade": upgrade_content.split("\n"), "downgrade": downgrade_content.split("\n")}
ret = {"upgrade": upgrade_content.split(";\n"), "downgrade": downgrade_content.split(";\n")}
return ret


Expand All @@ -85,7 +88,10 @@ def write_version_file(version_file: str, content: Dict):
if len(upgrade) > 1:
f.write(";\n".join(upgrade) + ";\n")
else:
f.write(f"{upgrade[0]};\n")
f.write(f"{upgrade[0]}")
if not upgrade[0].endswith(";"):
f.write(";")
f.write("\n")
downgrade = content.get("downgrade")
if downgrade:
f.write(_DOWNGRADE)
Expand Down

0 comments on commit 4e91749

Please sign in to comment.