-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structure control migration fix ups (#110)
* Remove measure_variable from memory_control and table_control * Rename control_measure_map to measure_map and control_measure_location to measure_location
- Loading branch information
1 parent
24f15f0
commit 83ea7a5
Showing
4 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
threedi_schema/migrations/versions/0227_fixups_structure_control.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Upgrade settings in schema | ||
Revision ID: 0227 | ||
Revises: | ||
Create Date: 2024-09-24 15:10 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "0227" | ||
down_revision = "0226" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
TABLES = ['memory_control', 'table_control'] | ||
RENAME_TABLES = [('control_measure_location', 'measure_location'), | ||
('control_measure_map', 'measure_map'), ] | ||
|
||
|
||
def fix_geometries(downgrade: bool=False): | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('memory_control', 'geom', 4326, 'POINT', 'XY')")) | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('table_control', 'geom', 4326, 'POINT', 'XY')")) | ||
if downgrade: | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('control_measure_location', 'geom', 4326, 'POINT', 'XY')")) | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('control_measure_map', 'geom', 4326, 'LINESTRING', 'XY')")) | ||
else: | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('measure_location', 'geom', 4326, 'POINT', 'XY')")) | ||
op.execute(sa.text("SELECT RecoverGeometryColumn('measure_map', 'geom', 4326, 'LINESTRING', 'XY')")) | ||
|
||
|
||
def upgrade(): | ||
# remove measure variable from memory_control and table_control | ||
for table_name in TABLES: | ||
with op.batch_alter_table(table_name) as batch_op: | ||
batch_op.drop_column('measure_variable') | ||
# rename column | ||
with op.batch_alter_table('control_measure_map') as batch_op: | ||
batch_op.alter_column('control_measure_location_id', new_column_name='measure_location_id') | ||
# rename tables | ||
for old_table_name, new_table_name in RENAME_TABLES: | ||
op.rename_table(old_table_name, new_table_name) | ||
fix_geometries() | ||
|
||
|
||
def downgrade(): | ||
# undo remove measure variable from memory_control and table_control | ||
for table_name in TABLES: | ||
with op.batch_alter_table(table_name) as batch_op: | ||
batch_op.add_column(sa.Column("measure_variable", sa.Text, server_default="water_level")) | ||
# undo rename columns | ||
with op.batch_alter_table('measure_map') as batch_op: | ||
batch_op.alter_column('measure_location_id', new_column_name='control_measure_location_id') | ||
# rename tables | ||
for old_table_name, new_table_name in RENAME_TABLES: | ||
op.rename_table(new_table_name, old_table_name) | ||
fix_geometries(downgrade=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters