Skip to content

Commit

Permalink
chore: 存版本号并添加入口,方便触发版本间的迁移逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer committed Jan 4, 2024
1 parent e36f829 commit 3defb07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions migrations/models/3_20240104221037_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
return """
CREATE TABLE IF NOT EXISTS "program" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"version" VARCHAR(20) NOT NULL
);"""


async def downgrade(db: BaseDBAsyncClient) -> str:
return """
DROP TABLE IF EXISTS "program";"""
16 changes: 16 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from settings import settings
from utils import aopen
from version import VERSION


class FavoriteList(Model):
Expand Down Expand Up @@ -148,6 +149,11 @@ def subtitle_path(self) -> Path:
)


class Program(Model):
id = fields.IntField(pk=True)
version = fields.CharField(max_length=20)


async def init_model() -> None:
await Tortoise.init(config=TORTOISE_ORM)
migrate_commands = (
Expand All @@ -157,3 +163,13 @@ async def init_model() -> None:
)
process = await create_subprocess_exec(*migrate_commands)
await process.communicate()
program, created = await Program.get_or_create(
defaults={
"version": VERSION,
}
)
if created or program.version != VERSION:
# 把新版本的迁移逻辑写在这里
pass
program.version = VERSION
await program.save()

0 comments on commit 3defb07

Please sign in to comment.