From 70ffd1a05cee6e2d78db318b3d4ce45fa691dc0e Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Fri, 21 Jan 2022 18:15:49 +0100
Subject: [PATCH] Revert short-lived session. #90

---
 src/scheduling/models.py | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/src/scheduling/models.py b/src/scheduling/models.py
index 4b2ccffa..b014727c 100644
--- a/src/scheduling/models.py
+++ b/src/scheduling/models.py
@@ -64,10 +64,15 @@ if __sqlalchemy_version < (1, 4):
         see https://docs.sqlalchemy.org/en/13/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it
         """
         session = scoped_session(DB.session_factory)
-        try:
-            yield session
-        finally:
-            session.close()
+
+        # Commented out because of https://gitlab.servus.at/aura/engine/-/issues/90
+        # try:
+        #     yield session
+        # finally:
+        #     session.close()
+
+        yield session
+
 
     DB.Session = get_session_context
 
@@ -192,27 +197,27 @@ class Timeslot(DB.Model, AuraDatabaseModel):
     playlist = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.playlist_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
     default_schedule_playlist = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.default_schedule_playlist_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
     default_show_playlist = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.default_show_playlist_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
     schedule_fallback = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.schedule_fallback_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
     show_fallback = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.show_fallback_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
     station_fallback = relationship("Playlist",
                             primaryjoin="and_(Timeslot.timeslot_start==Playlist.timeslot_start, \
                                 Timeslot.station_fallback_id==Playlist.playlist_id, Timeslot.show_name==Playlist.show_name)",
-                            uselist=False, back_populates="timeslot")
+                            uselist=False, back_populates="timeslot", lazy='subquery')
 
     playlist_id = Column(Integer)
     default_schedule_playlist_id = Column(Integer)
@@ -376,8 +381,8 @@ class Playlist(DB.Model, AuraDatabaseModel):
     timeslot_start = Column(DateTime, ForeignKey("timeslot.timeslot_start"))
 
     # Relationships
-    timeslot = relationship("Timeslot", uselist=False, back_populates="playlist")
-    entries = relationship("PlaylistEntry", back_populates="playlist")
+    timeslot = relationship("Timeslot", uselist=False, back_populates="playlist", lazy='subquery')
+    entries = relationship("PlaylistEntry", back_populates="playlist", lazy='subquery')
 
     # Data
     playlist_id = Column(Integer, autoincrement=False)
@@ -535,8 +540,8 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel):
     artificial_playlist_id = Column(Integer, ForeignKey("playlist.artificial_id"))
 
     # Relationships
-    playlist = relationship("Playlist", uselist=False, back_populates="entries")
-    meta_data = relationship("PlaylistEntryMetaData", uselist=False, back_populates="entry")
+    playlist = relationship("Playlist", uselist=False, back_populates="entries", lazy='subquery')
+    meta_data = relationship("PlaylistEntryMetaData", uselist=False, back_populates="entry", lazy='subquery')
 
     # Data
     entry_num = Column(Integer)
@@ -683,7 +688,7 @@ class PlaylistEntryMetaData(DB.Model, AuraDatabaseModel):
     artificial_entry_id = Column(Integer, ForeignKey("playlist_entry.artificial_id"))
 
     # Relationships
-    entry = relationship("PlaylistEntry", uselist=False, back_populates="meta_data")
+    entry = relationship("PlaylistEntry", uselist=False, back_populates="meta_data", lazy='subquery')
 
     # Data
     artist = Column(String(256))
-- 
GitLab