better migrations
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
42
migrations/versions/add_events_table.py
Normal file
42
migrations/versions/add_events_table.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""create events table
|
||||
|
||||
Revision ID: add_events_table
|
||||
Revises: f18735338888
|
||||
Create Date: 2024-03-19 10:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'add_events_table'
|
||||
down_revision = 'f18735338888'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if 'events' not in tables:
|
||||
op.create_table(
|
||||
'events',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('event_type', sa.String(50), nullable=False),
|
||||
sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id'), nullable=True),
|
||||
sa.Column('timestamp', sa.DateTime, nullable=False),
|
||||
sa.Column('details', sa.JSON),
|
||||
sa.Column('ip_address', sa.String(45)),
|
||||
sa.Column('user_agent', sa.String(255)),
|
||||
)
|
||||
op.create_index('idx_events_event_type', 'events', ['event_type'])
|
||||
op.create_index('idx_events_timestamp', 'events', ['timestamp'])
|
||||
op.create_index('idx_events_user_id', 'events', ['user_id'])
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('idx_events_event_type', table_name='events')
|
||||
op.drop_index('idx_events_timestamp', table_name='events')
|
||||
op.drop_index('idx_events_user_id', table_name='events')
|
||||
op.drop_table('events')
|
||||
46
migrations/versions/add_notifs_table.py
Normal file
46
migrations/versions/add_notifs_table.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""create notifs table
|
||||
|
||||
Revision ID: add_notifs_table
|
||||
Revises: add_events_table
|
||||
Create Date: 2024-03-19 10:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'add_notifs_table'
|
||||
down_revision = 'add_events_table'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if 'notifs' not in tables:
|
||||
op.create_table(
|
||||
'notifs',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('notif_type', sa.String(50), nullable=False),
|
||||
sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id'), nullable=False),
|
||||
sa.Column('sender_id', sa.Integer, sa.ForeignKey('user.id'), nullable=True),
|
||||
sa.Column('timestamp', sa.DateTime, nullable=False),
|
||||
sa.Column('read', sa.Boolean, nullable=False, default=False),
|
||||
sa.Column('details', sa.JSON),
|
||||
)
|
||||
op.create_index('idx_notifs_notif_type', 'notifs', ['notif_type'])
|
||||
op.create_index('idx_notifs_timestamp', 'notifs', ['timestamp'])
|
||||
op.create_index('idx_notifs_user_id', 'notifs', ['user_id'])
|
||||
op.create_index('idx_notifs_sender_id', 'notifs', ['sender_id'])
|
||||
op.create_index('idx_notifs_read', 'notifs', ['read'])
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('idx_notifs_notif_type', table_name='notifs')
|
||||
op.drop_index('idx_notifs_timestamp', table_name='notifs')
|
||||
op.drop_index('idx_notifs_user_id', table_name='notifs')
|
||||
op.drop_index('idx_notifs_sender_id', table_name='notifs')
|
||||
op.drop_index('idx_notifs_read', table_name='notifs')
|
||||
op.drop_table('notifs')
|
||||
27
migrations/versions/make_events_user_id_nullable.py
Normal file
27
migrations/versions/make_events_user_id_nullable.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""make events user_id nullable
|
||||
|
||||
Revision ID: make_events_user_id_nullable
|
||||
Revises: f18735338888
|
||||
Create Date: 2024-03-19 10:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'make_events_user_id_nullable'
|
||||
down_revision = 'f18735338888' # This should be the latest migration
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
# Make user_id nullable in events table
|
||||
op.alter_column('events', 'user_id',
|
||||
existing_type=sa.Integer(),
|
||||
nullable=True)
|
||||
|
||||
def downgrade():
|
||||
# Make user_id non-nullable again
|
||||
op.alter_column('events', 'user_id',
|
||||
existing_type=sa.Integer(),
|
||||
nullable=False)
|
||||
Reference in New Issue
Block a user