Compare commits
2 Commits
fb620e5d38
...
c6574efee3
| Author | SHA1 | Date | |
|---|---|---|---|
| c6574efee3 | |||
| a23ca87c84 |
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')
|
||||
@@ -10,9 +10,9 @@ class Admin(Base):
|
||||
class Channel(Base):
|
||||
__tablename__ = 'channels'
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String, nullable=False)
|
||||
link = Column(String, nullable=False)
|
||||
buttons = relationship('Button', back_populates='channel')
|
||||
name = Column(String)
|
||||
link = Column(String)
|
||||
admin_id = Column(Integer, ForeignKey('admins.id')) # если есть таблица admins
|
||||
|
||||
class Group(Base):
|
||||
__tablename__ = 'groups'
|
||||
|
||||
Reference in New Issue
Block a user