-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit 7669290)
- Loading branch information
Showing
10 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
11 changes: 1 addition & 10 deletions
11
src/nonebot_plugin_mahjong_scoreboard/model/orm/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1 @@ | ||
from nonebot import get_driver, require | ||
|
||
from nonebot_plugin_mahjong_scoreboard.config import conf | ||
|
||
require("nonebot_plugin_sqlalchemy") | ||
from nonebot_plugin_sqlalchemy import DataSource | ||
|
||
data_source = DataSource(get_driver(), conf.mahjong_scoreboard_database_conn_url) | ||
|
||
__all__ = ("data_source",) | ||
from ._data_source import data_source |
5 changes: 5 additions & 0 deletions
5
src/nonebot_plugin_mahjong_scoreboard/model/orm/_data_source/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from . import metainfo | ||
from . import migrations | ||
from .src import data_source, dialect | ||
|
||
__all__ = ("data_source", "dialect") |
55 changes: 55 additions & 0 deletions
55
src/nonebot_plugin_mahjong_scoreboard/model/orm/_data_source/metainfo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from sqlalchemy import Column, String, JSON, inspect, select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from .src import data_source | ||
|
||
APP_DB_VERSION = 2 | ||
|
||
|
||
@data_source.registry.mapped | ||
class MetaInfoOrm: | ||
__tablename__ = 'metainfo' | ||
|
||
key: str = Column(String(64), primary_key=True) | ||
value = Column(JSON) | ||
|
||
|
||
async def get_metainfo(key: str): | ||
async with AsyncSession(data_source.engine) as session: | ||
record = await session.get(MetaInfoOrm, key) | ||
return record.value | ||
|
||
|
||
async def set_metainfo(key: str, value: any): | ||
async with AsyncSession(data_source.engine) as session: | ||
record = await session.get(MetaInfoOrm, key) | ||
if record is None: | ||
record = MetaInfoOrm(key=key, value=value) | ||
session.add(record) | ||
|
||
record.value = value | ||
await session.commit() | ||
|
||
|
||
@data_source.on_engine_created | ||
async def initialize_metainfo(): | ||
async with data_source.engine.begin() as conn: | ||
await conn.run_sync(lambda conn: MetaInfoOrm.__table__.create(conn, checkfirst=True)) | ||
|
||
async with data_source.engine.begin() as conn: | ||
async with AsyncSession(data_source.engine, expire_on_commit=False) as session: | ||
# 判断是否初次建库 | ||
blank_database = not await conn.run_sync(lambda conn: inspect(conn).has_table("games")) | ||
if blank_database: | ||
result = MetaInfoOrm(key="db_version", value=APP_DB_VERSION) | ||
session.add(result) | ||
await session.commit() | ||
else: | ||
stmt = select(MetaInfoOrm).where(MetaInfoOrm.key == "db_version") | ||
result = (await session.execute(stmt)).scalar_one_or_none() | ||
if result is None: | ||
result = MetaInfoOrm(key="db_version", value=1) | ||
session.add(result) | ||
await session.commit() | ||
|
||
await conn.commit() |
21 changes: 21 additions & 0 deletions
21
src/nonebot_plugin_mahjong_scoreboard/model/orm/_data_source/migrations/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from nonebot import logger | ||
|
||
from .v1_to_v2 import migrate_v1_to_v2 | ||
from ..metainfo import get_metainfo, APP_DB_VERSION, set_metainfo | ||
from ..src import data_source | ||
|
||
migrations = { | ||
(1, 2): migrate_v1_to_v2 | ||
} | ||
|
||
|
||
@data_source.on_ready | ||
async def do_migrate(): | ||
cur_db_version = await get_metainfo('db_version') | ||
while cur_db_version < APP_DB_VERSION: | ||
mig = migrations[cur_db_version, cur_db_version + 1] | ||
await mig() | ||
await set_metainfo('db_version', cur_db_version + 1) | ||
logger.success(f"migrate database from version {cur_db_version} to version {cur_db_version + 1}") | ||
|
||
cur_db_version += 1 |
8 changes: 8 additions & 0 deletions
8
src/nonebot_plugin_mahjong_scoreboard/model/orm/_data_source/migrations/v1_to_v2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from sqlalchemy import text | ||
|
||
from ..src import data_source | ||
|
||
|
||
async def migrate_v1_to_v2(): | ||
async with data_source.engine.begin() as conn: | ||
await conn.execute(text("ALTER TABLE game_records ADD point_scale integer NOT NULL DEFAULT 0;")) |
23 changes: 23 additions & 0 deletions
23
src/nonebot_plugin_mahjong_scoreboard/model/orm/_data_source/src.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from urllib.parse import urlparse | ||
|
||
from nonebot import get_driver, require | ||
|
||
from nonebot_plugin_mahjong_scoreboard.config import conf | ||
|
||
require("nonebot_plugin_sqlalchemy") | ||
from nonebot_plugin_sqlalchemy import DataSource | ||
|
||
data_source = DataSource(get_driver(), conf.mahjong_scoreboard_database_conn_url) | ||
|
||
|
||
def _detect_dialect(): | ||
url = urlparse(conf.mahjong_scoreboard_database_conn_url) | ||
if '+' in url.scheme: | ||
return url.scheme.split('+')[0] | ||
else: | ||
return url.scheme | ||
|
||
|
||
dialect = _detect_dialect() | ||
|
||
__all__ = ("data_source", "dialect") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters