- Timestamp:
- Apr 7, 2020, 5:27:29 PM (7 years ago)
- Branches:
- debian/bullseye, master
- Children:
- cc107a8
- Parents:
- 8f3d1dd
- Location:
- src/mapservice
- Files:
-
- 3 edited
-
codegen/templates/__init__test.mustache (modified) (2 diffs)
-
src/server_impl/__init__.py (modified) (2 diffs)
-
src/server_impl/database/Model.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/mapservice/codegen/templates/__init__test.mustache
r8f3d1dd r5ec7c42 7 7 8 8 from {{packageName}}.encoder import JSONEncoder 9 from {{packageName}}.server_impl.database.Model import db, db_test_setup 10 11 def load_spatialite(dbapi_conn, connection_record): 12 print("enable spatial lite extension") 13 # From https://geoalchemy-2.readthedocs.io/en/latest/spatialite_tutorial.html 14 dbapi_conn.enable_load_extension(True) 15 dbapi_conn.load_extension('/usr/local/lib64/mod_spatialite') 9 from {{packageName}}.server_impl.database.Model import db, db_setup, load_spatialite 10 from {{packageName}}.server_impl.demo import DbSetup as demoDbSetup 16 11 17 12 class BaseTestCase(TestCase): … … 33 28 listen(db.engine, 'connect', load_spatialite) 34 29 db.drop_all() 35 db_test_setup() 30 # create the tables whether the database already exists or not 31 db_setup(False) 32 demoDbSetup.setup() 36 33 37 34 def tearDown(self): -
src/mapservice/src/server_impl/__init__.py
r8f3d1dd r5ec7c42 26 26 27 27 from .. import encoder 28 from .database.Model import db, User, ApiKey, db_setup 28 from .database.Model import db, User, ApiKey, db_setup, load_spatialite 29 29 from .ValidatorWorkaround import ValidatorWorkaround 30 30 … … 51 51 def create_database(): 52 52 @event.listens_for(db.engine, "connect") 53 def load_spatialite(dbapi_conn, connection_record): 54 if 'sqlite' in flask.current_app.config['SQLALCHEMY_DATABASE_URI']: 55 # From https://geoalchemy-2.readthedocs.io/en/latest/spatialite_tutorial.html 56 dbapi_conn.enable_load_extension(True) 57 dbapi_conn.load_extension('/usr/local/lib64/mod_spatialite') 53 def load_spatialite_extension(dbapi_conn, connection_record): 54 load_spatialite(dbapi_conn, connection_record) 58 55 59 56 if flask.current_app.config['CREATE_TEST_DATABASE'] == True: -
src/mapservice/src/server_impl/database/Model.py
r8f3d1dd r5ec7c42 27 27 import enum 28 28 import connexion 29 from pathlib import Path 30 import sys 31 import os 29 32 30 33 db = SQLAlchemy() … … 359 362 from_obj=(Log.__table__.join(LogEntry, Log.id == LogEntry.log_id).join(ProcessingStatus, Log.processing_state == ProcessingStatus.id)), 360 363 group_by=(Log.id) 361 ) 364 ), 365 cascade_on_drop=False 362 366 ) 363 367 … … 370 374 log = db.relationship("Log", backref=db.backref("queue"), lazy=True) 371 375 372 def db_setup( ):373 if not database_exists(db.engine.url) :376 def db_setup(if_not_exists = True): 377 if not database_exists(db.engine.url) or not if_not_exists: 374 378 create_database(db.engine.url) 375 379 if 'sqlite' in flask.current_app.config['SQLALCHEMY_DATABASE_URI']: 376 print("Initiate spatial meta data for Spatialite...") 377 db.engine.execute(select([func.InitSpatialMetaData()])) 378 print("...Finished") 380 result = db.engine.execute('SELECT count(name) FROM sqlite_master WHERE type="table" AND name="spatial_ref_sys"') 381 if result.fetchone()[0] == 1: 382 print('Spatial lite extension already loaded') 383 else: 384 print("Initiate spatial meta data for Spatialite...") 385 db.engine.execute(select([func.InitSpatialMetaData()])) 386 print("...Finished") 379 387 db.create_all() 380 388 ProcessingStatus.insert_states() 389 390 def load_spatialite(dbapi_conn, connection_record): 391 if not 'sqlite' in flask.current_app.config['SQLALCHEMY_DATABASE_URI']: 392 return 393 394 paths = [os.path.join(sys.prefix, 'lib'), os.path.join(sys.prefix, 'lib64'), '/usr/lib', '/usr/lib64'] 395 for path in paths: 396 if _load_spatialite_from_path(path, dbapi_conn, connection_record): 397 return 398 raise FileNotFoundError('mod_spatialite not found in folders {}'.format(', '.join(paths))) 399 400 def _load_spatialite_from_path(search_path, dbapi_conn, connection_record): 401 for path in Path(search_path).rglob('mod_spatialite.so*'): 402 if os.path.exists(path): 403 print('Enabling spatial lite extension. This may take a while') 404 # From https://geoalchemy-2.readthedocs.io/en/latest/spatialite_tutorial.html 405 dbapi_conn.enable_load_extension(True) 406 dbapi_conn.load_extension(str(path.resolve())) 407 return True 408 return False
Note:
See TracChangeset
for help on using the changeset viewer.
