UMBRA/backend/alembic/versions/001_initial_migration.py
2026-02-15 16:13:41 +08:00

174 lines
8.4 KiB
Python

"""Initial migration - create all tables
Revision ID: 001
Revises:
Create Date: 2024-01-01 00:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '001'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create settings table
op.create_table(
'settings',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pin_hash', sa.String(length=255), nullable=False),
sa.Column('accent_color', sa.String(length=20), nullable=False, server_default='cyan'),
sa.Column('upcoming_days', sa.Integer(), nullable=False, server_default='7'),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_settings_id'), 'settings', ['id'], unique=False)
# Create people table
op.create_table(
'people',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('email', sa.String(length=255), nullable=True),
sa.Column('phone', sa.String(length=50), nullable=True),
sa.Column('address', sa.Text(), nullable=True),
sa.Column('birthday', sa.Date(), nullable=True),
sa.Column('relationship', sa.String(length=100), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_people_id'), 'people', ['id'], unique=False)
# Create locations table
op.create_table(
'locations',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('address', sa.Text(), nullable=False),
sa.Column('category', sa.String(length=100), nullable=False, server_default='other'),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_locations_id'), 'locations', ['id'], unique=False)
# Create projects table
op.create_table(
'projects',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('status', sa.String(length=20), nullable=False, server_default='not_started'),
sa.Column('color', sa.String(length=20), nullable=True),
sa.Column('due_date', sa.Date(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_projects_id'), 'projects', ['id'], unique=False)
# Create reminders table
op.create_table(
'reminders',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('remind_at', sa.DateTime(), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'),
sa.Column('is_dismissed', sa.Boolean(), nullable=False, server_default='false'),
sa.Column('recurrence_rule', sa.String(length=255), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_reminders_id'), 'reminders', ['id'], unique=False)
# Create calendar_events table
op.create_table(
'calendar_events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('start_datetime', sa.DateTime(), nullable=False),
sa.Column('end_datetime', sa.DateTime(), nullable=False),
sa.Column('all_day', sa.Boolean(), nullable=False, server_default='false'),
sa.Column('color', sa.String(length=20), nullable=True),
sa.Column('location_id', sa.Integer(), nullable=True),
sa.Column('recurrence_rule', sa.String(length=255), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.ForeignKeyConstraint(['location_id'], ['locations.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_calendar_events_id'), 'calendar_events', ['id'], unique=False)
# Create todos table
op.create_table(
'todos',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('priority', sa.String(length=20), nullable=False, server_default='medium'),
sa.Column('due_date', sa.Date(), nullable=True),
sa.Column('completed', sa.Boolean(), nullable=False, server_default='false'),
sa.Column('completed_at', sa.DateTime(), nullable=True),
sa.Column('category', sa.String(length=100), nullable=True),
sa.Column('recurrence_rule', sa.String(length=255), nullable=True),
sa.Column('project_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_todos_id'), 'todos', ['id'], unique=False)
# Create project_tasks table
op.create_table(
'project_tasks',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('status', sa.String(length=20), nullable=False, server_default='pending'),
sa.Column('priority', sa.String(length=20), nullable=False, server_default='medium'),
sa.Column('due_date', sa.Date(), nullable=True),
sa.Column('person_id', sa.Integer(), nullable=True),
sa.Column('sort_order', sa.Integer(), nullable=False, server_default='0'),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
sa.ForeignKeyConstraint(['person_id'], ['people.id'], ),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_project_tasks_id'), 'project_tasks', ['id'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_project_tasks_id'), table_name='project_tasks')
op.drop_table('project_tasks')
op.drop_index(op.f('ix_todos_id'), table_name='todos')
op.drop_table('todos')
op.drop_index(op.f('ix_calendar_events_id'), table_name='calendar_events')
op.drop_table('calendar_events')
op.drop_index(op.f('ix_reminders_id'), table_name='reminders')
op.drop_table('reminders')
op.drop_index(op.f('ix_projects_id'), table_name='projects')
op.drop_table('projects')
op.drop_index(op.f('ix_locations_id'), table_name='locations')
op.drop_table('locations')
op.drop_index(op.f('ix_people_id'), table_name='people')
op.drop_table('people')
op.drop_index(op.f('ix_settings_id'), table_name='settings')
op.drop_table('settings')