Merge pull request 'migration fix' (#6) from v2 into main
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #6
This commit is contained in:
@@ -25,21 +25,22 @@ def upgrade() -> None:
|
|||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
# 1. Создать новую таблицу
|
# 1. Создать новую таблицу
|
||||||
op.create_table(
|
op.create_table(
|
||||||
'channel_new',
|
'channels_new',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('name', sa.String(length=255), nullable=False),
|
sa.Column('name', sa.String(length=255), nullable=True),
|
||||||
|
sa.Column('link', sa.String(length=255), nullable=True),
|
||||||
sa.Column('admin_id', sa.Integer(), nullable=True),
|
sa.Column('admin_id', sa.Integer(), nullable=True),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
# 2. Если старая таблица есть, скопировать данные
|
# 2. Если старая таблица есть, скопировать данные
|
||||||
result = conn.execute(sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name='channel'")).fetchone()
|
result = conn.execute(sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name='channels'")).fetchone()
|
||||||
if result:
|
if result:
|
||||||
conn.execute(sa.text("INSERT INTO channel_new (id, name, admin_id) SELECT id, name, admin_id FROM channel"))
|
conn.execute(sa.text("INSERT INTO channels_new (id, name, link, admin_id) SELECT id, name, link, admin_id FROM channels"))
|
||||||
op.drop_table('channel')
|
op.drop_table('channels')
|
||||||
# 3. Переименовать новую таблицу
|
# 3. Переименовать новую таблицу
|
||||||
conn.execute(sa.text("ALTER TABLE channel_new RENAME TO channel"))
|
conn.execute(sa.text("ALTER TABLE channels_new RENAME TO channels"))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
"""Downgrade schema."""
|
"""Downgrade schema."""
|
||||||
op.drop_table('channel')
|
op.drop_table('channels')
|
||||||
|
|||||||
Reference in New Issue
Block a user