Skip to content

Commit

Permalink
Remove optionality of orig_id and add name to trip unique contraint
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco committed Oct 1, 2023
1 parent 44e3336 commit 6adaa97
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MuoVErsi/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class Trip(Base):
__tablename__ = 'trips'

id: Mapped[int] = mapped_column(primary_key=True)
orig_id: Mapped[Optional[str]]
orig_id: Mapped[str]
dest_text: Mapped[str]
number: Mapped[int]
orig_dep_date: Mapped[date]
Expand Down
10 changes: 2 additions & 8 deletions alembic/versions/1f2c7b1eec8b_add_source_field_to_trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

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
Expand All @@ -30,16 +27,13 @@ def upgrade() -> None:
# 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'])
op.create_unique_constraint('trips_source_number_orig_dep_date_key', 'trips', ['source', 'number', 'orig_dep_date'])



def downgrade() -> None:
op.drop_constraint(None, 'trips', type_='unique')
op.drop_constraint('trips_source_number_orig_dep_date_key', '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'])

0 comments on commit 6adaa97

Please sign in to comment.