1111from collections .abc import Sequence
1212
1313from alembic import op
14+ from nonebot import logger
15+ from sqlalchemy .exc import IdentifierError
1416
1517revision : str = "9492159f98f7"
1618down_revision : str | Sequence [str ] | None = "ac57f7074e58"
@@ -22,35 +24,28 @@ def upgrade(name: str = "") -> None:
2224 if name :
2325 return
2426 # ### commands auto generated by Alembic - please adjust! ###
25- with op .batch_alter_table ("nonebot_plugin_user_bind" , schema = None ) as batch_op :
26- batch_op .drop_constraint (
27- "fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user" ,
28- type_ = "foreignkey" ,
29- )
30- batch_op .drop_constraint (
31- "fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user" ,
32- type_ = "foreignkey" ,
33- )
3427
28+ # 尝试删除外键约束
29+ # 老的数据库中还有约束,但是新的没有,所以这里可能会报错
30+ try :
31+ with op .batch_alter_table ("nonebot_plugin_user_bind" , schema = None ) as batch_op :
32+ batch_op .drop_constraint (
33+ "fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user" ,
34+ type_ = "foreignkey" ,
35+ )
36+ batch_op .drop_constraint (
37+ "fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user" ,
38+ type_ = "foreignkey" ,
39+ )
40+ logger .info ("[user] 已成功删除外键约束" )
41+ except (ValueError , IdentifierError ):
42+ logger .debug ("[user] 未找到外键约束,跳过删除" )
3543 # ### end Alembic commands ###
3644
3745
3846def downgrade (name : str = "" ) -> None :
3947 if name :
4048 return
4149 # ### commands auto generated by Alembic - please adjust! ###
42- with op .batch_alter_table ("nonebot_plugin_user_bind" , schema = None ) as batch_op :
43- batch_op .create_foreign_key (
44- "fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user" ,
45- "nonebot_plugin_user_user" ,
46- ["bind_id" ],
47- ["id" ],
48- )
49- batch_op .create_foreign_key (
50- "fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user" ,
51- "nonebot_plugin_user_user" ,
52- ["original_id" ],
53- ["id" ],
54- )
5550
5651 # ### end Alembic commands ###
0 commit comments