Skip to content

Commit 4529ba7

Browse files
authored
Migrations fix
1 parent c3318ae commit 4529ba7

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

migrations/versions/6a486347af93_order.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,28 @@
1717

1818

1919
def upgrade():
20-
op.add_column('button', sa.Column('order', sa.Integer(), nullable=False))
21-
op.add_column('button', sa.Column('link', sa.String(), nullable=False))
22-
op.add_column('button', sa.Column('type', sa.String(), nullable=False))
20+
op.add_column('button', sa.Column('order', sa.Integer(), nullable=True))
21+
op.add_column('button', sa.Column('link', sa.String(), nullable=True))
22+
op.add_column('button', sa.Column('type', sa.String(), nullable=True))
23+
conn = op.get_bind()
24+
res = conn.execute(sa.text("select id from button")).fetchall()
25+
for i in range(0, len(res)):
26+
conn.execute(sa.text(f'UPDATE button set order = {i+1}, link = "a", type = "b", where id={res[i]}'))
27+
op.alter_column('button', 'order', nullable=False)
28+
op.alter_column('button', 'link', nullable=False)
29+
op.alter_column('button', 'type', nullable=False)
2330
op.alter_column('button', 'name', existing_type=sa.VARCHAR(), nullable=False)
2431
op.alter_column('button', 'category_id', existing_type=sa.INTEGER(), nullable=False)
2532
op.alter_column('button', 'icon', existing_type=sa.VARCHAR(), nullable=False)
26-
op.add_column('category', sa.Column('order', sa.Integer(), nullable=False))
33+
op.add_column('category', sa.Column('order', sa.Integer(), nullable=True))
34+
conn = op.get_bind()
35+
res = conn.execute(sa.text("select id from category")).fetchall()
36+
for i in range(0, len(res)):
37+
conn.execute(sa.text(f'select order from category where category.id={res[i]} update category set order = {i+1}'))
38+
op.alter_column('category', 'order', nullable=False)
2739
op.alter_column('category', 'name', existing_type=sa.VARCHAR(), nullable=False)
2840
op.alter_column('category', 'type', existing_type=sa.VARCHAR(), nullable=False)
41+
op.alter_column('button', 'order', nullable=False)
2942

3043

3144
def downgrade():

0 commit comments

Comments
 (0)