Skip to content

Commit 2f2e49a

Browse files
committed
sqlalchemy and flask upgrade version adjustments
1 parent 5a25ad6 commit 2f2e49a

47 files changed

Lines changed: 1655 additions & 1499 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

alembic/versions/312c9eb92e40_add_cbs_locations_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def upgrade():
3434
sa.PrimaryKeyConstraint('id')
3535
)
3636
conn = op.get_bind()
37-
conn.execute("""INSERT INTO cbs_locations
37+
conn.execute(sa.text("""INSERT INTO cbs_locations
3838
(SELECT ROW_NUMBER() OVER (ORDER BY road1) as id, LOCATIONS.*
3939
FROM
4040
(SELECT DISTINCT road1,
@@ -52,7 +52,7 @@ def upgrade():
5252
WHERE (provider_code=1
5353
OR provider_code=3)
5454
AND (longitude is not null
55-
AND latitude is not null)) LOCATIONS)""")
55+
AND latitude is not null)) LOCATIONS)"""))
5656
# ### end Alembic commands ###
5757

5858

alembic/versions/4c4b79f8c4a_adding_geom_gix_to_markers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@
1212
depends_on = None
1313

1414
from alembic import op
15+
import sqlalchemy as sa
1516

1617

1718
def upgrade():
1819
### commands auto generated by Alembic - please adjust! ###
1920
conn = op.get_bind()
20-
conn.execute('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));')
21-
conn.execute('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));')
21+
conn.execute(sa.text('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));'))
22+
conn.execute(sa.text('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));'))
2223

2324
### end Alembic commands ###
2425

2526

2627
def downgrade():
2728
### commands auto generated by Alembic - please adjust! ###
2829
conn = op.get_bind()
29-
conn.execute('DROP INDEX geom_gix;')
30-
conn.execute('DROP INDEX discussions_gix;')
30+
conn.execute(sa.text('DROP INDEX geom_gix;'))
31+
conn.execute(sa.text('DROP INDEX discussions_gix;'))
3132
### end Alembic commands ###

alembic/versions/5a5ffe56bb7_adding_geom_table_to_markers_and_discussions.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,32 @@
1212
depends_on = None
1313

1414
from alembic import op
15+
import sqlalchemy as sa
1516

1617

1718
def upgrade():
1819
### commands auto generated by Alembic - please adjust! ###
1920
conn = op.get_bind()
20-
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
21-
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis_topology;')
22-
conn.execute("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);")
23-
conn.execute('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
24-
conn.execute('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);')
25-
conn.execute("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);")
26-
conn.execute('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
27-
conn.execute('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);')
21+
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis;'))
22+
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis_topology;'))
23+
conn.execute(sa.text("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);"))
24+
conn.execute(sa.text('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
25+
conn.execute(sa.text('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);'))
26+
conn.execute(sa.text("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);"))
27+
conn.execute(sa.text('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
28+
conn.execute(sa.text('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);'))
2829

2930
### end Alembic commands ###
3031

3132

3233
def downgrade():
3334
### commands auto generated by Alembic - please adjust! ###
3435
conn = op.get_bind()
35-
conn.execute('DROP INDEX idx_markers_geom;')
36+
conn.execute(sa.text('DROP INDEX idx_markers_geom;'))
3637
op.drop_column('markers', 'geom')
37-
conn.execute('DROP INDEX idx_discussions_geom;')
38+
conn.execute(sa.text('DROP INDEX idx_discussions_geom;'))
3839
op.drop_column('discussions', 'geom')
39-
conn.execute('DROP EXTENSION postgis_topology;')
40-
conn.execute('DROP EXTENSION postgis;')
41-
conn.execute('DROP SCHEMA IF EXISTS topology CASCADE;')
40+
conn.execute(sa.text('DROP EXTENSION postgis_topology;'))
41+
conn.execute(sa.text('DROP EXTENSION postgis;'))
42+
conn.execute(sa.text('DROP SCHEMA IF EXISTS topology CASCADE;'))
4243
### end Alembic commands ###

alembic/versions/7574885e1fed_remove_unecessary_table_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ def downgrade():
3434
op.create_index('provider_and_id_idx_involved', 'involved', ['provider_and_id'], unique=False)
3535
op.create_index('provider_and_id_idx_vehicles', 'vehicles', ['provider_and_id'], unique=False)
3636
conn = op.get_bind()
37-
conn.execute('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));')
38-
conn.execute('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));')
37+
conn.execute(sa.text('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));'))
38+
conn.execute(sa.text('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));'))
3939
# ### end Alembic commands ###

alembic/versions/7f629b4c8891_add_news_flash_fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
def upgrade():
2020
# ### commands auto generated by Alembic - please adjust! ###
2121
conn = op.get_bind()
22-
conn.execute("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');")
23-
conn.execute("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;")
24-
conn.execute("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;")
22+
conn.execute(sa.text("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');"))
23+
conn.execute(sa.text("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;"))
24+
conn.execute(sa.text("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;"))
2525
op.add_column('news_flash', sa.Column('district_hebrew', sa.Text(), nullable=True))
2626
op.add_column('news_flash', sa.Column('non_urban_intersection_hebrew', sa.Text(), nullable=True))
2727
op.add_column('news_flash', sa.Column('region_hebrew', sa.Text(), nullable=True))

anyway/accidents_around_schools.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from anyway.backend_constants import BE_CONST
99
from anyway.models import AccidentMarker, Involved, School
10-
from anyway.app_and_db import db
10+
from anyway.app_and_db import db, app
1111

1212
SUBTYPE_ACCIDENT_WITH_PEDESTRIAN = 1
1313
LOCATION_ACCURACY_PRECISE = True
@@ -48,27 +48,26 @@ def acc_inv_query(longitude, latitude, distance, start_date, end_date, school):
4848
pol_str = "POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))".format(
4949
base_x, base_y, distance_x, distance_y
5050
)
51-
52-
query_obj = (
53-
db.session.query(Involved, AccidentMarker)
54-
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
55-
.filter(AccidentMarker.geom.intersects(pol_str))
56-
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
57-
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
58-
.filter(
59-
or_(
60-
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
61-
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
51+
with app.app_context():
52+
query_obj = (
53+
db.session.query(Involved, AccidentMarker)
54+
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
55+
.filter(AccidentMarker.geom.intersects(pol_str))
56+
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
57+
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
58+
.filter(
59+
or_(
60+
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
61+
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
62+
)
6263
)
63-
)
64-
.filter(AccidentMarker.created >= start_date)
65-
.filter(AccidentMarker.created < end_date)
66-
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
67-
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
68-
.filter(Involved.age_group.in_([1, 2, 3, 4]))
69-
) # ages 0-19
70-
71-
df = pd.read_sql_query(query_obj.with_labels().statement, db.get_engine())
64+
.filter(AccidentMarker.created >= start_date)
65+
.filter(AccidentMarker.created < end_date)
66+
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
67+
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
68+
.filter(Involved.age_group.in_([1, 2, 3, 4]))
69+
) # ages 0-19
70+
df = pd.read_sql_query(query_obj.with_labels().statement, db.get_engine())
7271

7372
if LOCATION_ACCURACY_PRECISE:
7473
location_accurate = 1
@@ -110,7 +109,8 @@ def acc_inv_query(longitude, latitude, distance, start_date, end_date, school):
110109

111110
def main(start_date, end_date, distance, output_path):
112111
schools_query = sa.select([School])
113-
df_schools = pd.read_sql_query(schools_query, db.get_engine())
112+
with app.app_context():
113+
df_schools = pd.read_sql_query(schools_query, db.get_engine())
114114
df_total = pd.DataFrame()
115115
df_schools = df_schools.drop_duplicates( # pylint: disable=no-member
116116
["yishuv_name", "longitude", "latitude"]

anyway/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
try:
2-
from anyway.app_and_db import db
3-
4-
Base = db.Model
2+
from anyway.app_and_db import db, app
3+
with app.app_context():
4+
Base = db.Model
55
except ModuleNotFoundError:
66
from sqlalchemy.ext.declarative.api import declarative_base
77

0 commit comments

Comments
 (0)