-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddc5c2e
commit 366c905
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
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,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() |