diff --git a/be/alembic/versions/0001_initial_schema.py b/be/alembic/versions/0001_initial_schema.py index 21be9c3..e428ac0 100644 --- a/be/alembic/versions/0001_initial_schema.py +++ b/be/alembic/versions/0001_initial_schema.py @@ -25,17 +25,15 @@ recurrence_type_enum = postgresql.ENUM('DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY', n chore_frequency_enum = postgresql.ENUM('one_time', 'daily', 'weekly', 'monthly', 'custom', name='chorefrequencyenum', create_type=False) chore_type_enum = postgresql.ENUM('personal', 'group', name='choretypeenum', create_type=False) -def upgrade(context) -> None: - def create_enums(): - user_role_enum.create(op.get_bind(), checkfirst=True) - split_type_enum.create(op.get_bind(), checkfirst=True) - expense_split_status_enum.create(op.get_bind(), checkfirst=True) - expense_overall_status_enum.create(op.get_bind(), checkfirst=True) - recurrence_type_enum.create(op.get_bind(), checkfirst=True) - chore_frequency_enum.create(op.get_bind(), checkfirst=True) - chore_type_enum.create(op.get_bind(), checkfirst=True) - - create_enums() +def upgrade() -> None: + # Create enums + user_role_enum.create(op.get_bind(), checkfirst=True) + split_type_enum.create(op.get_bind(), checkfirst=True) + expense_split_status_enum.create(op.get_bind(), checkfirst=True) + expense_overall_status_enum.create(op.get_bind(), checkfirst=True) + recurrence_type_enum.create(op.get_bind(), checkfirst=True) + chore_frequency_enum.create(op.get_bind(), checkfirst=True) + chore_type_enum.create(op.get_bind(), checkfirst=True) op.create_table('users', sa.Column('id', sa.Integer(), nullable=False), @@ -109,6 +107,19 @@ def upgrade(context) -> None: op.create_index(op.f('ix_lists_id'), 'lists', ['id'], unique=False) op.create_index(op.f('ix_lists_name'), 'lists', ['name'], unique=False) + op.create_table('recurrence_patterns', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('type', recurrence_type_enum, nullable=False), + sa.Column('interval', sa.Integer(), server_default='1', nullable=False), + sa.Column('days_of_week', sa.String(), nullable=True), + sa.Column('end_date', sa.DateTime(timezone=True), nullable=True), + sa.Column('max_occurrences', sa.Integer(), nullable=True), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_recurrence_patterns_id'), 'recurrence_patterns', ['id'], unique=False) + op.create_table('items', sa.Column('id', sa.Integer(), nullable=False), sa.Column('list_id', sa.Integer(), nullable=False), @@ -129,18 +140,27 @@ def upgrade(context) -> None: op.create_index(op.f('ix_items_id'), 'items', ['id'], unique=False) op.create_index(op.f('ix_items_name'), 'items', ['name'], unique=False) - op.create_table('recurrence_patterns', + op.create_table('chores', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('type', recurrence_type_enum, nullable=False), - sa.Column('interval', sa.Integer(), server_default='1', nullable=False), - sa.Column('days_of_week', sa.String(), nullable=True), - sa.Column('end_date', sa.DateTime(timezone=True), nullable=True), - sa.Column('max_occurrences', sa.Integer(), nullable=True), + sa.Column('type', chore_type_enum, nullable=False), + sa.Column('group_id', sa.Integer(), nullable=True), + sa.Column('name', sa.String(), nullable=False), + sa.Column('description', sa.Text(), nullable=True), + sa.Column('created_by_id', sa.Integer(), nullable=False), + sa.Column('frequency', chore_frequency_enum, nullable=False), + sa.Column('custom_interval_days', sa.Integer(), nullable=True), + sa.Column('next_due_date', sa.Date(), nullable=False), + sa.Column('last_completed_at', sa.DateTime(timezone=True), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], ), + sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ondelete='CASCADE'), sa.PrimaryKeyConstraint('id') ) - op.create_index(op.f('ix_recurrence_patterns_id'), 'recurrence_patterns', ['id'], unique=False) + op.create_index(op.f('ix_chores_created_by_id'), 'chores', ['created_by_id'], unique=False) + op.create_index(op.f('ix_chores_group_id'), 'chores', ['group_id'], unique=False) + op.create_index(op.f('ix_chores_id'), 'chores', ['id'], unique=False) + op.create_index(op.f('ix_chores_name'), 'chores', ['name'], unique=False) op.create_table('expenses', sa.Column('id', sa.Integer(), nullable=False), @@ -180,6 +200,23 @@ def upgrade(context) -> None: op.create_index(op.f('ix_expenses_paid_by_user_id'), 'expenses', ['paid_by_user_id'], unique=False) op.create_index('ix_expenses_recurring_next_occurrence', 'expenses', ['is_recurring', 'next_occurrence'], unique=False, postgresql_where=sa.text('is_recurring = true')) + op.create_table('chore_assignments', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('chore_id', sa.Integer(), nullable=False), + sa.Column('assigned_to_user_id', sa.Integer(), nullable=False), + sa.Column('due_date', sa.Date(), nullable=False), + sa.Column('is_complete', sa.Boolean(), server_default=sa.text('false'), nullable=False), + sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['assigned_to_user_id'], ['users.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['chore_id'], ['chores.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_chore_assignments_assigned_to_user_id'), 'chore_assignments', ['assigned_to_user_id'], unique=False) + op.create_index(op.f('ix_chore_assignments_chore_id'), 'chore_assignments', ['chore_id'], unique=False) + op.create_index(op.f('ix_chore_assignments_id'), 'chore_assignments', ['id'], unique=False) + op.create_table('expense_splits', sa.Column('id', sa.Integer(), nullable=False), sa.Column('expense_id', sa.Integer(), nullable=False), @@ -238,113 +275,31 @@ def upgrade(context) -> None: sa.ForeignKeyConstraint(['paid_by_user_id'], ['users.id'], ), sa.PrimaryKeyConstraint('id') ) - op.create_index(op.f('ix_settlement_activity_created_by_user_id'), 'settlement_activities', ['created_by_user_id'], unique=False) - op.create_index(op.f('ix_settlement_activity_expense_split_id'), 'settlement_activities', ['expense_split_id'], unique=False) - op.create_index(op.f('ix_settlement_activity_paid_by_user_id'), 'settlement_activities', ['paid_by_user_id'], unique=False) + op.create_index(op.f('ix_settlement_activities_created_by_user_id'), 'settlement_activities', ['created_by_user_id'], unique=False) + op.create_index(op.f('ix_settlement_activities_expense_split_id'), 'settlement_activities', ['expense_split_id'], unique=False) + op.create_index(op.f('ix_settlement_activities_paid_by_user_id'), 'settlement_activities', ['paid_by_user_id'], unique=False) - op.create_table('chores', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('type', chore_type_enum, nullable=False), - sa.Column('group_id', sa.Integer(), nullable=True), - sa.Column('name', sa.String(), nullable=False), - sa.Column('description', sa.Text(), nullable=True), - sa.Column('created_by_id', sa.Integer(), nullable=False), - sa.Column('frequency', chore_frequency_enum, nullable=False), - sa.Column('custom_interval_days', sa.Integer(), nullable=True), - sa.Column('next_due_date', sa.Date(), nullable=False), - sa.Column('last_completed_at', sa.DateTime(timezone=True), nullable=True), - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], ), - sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ondelete='CASCADE'), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_chores_created_by_id'), 'chores', ['created_by_id'], unique=False) - op.create_index(op.f('ix_chores_group_id'), 'chores', ['group_id'], unique=False) - op.create_index(op.f('ix_chores_id'), 'chores', ['id'], unique=False) - op.create_index(op.f('ix_chores_name'), 'chores', ['name'], unique=False) - - op.create_table('chore_assignments', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('chore_id', sa.Integer(), nullable=False), - sa.Column('assigned_to_user_id', sa.Integer(), nullable=False), - sa.Column('due_date', sa.Date(), nullable=False), - sa.Column('is_complete', sa.Boolean(), server_default=sa.text('false'), nullable=False), - sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True), - sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), - sa.ForeignKeyConstraint(['assigned_to_user_id'], ['users.id'], ondelete='CASCADE'), - sa.ForeignKeyConstraint(['chore_id'], ['chores.id'], ondelete='CASCADE'), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_chore_assignments_assigned_to_user_id'), 'chore_assignments', ['assigned_to_user_id'], unique=False) - op.create_index(op.f('ix_chore_assignments_chore_id'), 'chore_assignments', ['chore_id'], unique=False) - op.create_index(op.f('ix_chore_assignments_id'), 'chore_assignments', ['id'], unique=False) def downgrade() -> None: - op.drop_table('chore_assignments') - op.drop_index(op.f('ix_chores_name'), table_name='chores') - op.drop_index(op.f('ix_chores_id'), table_name='chores') - op.drop_index(op.f('ix_chores_group_id'), table_name='chores') - op.drop_index(op.f('ix_chores_created_by_id'), table_name='chores') - op.drop_table('chores') - - op.drop_index(op.f('ix_settlement_activity_paid_by_user_id'), table_name='settlement_activities') - op.drop_index(op.f('ix_settlement_activity_expense_split_id'), table_name='settlement_activities') - op.drop_index(op.f('ix_settlement_activity_created_by_user_id'), table_name='settlement_activities') op.drop_table('settlement_activities') - - op.drop_index(op.f('ix_settlements_paid_to_user_id'), table_name='settlements') - op.drop_index(op.f('ix_settlements_paid_by_user_id'), table_name='settlements') - op.drop_index(op.f('ix_settlements_id'), table_name='settlements') - op.drop_index(op.f('ix_settlements_group_id'), table_name='settlements') - op.drop_index(op.f('ix_settlements_created_by_user_id'), table_name='settlements') op.drop_table('settlements') - - op.drop_index(op.f('ix_expense_splits_user_id'), table_name='expense_splits') - op.drop_index(op.f('ix_expense_splits_id'), table_name='expense_splits') op.drop_table('expense_splits') - - op.drop_index('ix_expenses_recurring_next_occurrence', table_name='expenses') - op.drop_index(op.f('ix_expenses_paid_by_user_id'), table_name='expenses') - op.drop_index(op.f('ix_expenses_list_id'), table_name='expenses') - op.drop_index(op.f('ix_expenses_id'), table_name='expenses') - op.drop_index(op.f('ix_expenses_group_id'), table_name='expenses') - op.drop_index(op.f('ix_expenses_created_by_user_id'), table_name='expenses') + op.drop_table('chore_assignments') op.drop_table('expenses') - - op.drop_index(op.f('ix_recurrence_patterns_id'), table_name='recurrence_patterns') - op.drop_table('recurrence_patterns') - - op.drop_index(op.f('ix_items_name'), table_name='items') - op.drop_index(op.f('ix_items_id'), table_name='items') + op.drop_table('chores') op.drop_table('items') - - op.drop_index(op.f('ix_lists_name'), table_name='lists') - op.drop_index(op.f('ix_lists_id'), table_name='lists') + op.drop_table('recurrence_patterns') op.drop_table('lists') - - op.drop_index('ix_invites_active_code', table_name='invites') - op.drop_index(op.f('ix_invites_id'), table_name='invites') - op.drop_index(op.f('ix_invites_code'), table_name='invites') op.drop_table('invites') - - op.drop_index(op.f('ix_user_groups_id'), table_name='user_groups') op.drop_table('user_groups') - - op.drop_index(op.f('ix_groups_name'), table_name='groups') - op.drop_index(op.f('ix_groups_id'), table_name='groups') op.drop_table('groups') - - op.drop_index(op.f('ix_users_name'), table_name='users') - op.drop_index(op.f('ix_users_id'), table_name='users') - op.drop_index(op.f('ix_users_email'), table_name='users') op.drop_table('users') - chore_type_enum.drop(op.get_bind(), checkfirst=False) - chore_frequency_enum.drop(op.get_bind(), checkfirst=False) - recurrence_type_enum.drop(op.get_bind(), checkfirst=False) - expense_overall_status_enum.drop(op.get_bind(), checkfirst=False) - expense_split_status_enum.drop(op.get_bind(), checkfirst=False) - split_type_enum.drop(op.get_bind(), checkfirst=False) - user_role_enum.drop(op.get_bind(), checkfirst=False) + # Drop enums in reverse order + chore_type_enum.drop(op.get_bind(), checkfirst=True) + chore_frequency_enum.drop(op.get_bind(), checkfirst=True) + recurrence_type_enum.drop(op.get_bind(), checkfirst=True) + expense_overall_status_enum.drop(op.get_bind(), checkfirst=True) + expense_split_status_enum.drop(op.get_bind(), checkfirst=True) + split_type_enum.drop(op.get_bind(), checkfirst=True) + user_role_enum.drop(op.get_bind(), checkfirst=True) \ No newline at end of file