|
| 1 | +"""normalize schema |
| 2 | +
|
| 3 | +Revision ID: 00f2b412576b |
| 4 | +Revises: 955122a76711 |
| 5 | +Create Date: 2019-05-15 21:46:42.147590 |
| 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 = '00f2b412576b' |
| 14 | +down_revision = '955122a76711' |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade(): |
| 20 | + # mark existing tables with old revision |
| 21 | + op.execute('ALTER TABLE rdl.execution RENAME TO execution_955122a76711') |
| 22 | + op.execute('ALTER TABLE rdl.execution_model RENAME TO execution_model_955122a76711') |
| 23 | + |
| 24 | + # create new schema tables |
| 25 | + op.create_table('execution', |
| 26 | + sa.Column('execution_id', postgresql.UUID(as_uuid=True), nullable=False), |
| 27 | + sa.Column('created_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 28 | + nullable=False), |
| 29 | + sa.Column('updated_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 30 | + nullable=False), |
| 31 | + sa.Column('status', sa.String(length=50), server_default='Started', nullable=False), |
| 32 | + sa.Column('started_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 33 | + nullable=False), |
| 34 | + sa.Column('completed_on', sa.DateTime(timezone=True), nullable=True), |
| 35 | + sa.Column('execution_time_s', sa.BigInteger(), nullable=True), |
| 36 | + sa.Column('rows_processed', sa.BigInteger(), nullable=True), |
| 37 | + sa.Column('models_processed', sa.Integer(), nullable=True), |
| 38 | + sa.PrimaryKeyConstraint('execution_id'), |
| 39 | + schema='rdl' |
| 40 | + ) |
| 41 | + op.create_table('execution_model', |
| 42 | + sa.Column('execution_model_id', postgresql.UUID(as_uuid=True), nullable=False), |
| 43 | + sa.Column('created_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 44 | + nullable=False), |
| 45 | + sa.Column('updated_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 46 | + nullable=False), |
| 47 | + sa.Column('execution_id', postgresql.UUID(as_uuid=True), nullable=False), |
| 48 | + sa.Column('model_name', sa.String(length=250), nullable=False), |
| 49 | + sa.Column('status', sa.String(length=50), server_default='Started', nullable=False), |
| 50 | + sa.Column('last_sync_version', sa.BigInteger(), nullable=False), |
| 51 | + sa.Column('sync_version', sa.BigInteger(), nullable=False), |
| 52 | + sa.Column('is_full_refresh', sa.Boolean(), nullable=False), |
| 53 | + sa.Column('full_refresh_reason', sa.String(length=100), nullable=False), |
| 54 | + sa.Column('started_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 55 | + nullable=False), |
| 56 | + sa.Column('completed_on', sa.DateTime(timezone=True), nullable=True), |
| 57 | + sa.Column('execution_time_ms', sa.BigInteger(), nullable=True), |
| 58 | + sa.Column('rows_processed', sa.BigInteger(), nullable=True), |
| 59 | + sa.Column('model_checksum', sa.String(length=100), nullable=False), |
| 60 | + sa.Column('failure_reason', sa.String(length=1000), nullable=True), |
| 61 | + sa.ForeignKeyConstraint(['execution_id'], ['rdl.execution.execution_id'], ), |
| 62 | + sa.PrimaryKeyConstraint('execution_model_id'), |
| 63 | + schema='rdl' |
| 64 | + ) |
| 65 | + |
| 66 | + # move data from old tables to new tables |
| 67 | + op.execute( |
| 68 | + ''' |
| 69 | + INSERT INTO rdl.execution ( |
| 70 | + execution_id, created_on, updated_on, |
| 71 | + status, started_on, completed_on, |
| 72 | + execution_time_s, rows_processed, models_processed |
| 73 | + ) |
| 74 | + SELECT |
| 75 | + id, execution_started, COALESCE(execution_ended, execution_started), |
| 76 | + status, execution_started, execution_ended, |
| 77 | + execution_time_s, total_rows_processed, total_models_processed |
| 78 | + FROM rdl.execution_955122a76711 |
| 79 | + ''') |
| 80 | + op.execute( |
| 81 | + ''' |
| 82 | + INSERT INTO rdl.execution_model ( |
| 83 | + execution_model_id, execution_id, created_on, updated_on, |
| 84 | + model_name, status, started_on, completed_on, failure_reason, |
| 85 | + last_sync_version, sync_version, is_full_refresh, full_refresh_reason, |
| 86 | + execution_time_ms, rows_processed, model_checksum |
| 87 | + ) |
| 88 | + SELECT |
| 89 | + uuid_generate_v4(), execution_id, started_on, COALESCE(completed_on, started_on), |
| 90 | + model_name, status, started_on, completed_on, failure_reason, |
| 91 | + last_sync_version, sync_version, is_full_refresh, full_refresh_reason, |
| 92 | + execution_time_ms, rows_processed, model_checksum |
| 93 | + FROM rdl.execution_model_955122a76711 |
| 94 | + ''') |
| 95 | + |
| 96 | + # drop old tables |
| 97 | + op.drop_table('execution_model_955122a76711', schema='rdl') |
| 98 | + op.drop_table('execution_955122a76711', schema='rdl') |
| 99 | + |
| 100 | + |
| 101 | +def downgrade(): |
| 102 | + # mark existing tables with new revision |
| 103 | + op.execute('ALTER TABLE rdl.execution RENAME TO execution_00f2b412576b') |
| 104 | + op.execute('ALTER TABLE rdl.execution_model RENAME TO execution_model_00f2b412576b') |
| 105 | + |
| 106 | + # create old revision tables |
| 107 | + op.create_table('execution', |
| 108 | + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), |
| 109 | + sa.Column('status', sa.String(length=50), server_default='Started', nullable=False), |
| 110 | + sa.Column('execution_started', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 111 | + nullable=False), |
| 112 | + sa.Column('execution_ended', sa.DateTime(timezone=True), nullable=True), |
| 113 | + sa.Column('execution_time_s', sa.BigInteger(), nullable=True), |
| 114 | + sa.Column('total_rows_processed', sa.BigInteger(), nullable=True), |
| 115 | + sa.Column('total_models_processed', sa.Integer(), nullable=True), |
| 116 | + sa.PrimaryKeyConstraint('id'), |
| 117 | + schema='rdl' |
| 118 | + ) |
| 119 | + op.create_table('execution_model', |
| 120 | + sa.Column('execution_id', postgresql.UUID(as_uuid=True), nullable=True), |
| 121 | + sa.Column('model_name', sa.String(length=250), nullable=False), |
| 122 | + sa.Column('status', sa.String(length=25), nullable=False), |
| 123 | + sa.Column('last_sync_version', sa.BigInteger(), nullable=False), |
| 124 | + sa.Column('sync_version', sa.BigInteger(), nullable=False), |
| 125 | + sa.Column('is_full_refresh', sa.Boolean(), nullable=False), |
| 126 | + sa.Column('full_refresh_reason', sa.String(length=100), nullable=False), |
| 127 | + sa.Column('started_on', sa.DateTime(timezone=True), server_default=sa.text('now()'), |
| 128 | + nullable=False), |
| 129 | + sa.Column('completed_on', sa.DateTime(timezone=True), nullable=True), |
| 130 | + sa.Column('execution_time_ms', sa.Integer(), nullable=True), |
| 131 | + sa.Column('rows_processed', sa.Integer(), nullable=True), |
| 132 | + sa.Column('model_checksum', sa.String(length=100), nullable=False), |
| 133 | + sa.Column('failure_reason', sa.String(length=1000), nullable=True), |
| 134 | + # sa.ForeignKeyConstraint(['execution_id'], ['rdl.execution.id'], ), |
| 135 | + # sa.PrimaryKeyConstraint('execution_id', 'model_name'), |
| 136 | + schema='rdl' |
| 137 | + ) |
| 138 | + op.create_primary_key("pk_data_load_execution", "execution_model", |
| 139 | + ["execution_id", "model_name"], schema='rdl') |
| 140 | + op.create_foreign_key("data_load_execution_execution_id_fkey", 'execution_model', 'execution', ['execution_id'], |
| 141 | + ['id'], source_schema='rdl', referent_schema='rdl') |
| 142 | + |
| 143 | + # move data from new revision tables to old revision tables |
| 144 | + op.execute( |
| 145 | + ''' |
| 146 | + INSERT INTO rdl.execution ( |
| 147 | + id, |
| 148 | + status, execution_started, execution_ended, |
| 149 | + execution_time_s, total_rows_processed, total_models_processed |
| 150 | + ) |
| 151 | + SELECT |
| 152 | + execution_id, |
| 153 | + status, started_on, completed_on, |
| 154 | + execution_time_s, rows_processed, models_processed |
| 155 | + FROM rdl.execution_00f2b412576b |
| 156 | + ''') |
| 157 | + op.execute( |
| 158 | + ''' |
| 159 | + INSERT INTO rdl.execution_model ( |
| 160 | + execution_id, |
| 161 | + model_name, status, started_on, completed_on, failure_reason, |
| 162 | + last_sync_version, sync_version, is_full_refresh, full_refresh_reason, |
| 163 | + execution_time_ms, rows_processed, model_checksum |
| 164 | + ) |
| 165 | + SELECT |
| 166 | + execution_id, |
| 167 | + model_name, status, started_on, completed_on, failure_reason, |
| 168 | + last_sync_version, sync_version, is_full_refresh, full_refresh_reason, |
| 169 | + execution_time_ms, rows_processed, model_checksum |
| 170 | + FROM rdl.execution_model_00f2b412576b |
| 171 | + ''') |
| 172 | + |
| 173 | + # drop new revision tables |
| 174 | + op.drop_table('execution_model_00f2b412576b', schema='rdl') |
| 175 | + op.drop_table('execution_00f2b412576b', schema='rdl') |
0 commit comments