Skip to content

Commit

Permalink
Populate group table
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirving committed Mar 12, 2024
1 parent dcd25cc commit 34a3f90
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions migrations/dimensions-config/1fae088c80b6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand All @@ -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,
)
)

0 comments on commit 34a3f90

Please sign in to comment.