diff --git a/CHANGELOG.md b/CHANGELOG.md index 153e001..4a82424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 0.4.2 - Use `pathlib` for path resolving. (#89) +- Fix upgrade in new db. (#96) ### 0.4.1 diff --git a/aerich/cli.py b/aerich/cli.py index 3c41d2a..f348072 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -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) @@ -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.") diff --git a/aerich/utils.py b/aerich/utils.py index b4bb636..b500ab9 100644 --- a/aerich/utils.py +++ b/aerich/utils.py @@ -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 @@ -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)