mirror of
https://github.com/A-Minos/nonebot-plugin-tetris-stats.git
synced 2026-03-05 05:36:54 +08:00
✨ 优化数据库迁移脚本
This commit is contained in:
@@ -47,6 +47,9 @@ def migrate_old_data() -> None:
|
|||||||
TimeRemainingColumn(),
|
TimeRemainingColumn(),
|
||||||
) as progress,
|
) as progress,
|
||||||
):
|
):
|
||||||
|
if session.query(OldHistoricalData).count() == 0:
|
||||||
|
logger.info('空表, 跳过')
|
||||||
|
return
|
||||||
task_id = progress.add_task('[cyan]Migrating:', total=session.query(OldHistoricalData).count())
|
task_id = progress.add_task('[cyan]Migrating:', total=session.query(OldHistoricalData).count())
|
||||||
pointer = 0
|
pointer = 0
|
||||||
while pointer < session.query(OldHistoricalData).order_by(desc(OldHistoricalData.id)).limit(1).one().id:
|
while pointer < session.query(OldHistoricalData).order_by(desc(OldHistoricalData.id)).limit(1).one().id:
|
||||||
|
|||||||
@@ -28,12 +28,6 @@ depends_on: str | Sequence[str] | None = None
|
|||||||
def upgrade(name: str = '') -> None: # noqa: C901
|
def upgrade(name: str = '') -> None: # noqa: C901
|
||||||
if name:
|
if name:
|
||||||
return
|
return
|
||||||
from nonebot_plugin_tetris_stats.version import __version__
|
|
||||||
|
|
||||||
if __version__ != '1.0.3':
|
|
||||||
msg = '本迁移需要1.0.3版本, 请先锁定版本至1.0.3版本再执行本迁移'
|
|
||||||
logger.critical(msg)
|
|
||||||
raise RuntimeError(msg)
|
|
||||||
|
|
||||||
from nonebot.compat import PYDANTIC_V2, type_validate_json
|
from nonebot.compat import PYDANTIC_V2, type_validate_json
|
||||||
from pydantic import BaseModel, ValidationError
|
from pydantic import BaseModel, ValidationError
|
||||||
@@ -46,10 +40,6 @@ def upgrade(name: str = '') -> None: # noqa: C901
|
|||||||
TimeRemainingColumn,
|
TimeRemainingColumn,
|
||||||
)
|
)
|
||||||
|
|
||||||
from nonebot_plugin_tetris_stats.game_data_processor.schemas import ( # type: ignore[import-untyped]
|
|
||||||
BaseProcessedData,
|
|
||||||
)
|
|
||||||
|
|
||||||
Base = automap_base() # noqa: N806
|
Base = automap_base() # noqa: N806
|
||||||
Base.prepare(autoload_with=op.get_bind())
|
Base.prepare(autoload_with=op.get_bind())
|
||||||
HistoricalData = Base.classes.nonebot_plugin_tetris_stats_historicaldata # noqa: N806
|
HistoricalData = Base.classes.nonebot_plugin_tetris_stats_historicaldata # noqa: N806
|
||||||
@@ -62,18 +52,33 @@ def upgrade(name: str = '') -> None: # noqa: C901
|
|||||||
def model_to_json(value: BaseModel) -> str:
|
def model_to_json(value: BaseModel) -> str:
|
||||||
return value.json(by_alias=True)
|
return value.json(by_alias=True)
|
||||||
|
|
||||||
models = BaseProcessedData.__subclasses__()
|
|
||||||
|
|
||||||
def json_to_model(value: str) -> BaseModel:
|
|
||||||
for i in models:
|
|
||||||
try:
|
|
||||||
return type_validate_json(i, value)
|
|
||||||
except ValidationError: # noqa: PERF203
|
|
||||||
...
|
|
||||||
raise ValueError
|
|
||||||
|
|
||||||
with Session(op.get_bind()) as session:
|
with Session(op.get_bind()) as session:
|
||||||
count = session.query(HistoricalData).count()
|
count = session.query(HistoricalData).count()
|
||||||
|
if count == 0:
|
||||||
|
logger.info('空表, 跳过')
|
||||||
|
return
|
||||||
|
|
||||||
|
from nonebot_plugin_tetris_stats.version import __version__
|
||||||
|
|
||||||
|
if __version__ != '1.0.3':
|
||||||
|
msg = '本迁移需要1.0.3版本, 请先锁定版本至1.0.3版本再执行本迁移'
|
||||||
|
logger.critical(msg)
|
||||||
|
raise RuntimeError(msg)
|
||||||
|
|
||||||
|
from nonebot_plugin_tetris_stats.game_data_processor.schemas import ( # type: ignore[import-untyped]
|
||||||
|
BaseProcessedData,
|
||||||
|
)
|
||||||
|
|
||||||
|
models = BaseProcessedData.__subclasses__()
|
||||||
|
|
||||||
|
def json_to_model(value: str) -> BaseModel:
|
||||||
|
for i in models:
|
||||||
|
try:
|
||||||
|
return type_validate_json(i, value)
|
||||||
|
except ValidationError: # noqa: PERF203
|
||||||
|
...
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
with Progress(
|
with Progress(
|
||||||
TextColumn('[progress.description]{task.description}'),
|
TextColumn('[progress.description]{task.description}'),
|
||||||
BarColumn(),
|
BarColumn(),
|
||||||
|
|||||||
@@ -26,12 +26,7 @@ depends_on: str | Sequence[str] | None = None
|
|||||||
def upgrade(name: str = '') -> None:
|
def upgrade(name: str = '') -> None:
|
||||||
if name:
|
if name:
|
||||||
return
|
return
|
||||||
from nonebot_plugin_tetris_stats.version import __version__
|
|
||||||
|
|
||||||
if __version__ != '1.0.4':
|
|
||||||
msg = '本迁移需要1.0.4版本, 请先锁定版本至1.0.4版本再执行本迁移'
|
|
||||||
logger.critical(msg)
|
|
||||||
raise RuntimeError(msg)
|
|
||||||
from nonebot.compat import type_validate_json
|
from nonebot.compat import type_validate_json
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
from rich.progress import (
|
from rich.progress import (
|
||||||
@@ -46,8 +41,6 @@ def upgrade(name: str = '') -> None:
|
|||||||
from sqlalchemy.ext.automap import automap_base
|
from sqlalchemy.ext.automap import automap_base
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from nonebot_plugin_tetris_stats.game_data_processor.schemas import BaseUser # type: ignore[import-untyped]
|
|
||||||
|
|
||||||
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
|
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
|
||||||
batch_op.add_column(sa.Column('user_unique_identifier', sa.String(length=32), nullable=True))
|
batch_op.add_column(sa.Column('user_unique_identifier', sa.String(length=32), nullable=True))
|
||||||
batch_op.create_index(
|
batch_op.create_index(
|
||||||
@@ -60,37 +53,48 @@ def upgrade(name: str = '') -> None:
|
|||||||
Base.prepare(autoload_with=connection)
|
Base.prepare(autoload_with=connection)
|
||||||
HistoricalData = Base.classes.nonebot_plugin_tetris_stats_historicaldata # noqa: N806
|
HistoricalData = Base.classes.nonebot_plugin_tetris_stats_historicaldata # noqa: N806
|
||||||
|
|
||||||
models: list[type[BaseUser]] = BaseUser.__subclasses__()
|
|
||||||
|
|
||||||
def json_to_model(value: str) -> BaseUser:
|
|
||||||
for i in models:
|
|
||||||
try:
|
|
||||||
return type_validate_json(i, value)
|
|
||||||
except ValidationError: # noqa: PERF203
|
|
||||||
...
|
|
||||||
raise ValueError
|
|
||||||
|
|
||||||
with Session(op.get_bind()) as session:
|
with Session(op.get_bind()) as session:
|
||||||
count = session.query(HistoricalData).count()
|
count = session.query(HistoricalData).count()
|
||||||
with Progress(
|
if count == 0:
|
||||||
TextColumn('[progress.description]{task.description}'),
|
logger.info('空表, 跳过')
|
||||||
BarColumn(),
|
else:
|
||||||
MofNCompleteColumn(),
|
from nonebot_plugin_tetris_stats.version import __version__
|
||||||
TaskProgressColumn(),
|
|
||||||
TimeRemainingColumn(),
|
if __version__ != '1.0.4':
|
||||||
) as progress:
|
msg = '本迁移需要1.0.4版本, 请先锁定版本至1.0.4版本再执行本迁移'
|
||||||
task_id = progress.add_task('[cyan]Updateing:', total=count)
|
logger.critical(msg)
|
||||||
for i in range(0, count, 100):
|
raise RuntimeError(msg)
|
||||||
for j in session.scalars(
|
from nonebot_plugin_tetris_stats.game_data_processor.schemas import BaseUser # type: ignore[import-untyped]
|
||||||
select(HistoricalData).where(HistoricalData.id > i).order_by(HistoricalData.id).limit(100)
|
|
||||||
):
|
models: list[type[BaseUser]] = BaseUser.__subclasses__()
|
||||||
model = json_to_model(j.game_user)
|
|
||||||
|
def json_to_model(value: str) -> BaseUser:
|
||||||
|
for i in models:
|
||||||
try:
|
try:
|
||||||
j.user_unique_identifier = model.unique_identifier
|
return type_validate_json(i, value)
|
||||||
except ValueError:
|
except ValidationError: # noqa: PERF203
|
||||||
session.delete(j)
|
...
|
||||||
progress.update(task_id, advance=1)
|
raise ValueError
|
||||||
session.commit()
|
|
||||||
|
with Progress(
|
||||||
|
TextColumn('[progress.description]{task.description}'),
|
||||||
|
BarColumn(),
|
||||||
|
MofNCompleteColumn(),
|
||||||
|
TaskProgressColumn(),
|
||||||
|
TimeRemainingColumn(),
|
||||||
|
) as progress:
|
||||||
|
task_id = progress.add_task('[cyan]Updateing:', total=count)
|
||||||
|
for i in range(0, count, 100):
|
||||||
|
for j in session.scalars(
|
||||||
|
select(HistoricalData).where(HistoricalData.id > i).order_by(HistoricalData.id).limit(100)
|
||||||
|
):
|
||||||
|
model = json_to_model(j.game_user)
|
||||||
|
try:
|
||||||
|
j.user_unique_identifier = model.unique_identifier
|
||||||
|
except ValueError:
|
||||||
|
session.delete(j)
|
||||||
|
progress.update(task_id, advance=1)
|
||||||
|
session.commit()
|
||||||
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
|
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
|
||||||
batch_op.alter_column('user_unique_identifier', existing_type=sa.VARCHAR(length=32), nullable=False)
|
batch_op.alter_column('user_unique_identifier', existing_type=sa.VARCHAR(length=32), nullable=False)
|
||||||
logger.success('database upgrade success')
|
logger.success('database upgrade success')
|
||||||
|
|||||||
Reference in New Issue
Block a user