Skip to content

Commit

Permalink
refactor: use pgsql uuid generator
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 23, 2024
1 parent 7a0309a commit febba08
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 121 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "common"]
path = common
url = https://github.com/Bangumi/common
[submodule "vendor/uuidv7"]
path = vendor/uuidv7
url = https://github.com/Betterment/postgresql-uuid-generate-v7.git
93 changes: 4 additions & 89 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ sslog = "0.0.0a48"
litestar = { version = "2.12.1", extras = ['jinja'] }
redis = { extras = ["hiredis"], version = "5.1.1" }
uvicorn = { version = "0.32.0" }
uuid-utils = "0.9.0"
bgm-tv-wiki = "0.0.29"
pyyaml = "6.0.2"
msgspec = "0.18.6"
Expand Down
16 changes: 13 additions & 3 deletions server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any, TypeAlias
from uuid import UUID
from uuid import UUID, uuid4

import asyncpg
import httpx
Expand All @@ -15,9 +15,8 @@
from redis.asyncio import Redis
from sslog import logger
from structlog.contextvars import bind_contextvars, reset_contextvars
from uuid_utils.compat import uuid4

from server.config import PG_DSN, REDIS_DSN
from server.config import PG_DSN, PROJECT_PATH, REDIS_DSN


session_key_back_to = "backTo"
Expand Down Expand Up @@ -83,6 +82,17 @@ class QueueItem:
async def pg_pool_startup() -> None:
logger.info("init")
await pg
pgcrypto_enabled = await pg.fetchval(
"SELECT count(1) FROM pg_extension where extname = 'pgcrypto'"
)
if not pgcrypto_enabled:
if not await pg.fetchval(
"SELECT count(1) FROM pg_available_extensions where extname = 'pgcrypto'"
):
raise Exception("require pgcrypto to be available")
await pg.execute("create extension pgcrypto;")

await pg.execute(PROJECT_PATH.joinpath("./vendor/uuidv7/uuid_generate_v7.sql").read_text())


Request = litestar.Request[None, User | None, Any]
Expand Down
33 changes: 12 additions & 21 deletions server/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
)
from litestar.params import Body
from litestar.response import Redirect, Template
from uuid_utils.compat import uuid7

from common.py.platform import PLATFORM_CONFIG, WIKI_TEMPLATES
from server.auth import require_user_login
Expand Down Expand Up @@ -131,15 +130,13 @@ async def suggest_api(
if key in changed:
check_invalid_input_str(changed[key])

pk = uuid7()

await pg.execute(
pk = await pg.fetchval(
"""
insert into subject_patch (id, subject_id, from_user_id, reason, name, infobox, summary, nsfw,
original_name, original_infobox, original_summary, subject_type, patch_desc)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
VALUES (uuid_generate_v7(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
returning id
""",
pk,
subject_id,
request.auth.user_id,
data.reason,
Expand Down Expand Up @@ -220,15 +217,13 @@ async def suggest_api_from_partial(
if key in changed:
check_invalid_input_str(changed[key])

pk = uuid7()

await pg.execute(
pk = await pg.fetchval(
"""
insert into subject_patch (id, subject_id, from_user_id, reason, name, infobox, summary, nsfw,
original_name, original_infobox, original_summary, subject_type, patch_desc)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
VALUES (uuid_generate_v7(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
returning id
""",
pk,
subject_id,
request.auth.user_id,
data.reason,
Expand Down Expand Up @@ -507,16 +502,14 @@ async def creat_episode_patch(
if c := contains_invalid_input_str(value):
raise BadRequestException(f"{patch_keys[key]} 包含不可见字符 {c!r}")

pk = uuid7()

await pg.execute(
pk = await pg.fetchval(
"""
insert into episode_patch (id, episode_id, from_user_id, reason, original_name, name,
original_name_cn, name_cn, original_duration, duration,
original_airdate, airdate, original_description, description, subject_id, ep)
VALUES ($1, $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)
VALUES (uuid_generate_v7(), $1, $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)
returning id
""",
pk,
episode_id,
request.auth.user_id,
reason,
Expand Down Expand Up @@ -612,15 +605,13 @@ async def patch_for_new_subject(
if data.platform not in PLATFORM_CONFIG.get(data.type_id, {}):
raise BadRequestException("平台不正确")

pk = uuid7()

await pg.execute(
pk = await pg.fetchval(
"""
insert into subject_patch (id, subject_id, from_user_id, reason, name, infobox, summary, nsfw,
subject_type, platform, patch_desc, action, original_name)
VALUES ($1, 0, $2, '', $3, $4, $5, $6, $7, $8, $9, $10, '')
VALUES (uuid_generate_v7(), 0, $1, '', $2, $3, $4, $5, $6, $7, $8, $9, '')
returning id
""",
pk,
request.auth.user_id,
data.name,
data.infobox,
Expand Down
4 changes: 1 addition & 3 deletions server/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from bgm_tv_wiki import WikiSyntaxError, parse
from litestar import Litestar
from sslog import logger
from uuid_utils import uuid7

from server.base import QueueItem, pg, subject_infobox_queue
from server.model import PatchType
Expand All @@ -26,9 +25,8 @@ async def check_infobox_error(item: QueueItem) -> None:
await pg.execute(
"""
insert into edit_suggestion (id, patch_id, patch_type, text, from_user)
VALUES ($1, $2, $3, $4, $5)
VALUES (uuid_generate_v7(), $1, $2, $3, $4)
""",
uuid7(),
item.patch_id,
PatchType.Subject,
"infobox 包含语法错误,请检查\n" + msg,
Expand Down
4 changes: 1 addition & 3 deletions server/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from litestar.params import Body
from litestar.response import Redirect
from sslog import logger
from uuid_utils import uuid7

from server.auth import refresh_access_token, require_user_editor, require_user_login
from server.base import (
Expand Down Expand Up @@ -449,9 +448,8 @@ async def add_comment(
await conn.execute(
"""
insert into edit_suggestion (id, patch_id, patch_type, text, from_user)
values ($1, $2, $3, $4, $5)
values (uuid_generate_v7(), $1, $2, $3, $4)
""",
uuid7(),
patch_id,
patch_type,
text,
Expand Down
Loading

0 comments on commit febba08

Please sign in to comment.