Skip to content

Commit

Permalink
(PC-31172)[API] feat: fill offer oa
Browse files Browse the repository at this point in the history
  • Loading branch information
anougbele-pass committed Sep 20, 2024
1 parent ddc5c2e commit 366c905
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions api/src/pcapi/scripts/fill_offer_address/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import argparse
import datetime
import logging
import time

import sqlalchemy as sa

from pcapi.app import app
from pcapi.core.offerers.models import Venue
from pcapi.models import db


app.app_context().push()

logger = logging.getLogger(__name__)


def fill_offer_oa(min_id: int, max_id: int, dry_run: bool = True, do_vaccum: bool = False) -> None:
venues_list = (
Venue.query.filter(Venue.isVirtual == False, Venue.id >= min_id, Venue.id < max_id)
.options(sa.orm.load_only(Venue.id, Venue.offererAddressId))
.all()
)
for venue in venues_list:
db.session.execute(
sa.text(
"""
UPDATE offer SET "offererAddressId"=:offererAddressId WHERE "venueId"=:venueId;
"""
),
{"offererAddressId": venue.offererAddressId, "venueId": venue.id},
)
if not dry_run:
db.session.commit()
logger.info("Updated offererAddressId for venueId: %s to %s", venue.id, venue.offererAddressId)
if not dry_run:
if do_vaccum:
start = time.time()
logger.info("Vaccum started at %s", datetime.datetime.utcnow())
logger.info("VACUUM ANALYZE offer")
db.session.execute(sa.text("COMMIT"))
db.session.execute(sa.text("VACUUM ANALYZE offer"))
logger.info("VACUUM ANALYZE offer took %s seconds", time.time() - start)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fill offer address")
parser.add_argument("--dry-run", action=argparse.BooleanOptionalAction, default=True)
parser.add_argument("--min-id", type=int, default=0)
parser.add_argument("--max-id", type=int, default=0)
parser.add_argument("--do-vaccummm", action=argparse.BooleanOptionalAction, default=False)

args = parser.parse_args()
try:
fill_offer_oa(args.min_id, args.max_id, args.dry_run, args.do_vaccum)
print("finished")
except:
db.session.rollback()
raise
if args.dry_run:
db.session.rollback()

0 comments on commit 366c905

Please sign in to comment.