diff --git a/alembic/versions/6c9ef3a680e3_create_stops_table.py b/alembic/versions/6c9ef3a680e3_create_stops_table.py index cd0761e..1dfd61a 100644 --- a/alembic/versions/6c9ef3a680e3_create_stops_table.py +++ b/alembic/versions/6c9ef3a680e3_create_stops_table.py @@ -7,6 +7,9 @@ """ import sqlalchemy as sa from alembic import op +from sqlalchemy.orm import sessionmaker + +from MuoVErsi.sources.base import Station # revision identifiers, used by Alembic. revision = '6c9ef3a680e3' @@ -16,8 +19,7 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('stops', + stops_table = op.create_table('stops', sa.Column('id', sa.String(), nullable=False), sa.Column('platform', sa.String(), nullable=True), sa.Column('lat', sa.Float(), nullable=False), @@ -26,7 +28,18 @@ def upgrade() -> None: sa.ForeignKeyConstraint(['station_id'], ['stations.id'], ), sa.PrimaryKeyConstraint('id') ) - # ### end Alembic commands ### + + # populate stops table from stations table + session = sessionmaker(bind=op.get_bind())() + bulk_inserts = [] + for station in session.scalars(sa.select(Station)).all(): + bulk_inserts.append({ + 'id': station.id, + 'lat': station.lat, + 'lon': station.lon, + 'station_id': station.id, + }) + op.bulk_insert(stops_table, bulk_inserts) def downgrade() -> None: