From 34a3f909d87c6a36d853d27dd4692a9de9318229 Mon Sep 17 00:00:00 2001 From: "David H. Irving" Date: Tue, 12 Mar 2024 15:57:30 -0700 Subject: [PATCH] Populate group table --- migrations/dimensions-config/1fae088c80b6.py | 28 ++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/migrations/dimensions-config/1fae088c80b6.py b/migrations/dimensions-config/1fae088c80b6.py index 518646d..26b1da7 100644 --- a/migrations/dimensions-config/1fae088c80b6.py +++ b/migrations/dimensions-config/1fae088c80b6.py @@ -9,7 +9,7 @@ import logging import sqlalchemy as sa -from alembic import op +from alembic import context, op # revision identifiers, used by Alembic. revision = "1fae088c80b6" @@ -32,7 +32,12 @@ def downgrade() -> None: def _create_group_table() -> None: - op.create_table( + mig_context = context.get_context() + schema = mig_context.version_table_schema + bind = mig_context.bind + assert bind is not None + + table = op.create_table( "group", sa.Column("instrument", sa.String(32), primary_key=True), sa.Column("name", sa.String(64), primary_key=True), @@ -41,4 +46,23 @@ def _create_group_table() -> None: refcolumns=["instrument.name"], name="fkey_group_instrument_name_instrument", ), + schema=schema, + ) + + # Populate the new table based on the data in the exposure table. + metadata = sa.schema.MetaData(schema=schema) + exposure_table = sa.schema.Table("exposure", metadata, autoload_with=bind, schema=schema) + select = sa.select( + exposure_table.columns["instrument"], + exposure_table.columns["group_name"], + ).distinct() + + op.execute( + table.insert().from_select( + [ + "instrument", + "name", + ], + select, + ) )