|
| 1 | +"""Add indexes, primary keys on join tables. ondelete cascade on primary keys and so passive_deletes=True in relationships. |
| 2 | +
|
| 3 | +Revision ID: ec7ff666caa2 |
| 4 | +Revises: c2ba8ccc9a6f |
| 5 | +Create Date: 2025-01-21 01:51:04.078090 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | + |
| 12 | +# revision identifiers, used by Alembic. |
| 13 | +revision = 'ec7ff666caa2' |
| 14 | +down_revision = 'c2ba8ccc9a6f' |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade(): |
| 20 | + # ### commands auto generated by Alembic - please adjust! ### |
| 21 | + with op.batch_alter_table('category', schema=None) as batch_op: |
| 22 | + batch_op.alter_column('created_at', |
| 23 | + existing_type=postgresql.TIMESTAMP(), |
| 24 | + nullable=False) |
| 25 | + |
| 26 | + with op.batch_alter_table('category_subcategory', schema=None) as batch_op: |
| 27 | + batch_op.create_index('category_subcategory_subcategory_id_idx', ['subcategory_id', 'category_id'], unique=False) |
| 28 | + batch_op.drop_constraint('category_subcategory_category_id_fkey', type_='foreignkey') |
| 29 | + batch_op.drop_constraint('category_subcategory_subcategory_id_fkey', type_='foreignkey') |
| 30 | + batch_op.create_foreign_key(batch_op.f('category_subcategory_category_id_fkey'), 'category', ['category_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE') |
| 31 | + batch_op.create_foreign_key(batch_op.f('category_subcategory_subcategory_id_fkey'), 'subcategory', ['subcategory_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE') |
| 32 | + |
| 33 | + batch_op.create_primary_key('category_subcategory_pkey', ['category_id', 'subcategory_id']) # Added manually |
| 34 | + |
| 35 | + with op.batch_alter_table('product', schema=None) as batch_op: |
| 36 | + batch_op.alter_column('created_at', |
| 37 | + existing_type=postgresql.TIMESTAMP(), |
| 38 | + nullable=False) |
| 39 | + |
| 40 | + with op.batch_alter_table('subcategory', schema=None) as batch_op: |
| 41 | + batch_op.alter_column('created_at', |
| 42 | + existing_type=postgresql.TIMESTAMP(), |
| 43 | + nullable=False) |
| 44 | + |
| 45 | + with op.batch_alter_table('subcategory_product', schema=None) as batch_op: |
| 46 | + batch_op.create_index('subcategory_product_product_id_idx', ['product_id', 'subcategory_id'], unique=False) |
| 47 | + batch_op.drop_constraint('subcategory_product_subcategory_id_fkey', type_='foreignkey') |
| 48 | + batch_op.drop_constraint('subcategory_product_product_id_fkey', type_='foreignkey') |
| 49 | + batch_op.create_foreign_key(batch_op.f('subcategory_product_subcategory_id_fkey'), 'subcategory', ['subcategory_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE') |
| 50 | + batch_op.create_foreign_key(batch_op.f('subcategory_product_product_id_fkey'), 'product', ['product_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE') |
| 51 | + |
| 52 | + batch_op.create_primary_key('subcategory_product_pkey', ['subcategory_id', 'product_id']) # Added manually |
| 53 | + |
| 54 | + # ### end Alembic commands ### |
| 55 | + |
| 56 | + |
| 57 | +def downgrade(): |
| 58 | + # ### commands auto generated by Alembic - please adjust! ### |
| 59 | + with op.batch_alter_table('subcategory_product', schema=None) as batch_op: |
| 60 | + batch_op.drop_constraint(batch_op.f('subcategory_product_product_id_fkey'), type_='foreignkey') |
| 61 | + batch_op.drop_constraint(batch_op.f('subcategory_product_subcategory_id_fkey'), type_='foreignkey') |
| 62 | + batch_op.create_foreign_key('subcategory_product_product_id_fkey', 'product', ['product_id'], ['id']) |
| 63 | + batch_op.create_foreign_key('subcategory_product_subcategory_id_fkey', 'subcategory', ['subcategory_id'], ['id']) |
| 64 | + batch_op.drop_index('subcategory_product_product_id_idx') |
| 65 | + |
| 66 | + batch_op.drop_constraint('subcategory_product_pkey', type_='primary') # added manually |
| 67 | + |
| 68 | + with op.batch_alter_table('subcategory', schema=None) as batch_op: |
| 69 | + batch_op.alter_column('created_at', |
| 70 | + existing_type=postgresql.TIMESTAMP(), |
| 71 | + nullable=True) |
| 72 | + |
| 73 | + with op.batch_alter_table('product', schema=None) as batch_op: |
| 74 | + batch_op.alter_column('created_at', |
| 75 | + existing_type=postgresql.TIMESTAMP(), |
| 76 | + nullable=True) |
| 77 | + |
| 78 | + with op.batch_alter_table('category_subcategory', schema=None) as batch_op: |
| 79 | + batch_op.drop_constraint(batch_op.f('category_subcategory_subcategory_id_fkey'), type_='foreignkey') |
| 80 | + batch_op.drop_constraint(batch_op.f('category_subcategory_category_id_fkey'), type_='foreignkey') |
| 81 | + batch_op.create_foreign_key('category_subcategory_subcategory_id_fkey', 'subcategory', ['subcategory_id'], ['id']) |
| 82 | + batch_op.create_foreign_key('category_subcategory_category_id_fkey', 'category', ['category_id'], ['id']) |
| 83 | + batch_op.drop_index('category_subcategory_subcategory_id_idx') |
| 84 | + |
| 85 | + batch_op.drop_constraint('category_subcategory_pkey', type_='primary') # added manually |
| 86 | + |
| 87 | + with op.batch_alter_table('category', schema=None) as batch_op: |
| 88 | + batch_op.alter_column('created_at', |
| 89 | + existing_type=postgresql.TIMESTAMP(), |
| 90 | + nullable=True) |
| 91 | + |
| 92 | + # ### end Alembic commands ### |
0 commit comments