Skip to content

Commit

Permalink
Refine catalog migration for Postgres (#543)
Browse files Browse the repository at this point in the history
* Refine migration for postgres.

* Use begin()

* one migration again
  • Loading branch information
danielballan authored Aug 5, 2023
1 parent a7d77b2 commit 1cc0347
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,26 +22,34 @@
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",
sa.MetaData(),
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(
Expand All @@ -59,15 +68,15 @@ 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"]
new_structure["dims"] = structure["macro"]["dims"]
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"]
Expand Down

0 comments on commit 1cc0347

Please sign in to comment.