-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from planetarium/release/2.2.1
Release/2.2.1
- Loading branch information
Showing
17 changed files
with
288 additions
and
53 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
common/alembic/versions/5e7148bfcf36_mileage_history_table.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,36 @@ | ||
"""Mileage history table | ||
Revision ID: 5e7148bfcf36 | ||
Revises: b3b8a0d4eba5 | ||
Create Date: 2024-12-05 09:43:59.814617 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '5e7148bfcf36' | ||
down_revision = 'b3b8a0d4eba5' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('mileage_history', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('agent_addr', sa.Text(), nullable=False), | ||
sa.Column('planet_id', sa.LargeBinary(length=12), nullable=True), | ||
sa.Column('mileage', sa.Integer(), nullable=False), | ||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True), | ||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('mileage_history') | ||
# ### end Alembic commands ### |
36 changes: 36 additions & 0 deletions
36
common/alembic/versions/bbbcfcbe1487_delete_planet_id_to_merge_mileage.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,36 @@ | ||
"""Delete planet_id to merge mileage | ||
Revision ID: bbbcfcbe1487 | ||
Revises: 5e7148bfcf36 | ||
Create Date: 2024-12-05 09:45:18.213511 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'bbbcfcbe1487' | ||
down_revision = '5e7148bfcf36' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint('unique_planet_agent', 'mileage', type_='unique') | ||
op.drop_index('ix_mileage_agent_planet', table_name='mileage') | ||
op.create_index('ix_mileage_agent_planet', 'mileage', ['agent_addr'], unique=False) | ||
op.create_unique_constraint('unique_agent', 'mileage', ['agent_addr']) | ||
op.drop_column('mileage', 'planet_id') | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('mileage', sa.Column('planet_id', postgresql.BYTEA(), autoincrement=False, nullable=False)) | ||
op.drop_constraint('unique_agent', 'mileage', type_='unique') | ||
op.drop_index('ix_mileage_agent_planet', table_name='mileage') | ||
op.create_index('ix_mileage_agent_planet', 'mileage', ['planet_id', 'agent_addr'], unique=False) | ||
op.create_unique_constraint('unique_planet_agent', 'mileage', ['planet_id', 'agent_addr']) | ||
# ### end Alembic commands ### |
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
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
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
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
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
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
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
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,57 @@ | ||
import sys | ||
|
||
from sqlalchemy import create_engine, select, delete | ||
from sqlalchemy.orm import scoped_session, sessionmaker | ||
|
||
from common.models.mileage import Mileage, MileageHistory | ||
from common.utils.receipt import PlanetID | ||
|
||
|
||
def merge(db_uri: str, dry_run: bool = True): | ||
print(f"Merge mileages for {db_uri.split('@')[-1]}") | ||
if dry_run: | ||
print("Dry run mode. This will not update DB.") | ||
else: | ||
proceed = input("You've selected update mode. This will update values into DB. Proceed? [y/N] : ") | ||
print(proceed) | ||
if proceed in ("y", "Y"): | ||
pass | ||
else: | ||
print("Cancel") | ||
return | ||
|
||
engine = create_engine(db_uri) | ||
sess = scoped_session(sessionmaker(bind=engine)) | ||
|
||
print("Delete old merged data") | ||
sess.execute(delete(Mileage).where(Mileage.planet_id.is_(None))) | ||
|
||
history_list = sess.scalars(select(MileageHistory)).fetchall() | ||
print(f"Total {len(history_list)} histories found to merge.") | ||
|
||
merged = {} | ||
print(f"{len(history_list)} mileages to merge") | ||
|
||
for i, m in enumerate(history_list): | ||
target = merged.get(m.agent_addr) | ||
if not target: | ||
merged[m.agent_addr] = target = Mileage(agent_addr=m.agent_addr, planet_id=None, mileage=0) | ||
prev = target.mileage | ||
target.mileage += m.mileage | ||
print( | ||
f"[{i + 1:4d}/{len(history_list):4d}] [{PlanetID(m.planet_id).name}] " | ||
f"{target.agent_addr}: {prev} + {m.mileage} = {target.mileage}" | ||
) | ||
|
||
if dry_run: | ||
print("Dry run mode. Do not update to DB") | ||
else: | ||
print("Update to DB") | ||
sess.add_all(merged.values()) | ||
sess.commit() | ||
|
||
|
||
if __name__ == "__main__": | ||
merge(sys.argv[1], | ||
dry_run=len(sys.argv) > 2 and sys.argv[2].lower() in ("dryrun", "dry_run", "dry-run", "dry") | ||
) |
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,43 @@ | ||
from sqlalchemy import create_engine, select, delete | ||
from sqlalchemy.orm import scoped_session, sessionmaker | ||
|
||
from common.models.mileage import Mileage, MileageHistory | ||
|
||
|
||
def move(db_uri: str, dry_run: bool = True): | ||
print(f"Move mileage to history for {db_uri.split('@')[-1]}") | ||
if dry_run: | ||
print("Dry run. Do not move data.") | ||
else: | ||
proceed = input("You've selected update mode. This will update values into DB. Proceed? [y/N] : ") | ||
print(proceed) | ||
if proceed in ("y", "Y"): | ||
pass | ||
else: | ||
print("Cancel") | ||
return | ||
|
||
engine = create_engine(db_uri) | ||
sess = scoped_session(sessionmaker(bind=engine)) | ||
mileage_list = sess.scalars(select(Mileage)).fetchall() | ||
history_list = [] | ||
print(f"{len(mileage_list)} mileages to save") | ||
for m in mileage_list: | ||
history_list.append(MileageHistory(**m.__dict__)) | ||
|
||
print(f"{len(history_list)}/{len(mileage_list)} moved to history") | ||
if len(history_list) != len(mileage_list): | ||
print("Mileage and history count not match. Abort.") | ||
return | ||
|
||
if dry_run: | ||
print("Dry run. Do not save history and delete mileage.") | ||
else: | ||
sess.add_all(history_list) | ||
sess.commit() | ||
print("History saved. Delete mileage data") | ||
sess.execute(delete(Mileage)) | ||
sess.commit() | ||
print("Mileage deleted. Merge mileage from history.") | ||
|
||
|
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
Oops, something went wrong.