Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Upgrade pm_event_id coumns to BIGINT Type #473

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading