Skip to content

Commit

Permalink
refactor db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 23, 2024
1 parent 0d4c2fb commit f7b6394
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 168 deletions.
27 changes: 0 additions & 27 deletions a.js

This file was deleted.

33 changes: 0 additions & 33 deletions episode.sql

This file was deleted.

65 changes: 0 additions & 65 deletions schema.sql

This file was deleted.

76 changes: 33 additions & 43 deletions server/migration.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,61 @@
"""A simple run_migration"""

from pathlib import Path
from typing import NamedTuple

from asyncpg import UndefinedTableError

from server.base import pg


sql_dir = Path(__file__, "../sql")

KEY_MIGRATION_VERSION = "version"


class Migrate(NamedTuple):
version: int
sql: str


migrations: list[Migrate] = [
Migrate(1, "select 1"),
Migrate(
2,
"""
create table episode_patch
(
id uuid primary key not null,
episode_id integer not null,
state integer default 0 not null,
from_user_id integer not null,
wiki_user_id integer default 0 not null,
reason text not null,
original_name text,
name text,
original_name_cn text,
name_cn text,
original_duration varchar(255),
duration varchar(255),
original_airdate varchar(64),
airdate varchar(64),
original_description text,
description text,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,
deleted_at timestamp with time zone,
reject_reason varchar(255) default '' not null
);
create index on episode_patch (state);
""",
),
Migrate(1, sql_dir.joinpath("001-init.sql").read_text(encoding="utf8")),
Migrate(2, "select 1;"), # noop
]


async def run_migration() -> None:
v = await pg.fetchval("select value from patch_db_migration where key=$1", "version")
v = None
try:
v = await pg.fetchval(
"select value from patch_db_migration where key=$1", KEY_MIGRATION_VERSION
)
except UndefinedTableError:
# do init
await pg.execute(
"""
create table if not exists patch_db_migration(
key text primary key not null,
value text not null
)
"""
)

if v is None:
await pg.execute(
"insert into patch_db_migration (key, value) VALUES ($1,$2)", "version", "0"
"insert into patch_db_migration (key, value) VALUES ($1, $2)",
KEY_MIGRATION_VERSION,
"0",
)
return
v = "0"

current_version = int(v)
for migrate in migrations:
if migrate.version <= current_version:
continue
await pg.execute(migrate.sql)
await pg.execute(
"update patch_db_migration set value = $1 where key = 'version'", str(migrate.version)
"update patch_db_migration set value = $1 where key = $2",
str(migrate.version),
KEY_MIGRATION_VERSION,
)
62 changes: 62 additions & 0 deletions server/sql/001-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
create table patch
(
id uuid default gen_random_uuid() not null
primary key,
subject_id integer not null,
state integer default 0 not null,
from_user_id integer not null,
wiki_user_id integer default 0 not null,
reason text not null,
name text,
original_name text default ''::text not null,
infobox text,
original_infobox text,
summary text,
original_summary text,
nsfw boolean,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,
deleted_at timestamp with time zone,
reject_reason varchar(255) default ''::character varying not null,
subject_type bigint default 0 not null
);

create index idx_subject_id on patch (subject_id);

create index idx_deleted_at on patch (deleted_at);

create table patch_users
(
user_id integer not null
primary key,
username varchar(255) not null,
nickname varchar(255) not null
);

create table episode_patch
(
id uuid not null
primary key,
episode_id integer not null,
state integer default 0 not null,
from_user_id integer not null,
wiki_user_id integer default 0 not null,
reason text not null,
original_name text,
name text,
original_name_cn text,
name_cn text,
original_duration varchar(255),
duration varchar(255),
original_airdate varchar(64),
airdate varchar(64),
original_description text,
description text,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,
deleted_at timestamp with time zone,
reject_reason varchar(255) default ''::character varying not null
);

create index episode_patch_state_idx
on episode_patch (state);

0 comments on commit f7b6394

Please sign in to comment.