Skip to content

Commit

Permalink
Import stops after creating stops table
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco committed Oct 1, 2023
1 parent fb50ea9 commit 26b400c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions alembic/versions/6c9ef3a680e3_create_stops_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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),
Expand All @@ -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:
Expand Down

0 comments on commit 26b400c

Please sign in to comment.