Skip to content

Commit bf011e1

Browse files
committed
sqlalchemy upgrade version adjustments
1 parent 5a25ad6 commit bf011e1

8 files changed

Lines changed: 45 additions & 39 deletions

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/parsers/infographics_data_cache_updater.py

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

33
from datetime import datetime
44
from sqlalchemy import not_
5+
import sqlalchemy as sa
56
from anyway.models import (
67
Base,
78
InfographicsDataCache,
@@ -214,13 +215,15 @@ def copy_temp_into_cache(table: Dict[str, Base]):
214215
db.session.commit()
215216
start = datetime.now()
216217
with db.get_engine().begin() as conn:
217-
conn.execute("lock table infographics_data_cache in exclusive mode")
218+
conn.execute(sa.text("lock table infographics_data_cache in exclusive mode"))
218219
logging.debug(f"in transaction, after lock")
219-
conn.execute(f"delete from {table[CACHE].__tablename__}")
220+
conn.execute(sa.text(f"delete from {table[CACHE].__tablename__}"))
220221
logging.debug(f"in transaction, after delete")
221222
conn.execute(
222-
f"insert into {table[CACHE].__tablename__} "
223-
f"SELECT * from {table[TEMP].__tablename__}"
223+
sa.text(
224+
f"insert into {table[CACHE].__tablename__} "
225+
f"SELECT * from {table[TEMP].__tablename__}"
226+
)
224227
)
225228
logging.debug(f"in transaction, after insert into")
226229
logging.info(f"cache unavailable time: {str(datetime.now() - start)}")

anyway/parsers/registered.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import datetime
88
from anyway.models import RegisteredVehicle, DeprecatedCity
99
from anyway.utilities import time_delta, CsvReader, ImporterUI, truncate_tables, decode_hebrew
10-
from anyway.app_and_db import db
10+
from anyway.app_and_db import db, app
1111

1212

1313
COLUMN_CITY_NAME_ENG = 0
@@ -71,8 +71,8 @@ def import_file(self, inputfile):
7171
else:
7272
self.header_row(row)
7373
row_count += 1
74-
75-
db.session.bulk_insert_mappings(RegisteredVehicle, inserts)
74+
with app.app_context():
75+
db.session.bulk_insert_mappings(RegisteredVehicle, inserts)
7676
return total
7777

7878
@staticmethod
@@ -131,11 +131,11 @@ def main(specific_folder, delete_all, path):
131131
started = datetime.now()
132132
for fname in dir_files:
133133
total += importer.import_file(fname)
134-
135-
db.session.commit()
136-
db.engine.execute(
137-
"UPDATE {0} SET city_id = (SELECT id FROM {1} WHERE {0}.search_name = {1}.search_heb) WHERE city_id IS NULL".format(
138-
RegisteredVehicle.__tablename__, DeprecatedCity.__tablename__
134+
with app.app_context():
135+
db.session.commit()
136+
db.engine.execute(
137+
"UPDATE {0} SET city_id = (SELECT id FROM {1} WHERE {0}.search_name = {1}.search_heb) WHERE city_id IS NULL".format(
138+
RegisteredVehicle.__tablename__, DeprecatedCity.__tablename__
139+
)
139140
)
140-
)
141-
logging.info("Total: {0} items in {1}".format(total, time_delta(started)))
141+
logging.info("Total: {0} items in {1}".format(total, time_delta(started)))

anyway/utilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from urllib.parse import urlparse
1313
from sqlalchemy import func, or_
1414
from sqlalchemy.sql import select
15+
import sqlalchemy as sa
1516

1617
import phonenumbers
1718
from dateutil.relativedelta import relativedelta
@@ -196,7 +197,7 @@ def truncate_tables(db, tables):
196197
def delete_all_rows_from_table(conn, table):
197198
table_name = table.__tablename__
198199
logging.info("Deleting all rows from table " + table_name)
199-
conn.execute("DELETE FROM " + table_name)
200+
conn.execute(sa.text("DELETE FROM " + table_name))
200201

201202

202203
def split_query_to_chunks_by_column(base_select, column_to_chunk_by, chunk_size, conn):

0 commit comments

Comments
 (0)