91 lines
No EOL
3.5 KiB
Python
91 lines
No EOL
3.5 KiB
Python
"""Bullet journal migration with user relationship
|
|
|
|
Revision ID: 001_bullet_journal
|
|
Revises:
|
|
Create Date: 2026-07-22
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '001_bullet_journal'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
# Enum strings for SQLite (no native enum support)
|
|
SLEEP_QUALITY = ['poor', 'fair', 'good', 'excellent']
|
|
ITEM_TYPE = ['bullet', 'gratitude', 'sleep', 'note', 'task']
|
|
STATUS_TYPE = ['todo', 'doing', 'done', 'cancelled']
|
|
|
|
|
|
def upgrade():
|
|
# Create users table first (foreign key dependency)
|
|
op.create_table('user',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('username', sa.String(length=64), nullable=False),
|
|
sa.Column('password_hash', sa.String(length=128), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('username')
|
|
)
|
|
|
|
# Create user_settings table
|
|
op.create_table('user_settings',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
|
sa.Column('theme', sa.String(length=10), nullable=True),
|
|
sa.Column('font_size', sa.Integer(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id')
|
|
)
|
|
|
|
# Create daily_log table with user_id foreign key
|
|
op.create_table('daily_log',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('date', sa.Date(), nullable=False),
|
|
sa.Column('prompt', sa.Text(), nullable=True),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.Column('gratitude', sa.JSON(), nullable=True),
|
|
sa.Column('bedtime', sa.String(length=20), nullable=True),
|
|
sa.Column('wake_time', sa.String(length=20), nullable=True),
|
|
sa.Column('sleep_quality', sa.String(length=10), nullable=True),
|
|
sa.Column('mood', sa.String(length=50), nullable=True),
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('date')
|
|
)
|
|
|
|
# Create daily_log_item table
|
|
op.create_table('daily_log_item',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('log_id', sa.Integer(), nullable=False),
|
|
sa.Column('type', sa.String(length=20), nullable=False),
|
|
sa.Column('symbol', sa.String(length=10), nullable=True),
|
|
sa.Column('text', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('status', sa.String(length=10), nullable=True),
|
|
sa.ForeignKeyConstraint(['log_id'], ['daily_log.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
|
|
# Create indexes
|
|
op.create_index('ix_log_id', 'daily_log_item', ['log_id'])
|
|
op.create_index('ix_log_id_status', 'daily_log_item', ['log_id', 'status'])
|
|
op.create_index('ix_type', 'daily_log_item', ['type'])
|
|
|
|
|
|
def downgrade():
|
|
op.drop_index('ix_type', table_name='daily_log_item')
|
|
op.drop_index('ix_log_id_status', table_name='daily_log_item')
|
|
op.drop_index('ix_log_id', table_name='daily_log_item')
|
|
op.drop_table('daily_log_item')
|
|
op.drop_table('daily_log')
|
|
op.drop_table('user_settings')
|
|
op.drop_table('user') |