Skip to content

Commit c1a567f

Browse files
♻️ 重构历史迁移脚本,修复pgsql 63字段长度限制
1 parent 3609205 commit c1a567f

6 files changed

+86
-69
lines changed

nonebot_plugin_tetris_stats/config/migrations/3c25a5a8c050_refactor_historical.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from rich.progress import BarColumn, MofNCompleteColumn, Progress, TaskProgressColumn, TextColumn, TimeRemainingColumn
1818
from sqlalchemy import desc, select
1919
from sqlalchemy.dialects import sqlite
20+
from sqlalchemy.exc import IdentifierError
2021
from sqlalchemy.ext.automap import automap_base
2122
from sqlalchemy.orm import Session
2223

@@ -205,12 +206,16 @@ def upgrade(name: str = '') -> None:
205206

206207
migrate_old_data()
207208

208-
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
209-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_command_type')
210-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_game_platform')
211-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_source_account')
212-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_source_type')
213-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_user_unique_identifier')
209+
try:
210+
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
211+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_command_type')
212+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_game_platform')
213+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_source_account')
214+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_source_type')
215+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_user_unique_identifier')
216+
except IdentifierError:
217+
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
218+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_historicaldata_user_uniq_5301')
214219

215220
op.drop_table('nonebot_plugin_tetris_stats_historicaldata')
216221
# ### end Alembic commands ###
@@ -222,40 +227,48 @@ def downgrade(name: str = '') -> None:
222227
# ### commands auto generated by Alembic - please adjust! ###
223228
op.create_table(
224229
'nonebot_plugin_tetris_stats_historicaldata',
225-
sa.Column('id', sa.INTEGER(), nullable=False),
226-
sa.Column('trigger_time', sa.DATETIME(), nullable=False),
227-
sa.Column('bot_platform', sa.VARCHAR(length=32), nullable=True),
228-
sa.Column('bot_account', sa.VARCHAR(), nullable=True),
229-
sa.Column('source_type', sa.VARCHAR(length=32), nullable=True),
230-
sa.Column('source_account', sa.VARCHAR(), nullable=True),
231-
sa.Column('message', sa.BLOB(), nullable=True),
232-
sa.Column('game_platform', sa.VARCHAR(length=32), nullable=False),
233-
sa.Column('command_type', sa.VARCHAR(length=16), nullable=False),
230+
sa.Column('id', sa.Integer(), nullable=False),
231+
sa.Column('trigger_time', sa.DateTime(), nullable=False),
232+
sa.Column('bot_platform', sa.String(length=32), nullable=True),
233+
sa.Column('bot_account', sa.String(), nullable=True),
234+
sa.Column('source_type', sa.String(length=32), nullable=True),
235+
sa.Column('source_account', sa.String(), nullable=True),
236+
sa.Column('message', sa.PickleType(), nullable=True),
237+
sa.Column('game_platform', sa.String(length=32), nullable=False),
238+
sa.Column('command_type', sa.String(length=16), nullable=False),
234239
sa.Column('command_args', sqlite.JSON(), nullable=False),
235240
sa.Column('game_user', sqlite.JSON(), nullable=False),
236241
sa.Column('processed_data', sqlite.JSON(), nullable=False),
237-
sa.Column('finish_time', sa.DATETIME(), nullable=False),
238-
sa.Column('user_unique_identifier', sa.VARCHAR(length=32), nullable=False),
242+
sa.Column('finish_time', sa.DateTime(), nullable=False),
243+
sa.Column('user_unique_identifier', sa.String(length=32), nullable=False),
239244
sa.PrimaryKeyConstraint('id', name='pk_nonebot_plugin_tetris_stats_historicaldata'),
240245
)
241-
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
242-
batch_op.create_index(
243-
'ix_nonebot_plugin_tetris_stats_historicaldata_user_unique_identifier',
244-
['user_unique_identifier'],
245-
unique=False,
246-
)
247-
batch_op.create_index(
248-
'ix_nonebot_plugin_tetris_stats_historicaldata_source_type', ['source_type'], unique=False
249-
)
250-
batch_op.create_index(
251-
'ix_nonebot_plugin_tetris_stats_historicaldata_source_account', ['source_account'], unique=False
252-
)
253-
batch_op.create_index(
254-
'ix_nonebot_plugin_tetris_stats_historicaldata_game_platform', ['game_platform'], unique=False
255-
)
256-
batch_op.create_index(
257-
'ix_nonebot_plugin_tetris_stats_historicaldata_command_type', ['command_type'], unique=False
258-
)
246+
try:
247+
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
248+
batch_op.create_index(
249+
'ix_nonebot_plugin_tetris_stats_historicaldata_source_type', ['source_type'], unique=False
250+
)
251+
batch_op.create_index(
252+
'ix_nonebot_plugin_tetris_stats_historicaldata_source_account', ['source_account'], unique=False
253+
)
254+
batch_op.create_index(
255+
'ix_nonebot_plugin_tetris_stats_historicaldata_game_platform', ['game_platform'], unique=False
256+
)
257+
batch_op.create_index(
258+
'ix_nonebot_plugin_tetris_stats_historicaldata_command_type', ['command_type'], unique=False
259+
)
260+
batch_op.create_index(
261+
'ix_nonebot_plugin_tetris_stats_historicaldata_user_unique_identifier',
262+
['user_unique_identifier'],
263+
unique=False,
264+
)
265+
except IdentifierError:
266+
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
267+
batch_op.create_index(
268+
'ix_nonebot_plugin_tetris_stats_historicaldata_user_uniq_5301',
269+
['user_unique_identifier'],
270+
unique=False,
271+
)
259272

260273
with op.batch_alter_table('nonebot_plugin_tetris_stats_triggerhistoricaldata', schema=None) as batch_op:
261274
batch_op.drop_index(batch_op.f('ix_nonebot_plugin_tetris_stats_triggerhistoricaldata_game_platform'))

nonebot_plugin_tetris_stats/config/migrations/9f6582279ce2_recreate_historical_data.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ def downgrade(name: str = '') -> None:
8282

8383
op.create_table(
8484
'nonebot_plugin_tetris_stats_historicaldata',
85-
sa.Column('id', sa.INTEGER(), nullable=False),
86-
sa.Column('trigger_time', sa.DATETIME(), nullable=False),
87-
sa.Column('bot_platform', sa.VARCHAR(length=32), nullable=True),
88-
sa.Column('bot_account', sa.VARCHAR(), nullable=True),
89-
sa.Column('source_type', sa.VARCHAR(length=32), nullable=True),
90-
sa.Column('source_account', sa.VARCHAR(), nullable=True),
91-
sa.Column('message', sa.BLOB(), nullable=True),
92-
sa.Column('game_platform', sa.VARCHAR(length=32), nullable=False),
93-
sa.Column('command_type', sa.VARCHAR(length=16), nullable=False),
85+
sa.Column('id', sa.Integer(), nullable=False),
86+
sa.Column('trigger_time', sa.DateTime(), nullable=False),
87+
sa.Column('bot_platform', sa.String(length=32), nullable=True),
88+
sa.Column('bot_account', sa.String(), nullable=True),
89+
sa.Column('source_type', sa.String(length=32), nullable=True),
90+
sa.Column('source_account', sa.String(), nullable=True),
91+
sa.Column('message', sa.PickleType(), nullable=True),
92+
sa.Column('game_platform', sa.String(length=32), nullable=False),
93+
sa.Column('command_type', sa.String(length=16), nullable=False),
9494
sa.Column('command_args', sqlite.JSON(), nullable=False),
95-
sa.Column('game_user', sa.BLOB(), nullable=False),
96-
sa.Column('processed_data', sa.BLOB(), nullable=False),
97-
sa.Column('finish_time', sa.DATETIME(), nullable=False),
95+
sa.Column('game_user', sa.PickleType(), nullable=False),
96+
sa.Column('processed_data', sa.PickleType(), nullable=False),
97+
sa.Column('finish_time', sa.DateTime(), nullable=False),
9898
sa.PrimaryKeyConstraint('id', name='pk_nonebot_plugin_tetris_stats_historicaldata'),
9999
)
100100
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:

nonebot_plugin_tetris_stats/config/migrations/b15844837693_migrate_to_nonobot_plugin_user.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ def downgrade(name: str = '') -> None:
5757

5858
op.create_table(
5959
'nonebot_plugin_tetris_stats_bind',
60-
sa.Column('id', sa.INTEGER(), nullable=False),
61-
sa.Column('chat_platform', sa.VARCHAR(length=32), nullable=False),
62-
sa.Column('chat_account', sa.VARCHAR(), nullable=False),
63-
sa.Column('game_platform', sa.VARCHAR(length=32), nullable=False),
64-
sa.Column('game_account', sa.VARCHAR(), nullable=False),
60+
sa.Column('id', sa.Integer(), nullable=False),
61+
sa.Column('chat_platform', sa.String(length=32), nullable=False),
62+
sa.Column('chat_account', sa.String(), nullable=False),
63+
sa.Column('game_platform', sa.String(length=32), nullable=False),
64+
sa.Column('game_account', sa.String(), nullable=False),
6565
sa.PrimaryKeyConstraint('id', name='pk_nonebot_plugin_tetris_stats_bind'),
6666
)
6767
with op.batch_alter_table('nonebot_plugin_tetris_stats_bind', schema=None) as batch_op:

nonebot_plugin_tetris_stats/config/migrations/b7fbdafc339a_add_user_unique_identifier_field_to_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def json_to_model(value: str) -> BaseUser:
9696
progress.update(task_id, advance=1)
9797
session.commit()
9898
with op.batch_alter_table('nonebot_plugin_tetris_stats_historicaldata', schema=None) as batch_op:
99-
batch_op.alter_column('user_unique_identifier', existing_type=sa.VARCHAR(length=32), nullable=False)
99+
batch_op.alter_column('user_unique_identifier', existing_type=sa.String(length=32), nullable=False)
100100
logger.success('database upgrade success')
101101

102102

nonebot_plugin_tetris_stats/config/migrations/cfeab6961dce_extend_api_type_field_length.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def upgrade(name: str = '') -> None:
2929
# ### commands auto generated by Alembic - please adjust! ###
3030
with op.batch_alter_table('nonebot_plugin_tetris_stats_tetriohistoricaldata', schema=None) as batch_op:
3131
batch_op.alter_column(
32-
'api_type', existing_type=sa.VARCHAR(length=16), type_=sa.String(length=32), existing_nullable=False
32+
'api_type', existing_type=sa.String(length=16), type_=sa.String(length=32), existing_nullable=False
3333
)
3434

3535
# ### end Alembic commands ###
@@ -44,7 +44,7 @@ def downgrade(name: str = '') -> None:
4444
input('如果确认可以迁移, 请按回车键继续!')
4545
with op.batch_alter_table('nonebot_plugin_tetris_stats_tetriohistoricaldata', schema=None) as batch_op:
4646
batch_op.alter_column(
47-
'api_type', existing_type=sa.String(length=32), type_=sa.VARCHAR(length=16), existing_nullable=False
47+
'api_type', existing_type=sa.String(length=32), type_=sa.String(length=16), existing_nullable=False
4848
)
4949

5050
# ### end Alembic commands ###

nonebot_plugin_tetris_stats/config/migrations/f5b4a6d1325b_tetr_io_new_season.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import sqlalchemy as sa
1414
from alembic import op
15+
from sqlalchemy.exc import IdentifierError
1516

1617
if TYPE_CHECKING:
1718
from collections.abc import Sequence
@@ -31,11 +32,14 @@ def upgrade(name: str = '') -> None:
3132
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_iorank_update_time')
3233

3334
op.drop_table('nonebot_plugin_tetris_stats_iorank')
34-
35-
with op.batch_alter_table('nonebot_plugin_tetris_stats_tetriohistoricaldata', schema=None) as batch_op:
36-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_api_type')
37-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_update_time')
38-
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_user_unique_identifier')
35+
try:
36+
with op.batch_alter_table('nonebot_plugin_tetris_stats_tetriohistoricaldata', schema=None) as batch_op:
37+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_api_type')
38+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_update_time')
39+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_user_unique_identifier')
40+
except IdentifierError:
41+
with op.batch_alter_table('nonebot_plugin_tetris_stats_tetriohistoricaldata', schema=None) as batch_op:
42+
batch_op.drop_index('ix_nonebot_plugin_tetris_stats_tetriohistoricaldata_use_4888')
3943

4044
op.drop_table('nonebot_plugin_tetris_stats_tetriohistoricaldata')
4145

@@ -68,21 +72,21 @@ def downgrade(name: str = '') -> None:
6872
return
6973
op.create_table(
7074
'nonebot_plugin_tetris_stats_iorank',
71-
sa.Column('id', sa.INTEGER(), nullable=False),
72-
sa.Column('rank', sa.VARCHAR(length=2), nullable=False),
73-
sa.Column('tr_line', sa.FLOAT(), nullable=False),
74-
sa.Column('player_count', sa.INTEGER(), nullable=False),
75+
sa.Column('id', sa.Integer(), nullable=False),
76+
sa.Column('rank', sa.String(length=2), nullable=False),
77+
sa.Column('tr_line', sa.Float(), nullable=False),
78+
sa.Column('player_count', sa.Integer(), nullable=False),
7579
sa.Column('low_pps', sa.JSON(), nullable=False),
7680
sa.Column('low_apm', sa.JSON(), nullable=False),
7781
sa.Column('low_vs', sa.JSON(), nullable=False),
78-
sa.Column('avg_pps', sa.FLOAT(), nullable=False),
79-
sa.Column('avg_apm', sa.FLOAT(), nullable=False),
80-
sa.Column('avg_vs', sa.FLOAT(), nullable=False),
82+
sa.Column('avg_pps', sa.Float(), nullable=False),
83+
sa.Column('avg_apm', sa.Float(), nullable=False),
84+
sa.Column('avg_vs', sa.Float(), nullable=False),
8185
sa.Column('high_pps', sa.JSON(), nullable=False),
8286
sa.Column('high_apm', sa.JSON(), nullable=False),
8387
sa.Column('high_vs', sa.JSON(), nullable=False),
84-
sa.Column('update_time', sa.DATETIME(), nullable=False),
85-
sa.Column('file_hash', sa.VARCHAR(length=128), nullable=True),
88+
sa.Column('update_time', sa.DateTime(), nullable=False),
89+
sa.Column('file_hash', sa.String(length=128), nullable=True),
8690
sa.PrimaryKeyConstraint('id', name='pk_nonebot_plugin_tetris_stats_iorank'),
8791
)
8892
with op.batch_alter_table('nonebot_plugin_tetris_stats_iorank', schema=None) as batch_op:

0 commit comments

Comments
 (0)