|
| 1 | +"""Convert registrar column types |
| 2 | +
|
| 3 | +Revision ID: 330024be7bef |
| 4 | +Revises: 9d2f6fab52b1 |
| 5 | +Create Date: 2024-02-15 11:48:41.458971 |
| 6 | +
|
| 7 | +""" |
| 8 | +import sqlalchemy as sa |
| 9 | +from alembic import op |
| 10 | + |
| 11 | +# revision identifiers, used by Alembic. |
| 12 | +revision = "330024be7bef" |
| 13 | +down_revision = "9d2f6fab52b1" |
| 14 | +branch_labels = None |
| 15 | +depends_on = None |
| 16 | + |
| 17 | + |
| 18 | +def upgrade(engine_name): |
| 19 | + globals()[f"upgrade_{engine_name}"]() |
| 20 | + |
| 21 | + |
| 22 | +def downgrade(engine_name): |
| 23 | + globals()[f"downgrade_{engine_name}"]() |
| 24 | + |
| 25 | + |
| 26 | +def upgrade_registrar(): |
| 27 | + with op.batch_alter_table("registrarmain") as batch_op: |
| 28 | + # SQLite, MySQL and MariaDB do not have a native BOOLEAN datatype but Postgres does. In the former engines, True |
| 29 | + # and False are automatically translated to 1 and 0 respectively. In Postgres, attempting to set an INTEGER |
| 30 | + # column to True/False results in an error. To ensure consistent behaviour across engines, convert the relevant |
| 31 | + # columns to the SQLAlchemy "Boolean" datatype which will automatically use the appropriate engine-native |
| 32 | + # datatype (INTEGER for SQLite, TINYINT for MySQL/MariaDB and BOOLEAN for Postgres). |
| 33 | + batch_op.alter_column( |
| 34 | + "active", |
| 35 | + existing_type=sa.Integer, |
| 36 | + type_=sa.Boolean, |
| 37 | + existing_nullable=True, |
| 38 | + postgresql_using="active::boolean", |
| 39 | + ) |
| 40 | + batch_op.alter_column( |
| 41 | + "virtual", |
| 42 | + existing_type=sa.Integer, |
| 43 | + type_=sa.Boolean, |
| 44 | + existing_nullable=True, |
| 45 | + postgresql_using="virtual::boolean", |
| 46 | + ) |
| 47 | + # Certificates can easily be more than 2048 characters when Base64 encoded. SQLite does not enforce length |
| 48 | + # restrictions (VARCHAR(2048) = TEXT) which may have prevented this from being a problem in the past. |
| 49 | + # The other engines do enforce these restrictions, so it is better to treat certificates as TEXT columns. |
| 50 | + batch_op.alter_column( |
| 51 | + "ekcert", |
| 52 | + existing_type=sa.String(2048), |
| 53 | + type_=sa.Text, |
| 54 | + existing_nullable=True, |
| 55 | + ) |
| 56 | + batch_op.alter_column( |
| 57 | + "mtls_cert", |
| 58 | + existing_type=sa.String(2048), |
| 59 | + type_=sa.Text, |
| 60 | + existing_nullable=True, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def downgrade_registrar(): |
| 65 | + with op.batch_alter_table("registrarmain") as batch_op: |
| 66 | + batch_op.alter_column( |
| 67 | + "active", |
| 68 | + existing_type=sa.Boolean, |
| 69 | + type_=sa.Integer, |
| 70 | + existing_nullable=True, |
| 71 | + postgresql_using="active::integer", |
| 72 | + ) |
| 73 | + batch_op.alter_column( |
| 74 | + "virtual", |
| 75 | + existing_type=sa.Boolean, |
| 76 | + type_=sa.Integer, |
| 77 | + existing_nullable=True, |
| 78 | + postgresql_using="virtual::integer", |
| 79 | + ) |
| 80 | + batch_op.alter_column( |
| 81 | + "ekcert", |
| 82 | + existing_type=sa.Text, |
| 83 | + type_=sa.String(2048), |
| 84 | + existing_nullable=True, |
| 85 | + ) |
| 86 | + batch_op.alter_column( |
| 87 | + "mtls_cert", |
| 88 | + existing_type=sa.Text, |
| 89 | + type_=sa.String(2048), |
| 90 | + existing_nullable=True, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def upgrade_cloud_verifier(): |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +def downgrade_cloud_verifier(): |
| 99 | + pass |
0 commit comments