-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into feat/upload_gtfs_stop_times_to_postgres
- Loading branch information
Showing
3 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
alembic/versions/1f2c7b1eec8b_add_source_field_to_trips.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""add source field to trips | ||
Revision ID: 1f2c7b1eec8b | ||
Revises: e07191853dcb | ||
Create Date: 2023-10-01 16:46:36.372782 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '1f2c7b1eec8b' | ||
down_revision = 'e07191853dcb' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.add_column('trips', sa.Column('source', sa.String(), server_default='treni', nullable=False)) | ||
op.alter_column('trips', 'orig_id', | ||
existing_type=sa.VARCHAR(), | ||
nullable=True) | ||
op.drop_constraint('trains_codOrigine_numeroTreno_dataPartenzaTreno_key', 'trips', type_='unique') | ||
|
||
# update foreign key of stop_times.trip_id to cascade on delete | ||
op.drop_constraint('stop_times_train_id_fkey', 'stop_times', type_='foreignkey') | ||
op.create_foreign_key('stop_times_trip_id_fkey', 'stop_times', 'trips', ['trip_id'], ['id'], ondelete='CASCADE') | ||
|
||
# remove duplicates of source, number, orig_dep_date from trips table | ||
op.execute('DELETE FROM trips WHERE id IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER (partition BY source, number, orig_dep_date ORDER BY id) AS rnum FROM trips) t WHERE t.rnum > 1);') | ||
|
||
op.create_unique_constraint(None, 'trips', ['source', 'number', 'orig_dep_date']) | ||
|
||
|
||
|
||
def downgrade() -> None: | ||
op.drop_constraint(None, 'trips', type_='unique') | ||
op.create_unique_constraint('trains_codOrigine_numeroTreno_dataPartenzaTreno_key', 'trips', ['orig_id', 'number', 'orig_dep_date']) | ||
op.alter_column('trips', 'orig_id', | ||
existing_type=sa.VARCHAR(), | ||
nullable=False) | ||
op.drop_column('trips', 'source') | ||
op.drop_constraint('stop_times_trip_id_fkey', 'stop_times', type_='foreignkey') | ||
op.create_foreign_key('stop_times_train_id_fkey', 'stop_times', 'trips', ['trip_id'], ['id']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters