Skip to content

Commit

Permalink
Merge branch 'margriet_schema_300_leftovers' into margriet_129_fix_us…
Browse files Browse the repository at this point in the history
…e_tables
  • Loading branch information
margrietpalm authored Dec 2, 2024
2 parents 7102e28 + e19b358 commit 179aba6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changelog of threedi-schema

- Rename sqlite table "tags" to "tag"
- Remove indices referring to removed tables in previous migrations
- Make model_settings.use_2d_rain and model_settings.friction_averaging booleans
- Remove columns referencing v2 in geometry_column
- Ensure correct use_* values when matching tables have no data

Expand Down
6 changes: 3 additions & 3 deletions threedi_schema/domain/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ class ModelSettings(Base):
embedded_cutoff_threshold = Column(Float)
epsg_code = Column(Integer)
max_angle_1d_advection = Column(Float)
friction_averaging = Column(IntegerEnum(constants.OffOrStandard))
friction_averaging = Column(Boolean)
table_step_size_1d = Column(Float)
use_2d_rain = Column(Integer)
use_2d_rain = Column(Boolean)
use_interflow = Column(Boolean)
use_interception = Column(Boolean)
use_simple_infiltration = Column(Boolean)
Expand Down Expand Up @@ -409,7 +409,7 @@ class PhysicalSettings(Base):
__tablename__ = "physical_settings"
id = Column(Integer, primary_key=True)
use_advection_1d = Column(IntegerEnum(constants.AdvectionTypes1D))
use_advection_2d = Column(IntegerEnum(constants.OffOrStandard))
use_advection_2d = Column(Boolean)


class SimulationTemplateSettings(Base):
Expand Down
52 changes: 46 additions & 6 deletions threedi_schema/migrations/versions/0229_clean_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,60 @@
Create Date: 2024-11-15 14:18
"""
import uuid
from typing import List

import sqlalchemy as sa
from alembic import op

from threedi_schema import models


# revision identifiers, used by Alembic.
revision = "0229"
down_revision = "0228"
branch_labels = None
depends_on = None


def find_model(table_name):
for model in models.DECLARED_MODELS:
if model.__tablename__ == table_name:
return model
# This can only go wrong if the migration or model is incorrect
raise


def create_sqlite_table_from_model(model, table_name):
cols = get_cols_for_model(model, skip_cols=["id"])
op.execute(sa.text(f"""
CREATE TABLE {table_name} (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
{','.join(f"{col.name} {col.type}" for col in cols)}
);"""))


def get_cols_for_model(model, skip_cols=None):
from sqlalchemy.orm.attributes import InstrumentedAttribute
if skip_cols is None:
skip_cols = []
return [getattr(model, item) for item in model.__dict__
if item not in skip_cols
and isinstance(getattr(model, item), InstrumentedAttribute)]


def sync_orm_types_to_sqlite(table_name):
temp_table_name = f'_temp_229_{uuid.uuid4().hex}'
model = find_model(table_name)
create_sqlite_table_from_model(model, temp_table_name)
col_names = [col.name for col in get_cols_for_model(model)]
# This may copy wrong type data because some types change!!
op.execute(sa.text(f"INSERT INTO {temp_table_name} ({','.join(col_names)}) "
f"SELECT {','.join(col_names)} FROM {table_name}"))
op.execute(sa.text(f"DROP TABLE {table_name}"))
op.execute(sa.text(f"ALTER TABLE {temp_table_name} RENAME TO {table_name};"))


def remove_tables(tables: List[str]):
for table in tables:
op.drop_table(table)
Expand Down Expand Up @@ -57,11 +97,6 @@ def clean_triggers():
op.execute(f"DROP TRIGGER IF EXISTS {trigger};")


def upgrade():
remove_old_tables()
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 = [
Expand Down Expand Up @@ -93,9 +128,14 @@ def update_use_settings():
.values({setting.name: False})
)


def upgrade():
remove_old_tables()
clean_geometry_columns()
clean_triggers()
update_use_settings()
# Apply changing use_2d_rain and friction_averaging type to bool
sync_orm_types_to_sqlite('model_settings')


def downgrade():
Expand Down

0 comments on commit 179aba6

Please sign in to comment.