|
| 1 | +"""project constraint fix |
| 2 | +
|
| 3 | +Revision ID: 647e7a75e2cd |
| 4 | +Revises: 5fe1ab1ccebe |
| 5 | +Create Date: 2025-06-03 12:48:30.162566 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | + |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = "647e7a75e2cd" |
| 17 | +down_revision: Union[str, None] = "5fe1ab1ccebe" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + """Remove the problematic UNIQUE constraint on is_default column. |
| 24 | +
|
| 25 | + The UNIQUE constraint prevents multiple projects from having is_default=FALSE, |
| 26 | + which breaks project creation when the service sets is_default=False. |
| 27 | +
|
| 28 | + Since SQLite doesn't support dropping specific constraints easily, we'll |
| 29 | + recreate the table without the problematic constraint. |
| 30 | + """ |
| 31 | + # For SQLite, we need to recreate the table without the UNIQUE constraint |
| 32 | + # Create a new table without the UNIQUE constraint on is_default |
| 33 | + op.create_table( |
| 34 | + "project_new", |
| 35 | + sa.Column("id", sa.Integer(), nullable=False), |
| 36 | + sa.Column("name", sa.String(), nullable=False), |
| 37 | + sa.Column("description", sa.Text(), nullable=True), |
| 38 | + sa.Column("permalink", sa.String(), nullable=False), |
| 39 | + sa.Column("path", sa.String(), nullable=False), |
| 40 | + sa.Column("is_active", sa.Boolean(), nullable=False), |
| 41 | + sa.Column("is_default", sa.Boolean(), nullable=True), # No UNIQUE constraint! |
| 42 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 43 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 44 | + sa.PrimaryKeyConstraint("id"), |
| 45 | + sa.UniqueConstraint("name"), |
| 46 | + sa.UniqueConstraint("permalink"), |
| 47 | + ) |
| 48 | + |
| 49 | + # Copy data from old table to new table |
| 50 | + op.execute("INSERT INTO project_new SELECT * FROM project") |
| 51 | + |
| 52 | + # Drop the old table |
| 53 | + op.drop_table("project") |
| 54 | + |
| 55 | + # Rename the new table |
| 56 | + op.rename_table("project_new", "project") |
| 57 | + |
| 58 | + # Recreate the indexes |
| 59 | + with op.batch_alter_table("project", schema=None) as batch_op: |
| 60 | + batch_op.create_index("ix_project_created_at", ["created_at"], unique=False) |
| 61 | + batch_op.create_index("ix_project_name", ["name"], unique=True) |
| 62 | + batch_op.create_index("ix_project_path", ["path"], unique=False) |
| 63 | + batch_op.create_index("ix_project_permalink", ["permalink"], unique=True) |
| 64 | + batch_op.create_index("ix_project_updated_at", ["updated_at"], unique=False) |
| 65 | + |
| 66 | + |
| 67 | +def downgrade() -> None: |
| 68 | + """Add back the UNIQUE constraint on is_default column. |
| 69 | +
|
| 70 | + WARNING: This will break project creation again if multiple projects |
| 71 | + have is_default=FALSE. |
| 72 | + """ |
| 73 | + # Recreate the table with the UNIQUE constraint |
| 74 | + op.create_table( |
| 75 | + "project_old", |
| 76 | + sa.Column("id", sa.Integer(), nullable=False), |
| 77 | + sa.Column("name", sa.String(), nullable=False), |
| 78 | + sa.Column("description", sa.Text(), nullable=True), |
| 79 | + sa.Column("permalink", sa.String(), nullable=False), |
| 80 | + sa.Column("path", sa.String(), nullable=False), |
| 81 | + sa.Column("is_active", sa.Boolean(), nullable=False), |
| 82 | + sa.Column("is_default", sa.Boolean(), nullable=True), |
| 83 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 84 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 85 | + sa.PrimaryKeyConstraint("id"), |
| 86 | + sa.UniqueConstraint("is_default"), # Add back the problematic constraint |
| 87 | + sa.UniqueConstraint("name"), |
| 88 | + sa.UniqueConstraint("permalink"), |
| 89 | + ) |
| 90 | + |
| 91 | + # Copy data (this may fail if multiple FALSE values exist) |
| 92 | + op.execute("INSERT INTO project_old SELECT * FROM project") |
| 93 | + |
| 94 | + # Drop the current table and rename |
| 95 | + op.drop_table("project") |
| 96 | + op.rename_table("project_old", "project") |
| 97 | + |
| 98 | + # Recreate indexes |
| 99 | + with op.batch_alter_table("project", schema=None) as batch_op: |
| 100 | + batch_op.create_index("ix_project_created_at", ["created_at"], unique=False) |
| 101 | + batch_op.create_index("ix_project_name", ["name"], unique=True) |
| 102 | + batch_op.create_index("ix_project_path", ["path"], unique=False) |
| 103 | + batch_op.create_index("ix_project_permalink", ["permalink"], unique=True) |
| 104 | + batch_op.create_index("ix_project_updated_at", ["updated_at"], unique=False) |
0 commit comments