|
| 1 | +"""update confirmation created column |
| 2 | +
|
| 3 | +Revision ID: 9dddb16914a4 |
| 4 | +Revises: 5679165336c8 |
| 5 | +Create Date: 2025-07-28 17:25:06.534720+00:00 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import sqlalchemy as sa |
| 10 | +from alembic import op |
| 11 | +from sqlalchemy.dialects import postgresql |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision = "9dddb16914a4" |
| 15 | +down_revision = "5679165336c8" |
| 16 | +branch_labels = None |
| 17 | +depends_on = None |
| 18 | + |
| 19 | + |
| 20 | +def upgrade(): |
| 21 | + # ### commands auto generated by Alembic - please adjust! ### |
| 22 | + # Step 1: Add new column as nullable first |
| 23 | + op.add_column( |
| 24 | + "confirmations", |
| 25 | + sa.Column( |
| 26 | + "created", |
| 27 | + sa.DateTime(timezone=True), |
| 28 | + nullable=True, |
| 29 | + ), |
| 30 | + ) |
| 31 | + |
| 32 | + # Step 2: Copy data from created_at to created, assuming UTC timezone for existing data |
| 33 | + op.execute( |
| 34 | + "UPDATE confirmations SET created = created_at AT TIME ZONE 'UTC' WHERE created_at IS NOT NULL" |
| 35 | + ) |
| 36 | + |
| 37 | + # Step 3: Make the column non-nullable with default |
| 38 | + op.alter_column( |
| 39 | + "confirmations", |
| 40 | + "created", |
| 41 | + nullable=False, |
| 42 | + server_default=sa.text("now()"), |
| 43 | + ) |
| 44 | + |
| 45 | + # Step 4: Drop old column |
| 46 | + op.drop_column("confirmations", "created_at") |
| 47 | + # ### end Alembic commands ### |
| 48 | + |
| 49 | + |
| 50 | +def downgrade(): |
| 51 | + # ### commands auto generated by Alembic - please adjust! ### |
| 52 | + # Step 1: Add back the old column |
| 53 | + op.add_column( |
| 54 | + "confirmations", |
| 55 | + sa.Column( |
| 56 | + "created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + # Step 2: Copy data back, converting timezone-aware to naive timestamp |
| 61 | + op.execute( |
| 62 | + "UPDATE confirmations SET created_at = created AT TIME ZONE 'UTC' WHERE created IS NOT NULL" |
| 63 | + ) |
| 64 | + |
| 65 | + # Step 3: Make the column non-nullable |
| 66 | + op.alter_column( |
| 67 | + "confirmations", |
| 68 | + "created_at", |
| 69 | + nullable=False, |
| 70 | + ) |
| 71 | + |
| 72 | + # Step 4: Drop new column |
| 73 | + op.drop_column("confirmations", "created") |
| 74 | + # ### end Alembic commands ### |
0 commit comments