diff options
| -rw-r--r-- | fietsboek/actions.py | 4 | ||||
| -rw-r--r-- | fietsboek/models/__init__.py | 13 | ||||
| -rw-r--r-- | fietsboek/models/track.py | 16 | ||||
| -rw-r--r-- | tests/integration/test_browse.py | 11 |
4 files changed, 36 insertions, 8 deletions
diff --git a/fietsboek/actions.py b/fietsboek/actions.py index bb6cafd..2d23d97 100644 --- a/fietsboek/actions.py +++ b/fietsboek/actions.py @@ -108,11 +108,11 @@ def add_track( # Best time to build the cache is right after the upload, but *after* the # transformers have been applied! - track.ensure_cache() + track.ensure_cache(path) dbsession.add(track.cache) LOGGER.debug("Building preview image for %s", track.id) - preview_image = trackmap.render(track.path(), layer, tile_requester) + preview_image = trackmap.render(path, layer, tile_requester) image_io = io.BytesIO() preview_image.save(image_io, "PNG") manager.set_preview(image_io.getvalue()) diff --git a/fietsboek/models/__init__.py b/fietsboek/models/__init__.py index c70fee1..85664ca 100644 --- a/fietsboek/models/__init__.py +++ b/fietsboek/models/__init__.py @@ -5,7 +5,7 @@ access the submodules if you need some of the auxiliary definitions. """ import zope.sqlalchemy -from sqlalchemy import engine_from_config +from sqlalchemy import engine_from_config, event from sqlalchemy.orm import configure_mappers, sessionmaker from .badge import Badge # flake8: noqa @@ -24,7 +24,16 @@ configure_mappers() def get_engine(settings, prefix="sqlalchemy."): """Create an SQL Engine from the given settings.""" - return engine_from_config(settings, prefix) + engine = engine_from_config(settings, prefix) + + # SQLite is quite loose with foreign keys by default, so make sure it + # checks them. + def _fk_pragma_on_connect(dbapi_con, _con_record): + dbapi_con.execute("PRAGMA foreign_keys=ON;") + + if engine.dialect.name == "sqlite": + event.listen(engine, "connect", _fk_pragma_on_connect) + return engine def get_session_factory(engine): diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index ab9d698..3bf7eee 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -522,12 +522,22 @@ class Track(Base): result = ACLHelper().permits(self, principals, "track.view") return isinstance(result, ACLAllowed) - def ensure_cache(self): - """Ensure that a cached version of this track's metadata exists.""" + def ensure_cache(self, path: geo.Path | None = None): + """Ensure that a cached version of this track's metadata exists. + + If ``path`` is given, it is used as the basis for the calculation. This + is useful if you have already loaded the path, to avoid a database + access. + + :param path: The path, if preloaded. Otherwise, ``self.path()`` will be + used. + """ if self.cache is not None: return self.cache = TrackCache() - meta = self.path().movement_data() + if path is None: + path = self.path() + meta = path.movement_data() self.cache.length = meta.length self.cache.uphill = meta.uphill self.cache.downhill = meta.downhill diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 68ca1e7..68ead8f 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -23,6 +23,8 @@ def added_tracks(tm, dbsession, owner, data_manager): # objects to the database. tm.abort() + path = convert.smart_convert(load_gpx_asset("Teasi_1.gpx.gz")).path() + tracks = [] track_ids = [] with tm: @@ -53,9 +55,11 @@ def added_tracks(tm, dbsession, owner, data_manager): tagged_people=[], ) track.date = datetime(2022, 10, 29, 13, 37, 11) - track.set_path(convert.smart_convert(load_gpx_asset("Teasi_1.gpx.gz")).path()) dbsession.add(track) dbsession.flush() + track.fast_set_path(path) + track.ensure_cache(path) + dbsession.add(track.cache) data_manager.initialize(track.id) tracks.append(track) track_ids.append(track.id) @@ -108,6 +112,8 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager): dbsession.add(track) dbsession.flush() track.fast_set_path(path) + track.ensure_cache(path) + dbsession.add(track.cache) tracks.append(track) track_ids.append(track.id) data_manager.initialize(track.id) @@ -123,6 +129,9 @@ def a_lot_of_tracks(tm, dbsession, owner, data_manager): with tm: for track_id in track_ids: dbsession.execute(delete(table).where(table.c.track_id == track_id)) + dbsession.execute( + delete(models.TrackCache).where(models.TrackCache.track_id == track_id) + ) dbsession.execute(delete(models.Track).where(models.Track.id == track_id)) tm.begin() tm.doom() |
