model channels
This commit is contained in:
45
alembic/versions/7506a3320699_channel_table.py
Normal file
45
alembic/versions/7506a3320699_channel_table.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""channel table
|
||||
|
||||
Revision ID: 7506a3320699
|
||||
Revises: 69ef23ef1ed1
|
||||
Create Date: 2025-09-05 14:12:37.430983
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import table, column
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '7506a3320699'
|
||||
down_revision: Union[str, Sequence[str], None] = '69ef23ef1ed1'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# Для SQLite: создаём новую таблицу, копируем данные, удаляем старую, переименовываем новую
|
||||
conn = op.get_bind()
|
||||
# 1. Создать новую таблицу
|
||||
op.create_table(
|
||||
'channel_new',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('admin_id', sa.Integer(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
# 2. Если старая таблица есть, скопировать данные
|
||||
result = conn.execute(sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name='channel'")).fetchone()
|
||||
if result:
|
||||
conn.execute(sa.text("INSERT INTO channel_new (id, name, admin_id) SELECT id, name, admin_id FROM channel"))
|
||||
op.drop_table('channel')
|
||||
# 3. Переименовать новую таблицу
|
||||
conn.execute(sa.text("ALTER TABLE channel_new RENAME TO channel"))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
op.drop_table('channel')
|
||||
Reference in New Issue
Block a user