Fix upgrade in new db. (#96)

This commit is contained in:
long2ice 2020-11-30 11:02:48 +08:00
parent bfa66f6dd4
commit 4e917495a0
3 changed files with 13 additions and 4 deletions

View File

@ -5,6 +5,7 @@
### 0.4.2
- Use `pathlib` for path resolving. (#89)
- Fix upgrade in new db. (#96)
### 0.4.1

View File

@ -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.")

View File

@ -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)