Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use tables #145

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Changelog of threedi-schema
- Rename sqlite table "tags" to "tag"
- Remove indices referring to removed tables in previous migrations
- Remove columns referencing v2 in geometry_column

- Ensure correct use_* values when matching tables have no data


0.228.1 (2024-11-26)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ def set_use_inteception():
);
"""))

op.execute(sa.text("""
DELETE FROM interception
WHERE (interception IS NULL OR interception = '')
AND (interception_file IS NULL OR interception_file = '');
"""))


def delete_all_but_matching_id(table, settings_id):
op.execute(f"DELETE FROM {table} WHERE id NOT IN (SELECT {settings_id} FROM model_settings);")
Expand Down
11 changes: 11 additions & 0 deletions threedi_schema/migrations/versions/0223_upgrade_db_inflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@ def populate_surface_and_dry_weather_flow():
# Populate tables with default values
populate_dry_weather_flow_distribution()
populate_surface_parameters()
update_use_0d_inflow()

def update_use_0d_inflow():
op.execute(sa.text("""
UPDATE simulation_template_settings
SET use_0d_inflow = 0
WHERE
(SELECT COUNT(*) FROM surface) = 0
AND
(SELECT COUNT(*) FROM dry_weather_flow) = 0;
"""))


def set_surface_parameters_id():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ def fix_geometry_columns():
op.execute(sa.text(migration_query))


def update_use_structure_control():
op.execute("""
UPDATE simulation_template_settings SET use_structure_control = CASE
WHEN
(SELECT COUNT(*) FROM table_control) = 0 AND
(SELECT COUNT(*) FROM memory_control) = 0 THEN 0
ELSE use_structure_control
END;
""")


def upgrade():
# Remove existing tables (outside of the specs) that conflict with new table names
drop_conflicting(op, list(ADD_TABLES.keys()) + [new_name for _, new_name in RENAME_TABLES])
Expand All @@ -395,6 +406,7 @@ def upgrade():
rename_measure_operator('memory_control')
move_setting('model_settings', 'use_structure_control',
'simulation_template_settings', 'use_structure_control')
update_use_structure_control()
remove_tables(DEL_TABLES)
# Fix geometry columns and also make all but geom column nullable
fix_geometry_columns()
Expand Down
37 changes: 37 additions & 0 deletions threedi_schema/migrations/versions/0229_clean_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import sqlalchemy as sa
from alembic import op

from threedi_schema import models

# revision identifiers, used by Alembic.
revision = "0229"
down_revision = "0228"
Expand Down Expand Up @@ -60,6 +62,41 @@ def upgrade():
clean_geometry_columns()
clean_triggers()

def update_use_settings():
# Ensure that use_* settings are only True when there is actual data for them
use_settings = [
(models.ModelSettings.use_groundwater_storage, models.GroundWater),
(models.ModelSettings.use_groundwater_flow, models.GroundWater),
(models.ModelSettings.use_interflow, models.Interflow),
(models.ModelSettings.use_simple_infiltration, models.SimpleInfiltration),
(models.ModelSettings.use_vegetation_drag_2d, models.VegetationDrag),
(models.ModelSettings.use_interception, models.Interception)
]
connection = op.get_bind() # Get the connection for raw SQL execution
for setting, table in use_settings:
use_row = connection.execute(
sa.select(getattr(models.ModelSettings, setting.name))
).scalar()
if not use_row:
continue
row = connection.execute(sa.select(table)).first()
use_row = (row is not None)
if use_row:
use_row = not all(
getattr(row, column.name) in (None, "")
for column in table.__table__.columns
if column.name != "id"
)
if not use_row:
connection.execute(
sa.update(models.ModelSettings)
.values({setting.name: False})
)


def upgrade():
update_use_settings()


def downgrade():
pass