This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import engine_from_config
|
||||
from alembic import context
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from src.database import Base, DATABASE_URL
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
url = context.config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=None,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,22 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Initial schema
|
||||
|
||||
Revision ID: 001
|
||||
Revises:
|
||||
Create Date: 2026-10-12
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '001'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('username', sa.String(length=32), nullable=False),
|
||||
sa.Column('display_name', sa.String(length=100), nullable=False),
|
||||
sa.Column('password_hash', sa.String(length=255), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_users_username', 'users', ['username'], unique=True)
|
||||
|
||||
op.create_table('sessions',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('token', sa.String(length=255), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_sessions_token', 'sessions', ['token'], unique=True)
|
||||
|
||||
op.create_table('chats',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('title', sa.String(length=100), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('chat_members',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('messages',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('sender_id', sa.String(), nullable=False),
|
||||
sa.Column('text', sa.Text(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['sender_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.drop_table('messages')
|
||||
op.drop_table('chat_members')
|
||||
op.drop_table('chats')
|
||||
op.drop_index('ix_sessions_token', table_name='sessions')
|
||||
op.drop_table('sessions')
|
||||
op.drop_index('ix_users_username', table_name='users')
|
||||
op.drop_table('users')
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Add membership uniqueness and the keyset history index.
|
||||
|
||||
Revision ID: 002
|
||||
Revises: 001
|
||||
Create Date: 2026-10-14
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '002'
|
||||
down_revision = '001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Older schemas allowed nullable timestamps. Backfill before tightening the
|
||||
# constraint so the migration also works on an existing populated database.
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.execute(sa.text(f'UPDATE {table} SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL'))
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=False)
|
||||
|
||||
op.create_unique_constraint('uq_chat_member', 'chat_members', ['chat_id', 'user_id'])
|
||||
op.create_index('ix_chat_members_user_chat', 'chat_members', ['user_id', 'chat_id'])
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created_id', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
op.drop_index('ix_chat_members_user_chat', table_name='chat_members')
|
||||
op.drop_constraint('uq_chat_member', 'chat_members', type_='unique')
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=True)
|
||||
Reference in New Issue
Block a user