-
Notifications
You must be signed in to change notification settings - Fork 4
/
migration_add_productoffer_avg_price_column.py
executable file
·64 lines (47 loc) · 1.96 KB
/
migration_add_productoffer_avg_price_column.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
argostime_update_prices.py
Standalone script to add new columns added in the sqlalchemy-v2 branch.
Copyright (c) 2023 Martijn <martijn [at] mrtijn.nl>
This file is part of Argostimè.
Argostimè is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Argostimè is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Argostimè. If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from sqlalchemy import text
from sqlalchemy.exc import OperationalError
from argostime import create_app, db
from argostime.models import ProductOffer, Product
app = create_app()
app.app_context().push()
logging.info("Adding average_price column")
try:
db.session.execute(text('ALTER TABLE ProductOffer ADD COLUMN average_price float'))
except OperationalError:
logging.info("Column already seems to exist, fine")
try:
db.session.execute(text('ALTER TABLE ProductOffer ADD COLUMN minimum_price float'))
except OperationalError:
logging.info("Column already seems to exist, fine")
try:
db.session.execute(text('ALTER TABLE ProductOffer ADD COLUMN maximum_price float'))
except OperationalError:
logging.info("Column already seems to exist, fine")
logging.info("Calculate average prices")
offers = db.session.scalars(
db.select(ProductOffer)
.join(Product)
.order_by(Product.name)
).all()
offer: ProductOffer
for offer in offers:
logging.info("Calculating initial memoization values for %s", offer)
offer.update_memoized_values()