Skip to content

Commit

Permalink
FIX: Upgrade pm_event_id coumns to BIGINT Type (#473)
Browse files Browse the repository at this point in the history
This change updates the pm_event_id columns from int to bigint so we don't run out of keys.
  • Loading branch information
rymarczy authored Jan 7, 2025
1 parent 7df9a5e commit e5b923a
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""upgrade sequence
Revision ID: 36e7a7aee148
Revises: 32ba735d080c
Create Date: 2025-01-07 13:57:50.433896
This change upgrades the pm_event_id sequence type to bigint to avoid running out of keys
Details
* upgrade -> drop opmi view, upgrade sequence, update sequence storage columns
* downgrade -> not possible, can't go from bigint to int
"""

from alembic import op
import sqlalchemy as sa

from lamp_py.migrations.versions.performance_manager_prod.sql_strings.strings_001 import view_opmi_all_rt_fields_joined

# revision identifiers, used by Alembic.
revision = "36e7a7aee148"
down_revision = "32ba735d080c"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Upgrade sequence to BIGINT
op.execute("ALTER SEQUENCE vehicle_events_pm_event_id_seq as bigint MAXVALUE 9223372036854775807;")
# DROP VIEW before upgrading columns
drop_opmi_all_rt_fields_joined = "DROP VIEW IF EXISTS opmi_all_rt_fields_joined;"
op.execute(drop_opmi_all_rt_fields_joined)
# Upgrade event_id columns to BIGINT
op.alter_column(
"vehicle_events",
"pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=False,
autoincrement=True,
)
op.alter_column(
"vehicle_events",
"previous_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.alter_column(
"vehicle_events",
"next_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.execute(view_opmi_all_rt_fields_joined)


def downgrade() -> None:
# Can not migrate from INT to BIGINT without losing data.
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""upgrade sequence
Revision ID: 36e7a7aee148
Revises: 32ba735d080c
Create Date: 2025-01-07 13:57:50.433896
This change upgrades the pm_event_id sequence type to bigint to avoid running out of keys
Details
* upgrade -> drop opmi view, upgrade sequence, update sequence storage columns
* downgrade -> not possible, can't go from bigint to int
"""

from alembic import op
import sqlalchemy as sa

from lamp_py.migrations.versions.performance_manager_prod.sql_strings.strings_001 import view_opmi_all_rt_fields_joined

# revision identifiers, used by Alembic.
revision = "36e7a7aee148"
down_revision = "32ba735d080c"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Upgrade sequence to BIGINT
op.execute("ALTER SEQUENCE vehicle_events_pm_event_id_seq as bigint MAXVALUE 9223372036854775807;")
# DROP VIEW before upgrading columns
drop_opmi_all_rt_fields_joined = "DROP VIEW IF EXISTS opmi_all_rt_fields_joined;"
op.execute(drop_opmi_all_rt_fields_joined)
# Upgrade event_id columns to BIGINT
op.alter_column(
"vehicle_events",
"pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=False,
autoincrement=True,
)
op.alter_column(
"vehicle_events",
"previous_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.alter_column(
"vehicle_events",
"next_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.execute(view_opmi_all_rt_fields_joined)


def downgrade() -> None:
# Can not migrate from INT to BIGINT without losing data.
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""upgrade sequence
Revision ID: 36e7a7aee148
Revises: 32ba735d080c
Create Date: 2025-01-07 13:57:50.433896
This change upgrades the pm_event_id sequence type to bigint to avoid running out of keys
Details
* upgrade -> drop opmi view, upgrade sequence, update sequence storage columns
* downgrade -> not possible, can't go from bigint to int
"""

from alembic import op
import sqlalchemy as sa

from lamp_py.migrations.versions.performance_manager_staging.sql_strings.strings_001 import (
view_opmi_all_rt_fields_joined,
)

# revision identifiers, used by Alembic.
revision = "36e7a7aee148"
down_revision = "32ba735d080c"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Upgrade sequence to BIGINT
op.execute("ALTER SEQUENCE vehicle_events_pm_event_id_seq as bigint MAXVALUE 9223372036854775807;")
# DROP VIEW before upgrading columns
drop_opmi_all_rt_fields_joined = "DROP VIEW IF EXISTS opmi_all_rt_fields_joined;"
op.execute(drop_opmi_all_rt_fields_joined)
# Upgrade event_id columns to BIGINT
op.alter_column(
"vehicle_events",
"pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=False,
autoincrement=True,
)
op.alter_column(
"vehicle_events",
"previous_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.alter_column(
"vehicle_events",
"next_trip_stop_pm_event_id",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
existing_nullable=True,
)
op.execute(view_opmi_all_rt_fields_joined)


def downgrade() -> None:
# Can not migrate from INT to BIGINT without losing data.
pass
6 changes: 3 additions & 3 deletions src/lamp_py/postgres/rail_performance_manager_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VehicleEvents(RpmSqlBase): # pylint: disable=too-few-public-methods

__tablename__ = "vehicle_events"

pm_event_id = sa.Column(sa.Integer, primary_key=True)
pm_event_id = sa.Column(sa.BigInteger, primary_key=True)

# trip identifiers
service_date = sa.Column(sa.Integer, nullable=False)
Expand All @@ -33,8 +33,8 @@ class VehicleEvents(RpmSqlBase): # pylint: disable=too-few-public-methods
parent_station = sa.Column(sa.String(60), nullable=False)

# stop link fields
previous_trip_stop_pm_event_id = sa.Column(sa.Integer, nullable=True)
next_trip_stop_pm_event_id = sa.Column(sa.Integer, nullable=True)
previous_trip_stop_pm_event_id = sa.Column(sa.BigInteger, nullable=True)
next_trip_stop_pm_event_id = sa.Column(sa.BigInteger, nullable=True)

# event timestamps used for metrics
vp_move_timestamp = sa.Column(sa.Integer, nullable=True)
Expand Down

0 comments on commit e5b923a

Please sign in to comment.