Skip to content

Commit

Permalink
sqlalchemy and flask upgrade version adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
atalyaalon committed Jul 25, 2023
1 parent 5a25ad6 commit 3c863ec
Show file tree
Hide file tree
Showing 48 changed files with 1,660 additions and 1,504 deletions.
4 changes: 2 additions & 2 deletions alembic/versions/312c9eb92e40_add_cbs_locations_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def upgrade():
sa.PrimaryKeyConstraint('id')
)
conn = op.get_bind()
conn.execute("""INSERT INTO cbs_locations
conn.execute(sa.text("""INSERT INTO cbs_locations
(SELECT ROW_NUMBER() OVER (ORDER BY road1) as id, LOCATIONS.*
FROM
(SELECT DISTINCT road1,
Expand All @@ -52,7 +52,7 @@ def upgrade():
WHERE (provider_code=1
OR provider_code=3)
AND (longitude is not null
AND latitude is not null)) LOCATIONS)""")
AND latitude is not null)) LOCATIONS)"""))
# ### end Alembic commands ###


Expand Down
9 changes: 5 additions & 4 deletions alembic/versions/4c4b79f8c4a_adding_geom_gix_to_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
depends_on = None

from alembic import op
import sqlalchemy as sa


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

### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('DROP INDEX geom_gix;')
conn.execute('DROP INDEX discussions_gix;')
conn.execute(sa.text('DROP INDEX geom_gix;'))
conn.execute(sa.text('DROP INDEX discussions_gix;'))
### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis_topology;')
conn.execute("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);")
conn.execute('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
conn.execute('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);')
conn.execute("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);")
conn.execute('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
conn.execute('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);')
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis;'))
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis_topology;'))
conn.execute(sa.text("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);"))
conn.execute(sa.text('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
conn.execute(sa.text('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);'))
conn.execute(sa.text("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);"))
conn.execute(sa.text('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
conn.execute(sa.text('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);'))

### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('DROP INDEX idx_markers_geom;')
conn.execute(sa.text('DROP INDEX idx_markers_geom;'))
op.drop_column('markers', 'geom')
conn.execute('DROP INDEX idx_discussions_geom;')
conn.execute(sa.text('DROP INDEX idx_discussions_geom;'))
op.drop_column('discussions', 'geom')
conn.execute('DROP EXTENSION postgis_topology;')
conn.execute('DROP EXTENSION postgis;')
conn.execute('DROP SCHEMA IF EXISTS topology CASCADE;')
conn.execute(sa.text('DROP EXTENSION postgis_topology;'))
conn.execute(sa.text('DROP EXTENSION postgis;'))
conn.execute(sa.text('DROP SCHEMA IF EXISTS topology CASCADE;'))
### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def downgrade():
op.create_index('provider_and_id_idx_involved', 'involved', ['provider_and_id'], unique=False)
op.create_index('provider_and_id_idx_vehicles', 'vehicles', ['provider_and_id'], unique=False)
conn = op.get_bind()
conn.execute('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));')
conn.execute('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));')
conn.execute(sa.text('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));'))
conn.execute(sa.text('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));'))
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions alembic/versions/7f629b4c8891_add_news_flash_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');")
conn.execute("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;")
conn.execute("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;")
conn.execute(sa.text("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');"))
conn.execute(sa.text("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;"))
conn.execute(sa.text("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;"))
op.add_column('news_flash', sa.Column('district_hebrew', sa.Text(), nullable=True))
op.add_column('news_flash', sa.Column('non_urban_intersection_hebrew', sa.Text(), nullable=True))
op.add_column('news_flash', sa.Column('region_hebrew', sa.Text(), nullable=True))
Expand Down
44 changes: 22 additions & 22 deletions anyway/accidents_around_schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from anyway.backend_constants import BE_CONST
from anyway.models import AccidentMarker, Involved, School
from anyway.app_and_db import db
from anyway.app_and_db import db, app

SUBTYPE_ACCIDENT_WITH_PEDESTRIAN = 1
LOCATION_ACCURACY_PRECISE = True
Expand Down Expand Up @@ -48,27 +48,26 @@ def acc_inv_query(longitude, latitude, distance, start_date, end_date, school):
pol_str = "POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))".format(
base_x, base_y, distance_x, distance_y
)

query_obj = (
db.session.query(Involved, AccidentMarker)
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(AccidentMarker.geom.intersects(pol_str))
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(
or_(
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
with app.app_context():
query_obj = (
db.session.query(Involved, AccidentMarker)
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(AccidentMarker.geom.intersects(pol_str))
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(
or_(
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
)
)
)
.filter(AccidentMarker.created >= start_date)
.filter(AccidentMarker.created < end_date)
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
.filter(Involved.age_group.in_([1, 2, 3, 4]))
) # ages 0-19

df = pd.read_sql_query(query_obj.with_labels().statement, db.get_engine())
.filter(AccidentMarker.created >= start_date)
.filter(AccidentMarker.created < end_date)
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
.filter(Involved.age_group.in_([1, 2, 3, 4]))
) # ages 0-19
df = pd.read_sql_query(query_obj.with_labels().statement, db.get_engine())

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

def main(start_date, end_date, distance, output_path):
schools_query = sa.select([School])
df_schools = pd.read_sql_query(schools_query, db.get_engine())
with app.app_context():
df_schools = pd.read_sql_query(schools_query, db.get_engine())
df_total = pd.DataFrame()
df_schools = df_schools.drop_duplicates( # pylint: disable=no-member
["yishuv_name", "longitude", "latitude"]
Expand Down
6 changes: 3 additions & 3 deletions anyway/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
from anyway.app_and_db import db

Base = db.Model
from anyway.app_and_db import db, app
with app.app_context():
Base = db.Model
except ModuleNotFoundError:
from sqlalchemy.ext.declarative.api import declarative_base

Expand Down
10 changes: 5 additions & 5 deletions anyway/db_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def create_markers_hebrew_view(self):
.join(ProviderCode,
and_(AccidentMarker.provider_code == ProviderCode.id),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_involved_hebrew_view(self):
Expand Down Expand Up @@ -501,7 +501,7 @@ def create_involved_hebrew_view(self):
Involved.accident_year == LateDeceased.year,
Involved.provider_code == LateDeceased.provider_code),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_involved_hebrew_markers_hebrew_view(self):
Expand Down Expand Up @@ -681,7 +681,7 @@ def create_involved_hebrew_markers_hebrew_view(self):
InvolvedView.accident_year == VehiclesView.accident_year,
InvolvedView.car_id == VehiclesView.car_id),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_vehicles_hebrew_view(self):
Expand Down Expand Up @@ -748,7 +748,7 @@ def create_vehicles_hebrew_view(self):
Vehicle.accident_year == VehicleDamage.year,
Vehicle.provider_code == VehicleDamage.provider_code),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_vehicles_markers_hebrew_view(self):
Expand Down Expand Up @@ -879,7 +879,7 @@ def create_vehicles_markers_hebrew_view(self):
and_(VehiclesView.provider_code == AccidentMarkerView.provider_code,
VehiclesView.accident_id == AccidentMarkerView.id,
VehiclesView.accident_year == AccidentMarkerView.accident_year))
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

VIEWS = Views()
Loading

0 comments on commit 3c863ec

Please sign in to comment.