diff --git a/tiled/catalog/migrations/versions/83889e049ddc_reorganize_structure_and_rename_.py b/tiled/catalog/migrations/versions/83889e049ddc_add_table_to_structurefamily_enum.py similarity index 79% rename from tiled/catalog/migrations/versions/83889e049ddc_reorganize_structure_and_rename_.py rename to tiled/catalog/migrations/versions/83889e049ddc_add_table_to_structurefamily_enum.py index 404b378fc..4d769b020 100644 --- a/tiled/catalog/migrations/versions/83889e049ddc_reorganize_structure_and_rename_.py +++ b/tiled/catalog/migrations/versions/83889e049ddc_add_table_to_structurefamily_enum.py @@ -13,6 +13,7 @@ from sqlalchemy.dialects.postgresql import JSONB from tiled.serialization.table import deserialize_arrow +from tiled.structures.core import StructureFamily from tiled.structures.table import B64_ENCODED_PREFIX # revision identifiers, used by Alembic. @@ -21,15 +22,22 @@ branch_labels = None depends_on = None + # Use JSON with SQLite and JSONB with PostgreSQL. JSONVariant = sa.JSON().with_variant(JSONB(), "postgresql") -# This is a *data migration* only. There are no changes to the SQL schema. - def upgrade(): connection = op.get_bind() + if connection.engine.dialect.name == "postgresql": + # This change must be committed befor ethe new 'table' enum value can be used. + with op.get_context().autocommit_block(): + op.execute( + sa.text( + "ALTER TYPE structurefamily ADD VALUE IF NOT EXISTS 'table' AFTER 'dataframe'" + ) + ) # Rename "dataframe" to "table". nodes = sa.Table( "nodes", @@ -37,10 +45,11 @@ def upgrade(): sa.Column("id", sa.Integer), sa.Column("structure_family", sa.Unicode(32)), ) - connection.execute( - nodes.update() - .where(nodes.c.structure_family == "dataframe") - .values(structure_family="table") + # Use a raw text query to work around type fussiness in postgres. + op.execute( + sa.text( + "UPDATE nodes SET structure_family='table' WHERE nodes.structure_family = 'dataframe'" + ) ) data_sources = sa.Table( @@ -59,7 +68,7 @@ def upgrade(): ).select_from(joined) ).fetchall() for id_, structure, structure_family in results: - if structure_family == "array": + if structure_family == StructureFamily.array: # Consolidate "macro" and "micro". new_structure = {} new_structure["shape"] = structure["macro"]["shape"] @@ -67,7 +76,7 @@ def upgrade(): new_structure["chunks"] = structure["macro"]["chunks"] new_structure["resizable"] = structure["macro"]["resizable"] new_structure["data_type"] = structure["micro"] - elif structure_family == "table": + elif structure_family == StructureFamily.table: # Consolidate "macro" and "micro". new_structure = {} new_structure["columns"] = structure["macro"]["columns"]