|
| 1 | +"""Migrate to uninfo |
| 2 | +
|
| 3 | +迁移 ID: 766cc7e75a62 |
| 4 | +父迁移: 612d8b00d9ac |
| 5 | +创建时间: 2025-05-26 04:51:54.665200 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import math |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +from alembic import op |
| 15 | +from nonebot.log import logger |
| 16 | +from rich.progress import BarColumn, Progress, SpinnerColumn, TaskProgressColumn, TextColumn |
| 17 | +from sqlalchemy import inspect |
| 18 | +from sqlalchemy.ext.automap import automap_base |
| 19 | +from sqlalchemy.orm import Session |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from collections.abc import Sequence |
| 23 | + |
| 24 | +revision: str = '766cc7e75a62' |
| 25 | +down_revision: str | Sequence[str] | None = '612d8b00d9ac' |
| 26 | +branch_labels: str | Sequence[str] | None = None |
| 27 | +depends_on: str | Sequence[str] | None = None |
| 28 | + |
| 29 | + |
| 30 | +def data_migrate() -> None: |
| 31 | + conn = op.get_bind() |
| 32 | + insp = inspect(conn) |
| 33 | + table_names = insp.get_table_names() |
| 34 | + if 'nonebot_plugin_tetris_stats_triggerhistoricaldata' not in table_names: |
| 35 | + return |
| 36 | + |
| 37 | + Base = automap_base() # noqa: N806 |
| 38 | + Base.prepare(autoload_with=conn) |
| 39 | + TriggerHistoricalData = Base.classes.nonebot_plugin_tetris_stats_triggerhistoricaldata # noqa: N806 |
| 40 | + TriggerHistoricalDataV2 = Base.classes.nonebot_plugin_tetris_stats_triggerhistoricaldatav2 # noqa: N806 |
| 41 | + |
| 42 | + with Session(conn) as db_session: |
| 43 | + count = db_session.query(TriggerHistoricalData).count() |
| 44 | + if count == 0: |
| 45 | + return |
| 46 | + |
| 47 | + try: |
| 48 | + from nonebot_session_to_uninfo import check_tables, get_id_map # type: ignore[import-untyped] |
| 49 | + except ImportError as err: |
| 50 | + msg = '请安装 `nonebot-session-to-uninfo` 以迁移数据' |
| 51 | + raise ValueError(msg) from err |
| 52 | + |
| 53 | + check_tables() |
| 54 | + |
| 55 | + migration_limit = 10000 # 每次迁移的数据量为 10000 条 |
| 56 | + last_id = -1 |
| 57 | + id_map: dict[int, int] = {} |
| 58 | + |
| 59 | + logger.warning('tetris_stats: 正在迁移数据, 请不要关闭程序...') |
| 60 | + |
| 61 | + with Progress( |
| 62 | + SpinnerColumn(), |
| 63 | + TextColumn('[progress.description]{task.description}'), |
| 64 | + BarColumn(), |
| 65 | + TaskProgressColumn(), |
| 66 | + ) as progress: |
| 67 | + task = progress.add_task('迁移数据...', total=count) |
| 68 | + |
| 69 | + for _ in range(math.ceil(count / migration_limit)): |
| 70 | + records = ( |
| 71 | + db_session.query(TriggerHistoricalData) |
| 72 | + .order_by(TriggerHistoricalData.id) |
| 73 | + .where(TriggerHistoricalData.id > last_id) |
| 74 | + .limit(migration_limit) |
| 75 | + .all() |
| 76 | + ) |
| 77 | + last_id = records[-1].id |
| 78 | + |
| 79 | + session_ids = [ |
| 80 | + record.session_persist_id for record in records if record.session_persist_id not in id_map |
| 81 | + ] |
| 82 | + if session_ids: |
| 83 | + id_map.update(get_id_map(session_ids)) |
| 84 | + |
| 85 | + db_session.add_all( |
| 86 | + TriggerHistoricalDataV2( |
| 87 | + id=record.id, |
| 88 | + session_persist_id=id_map[record.session_persist_id], |
| 89 | + trigger_time=record.trigger_time, |
| 90 | + game_platform=record.game_platform, |
| 91 | + command_type=record.command_type, |
| 92 | + command_args=record.command_args, |
| 93 | + finish_time=record.finish_time, |
| 94 | + ) |
| 95 | + for record in records |
| 96 | + ) |
| 97 | + |
| 98 | + progress.update(task, advance=len(records)) |
| 99 | + |
| 100 | + db_session.commit() |
| 101 | + |
| 102 | + logger.success('tetris_stats: 数据迁移完成!') |
| 103 | + |
| 104 | + |
| 105 | +def upgrade(name: str = '') -> None: |
| 106 | + if name: |
| 107 | + return |
| 108 | + data_migrate() |
| 109 | + |
| 110 | + |
| 111 | +def downgrade(name: str = '') -> None: |
| 112 | + if name: |
| 113 | + return |
0 commit comments