]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Call get_session() only when necessary
authorRoman Podolyaka <rpodolyaka@mirantis.com>
Wed, 24 Jul 2013 14:13:07 +0000 (17:13 +0300)
committerRoman Podolyaka <rpodolyaka@mirantis.com>
Thu, 1 Aug 2013 19:23:06 +0000 (22:23 +0300)
A Session instance should be retrieved only when it's
really needed, e. g. to make a few DB queries inside a
single transaction or to pass the session to a private
DB API method, etc. At the same time, many queries are
simple and can be expressed by the means  of auxiliary
model_query() function.

Blueprint: db-session-cleanup

Change-Id: I2ddc0cda42a84fe600c04f4624db9c2933799bfc

cinder/db/sqlalchemy/api.py

index 749b36eda906570b1241d331ded3683ce7f03a54..2a2bed9e9a2ce95ebc84ee2910e8ad289968f46f 100644 (file)
@@ -1425,9 +1425,8 @@ def snapshot_data_get_for_project(context, project_id, volume_type_id=None,
 @require_context
 def snapshot_get_active_by_window(context, begin, end=None, project_id=None):
     """Return snapshots that were active during window."""
-    session = get_session()
-    query = session.query(models.Snapshot)
 
+    query = model_query(context, models.Snapshot, read_deleted="yes")
     query = query.filter(or_(models.Snapshot.deleted_at == None,
                              models.Snapshot.deleted_at > begin))
     if end:
@@ -1668,9 +1667,7 @@ def volume_get_active_by_window(context,
                                 end=None,
                                 project_id=None):
     """Return volumes that were active during window."""
-    session = get_session()
-    query = session.query(models.Volume)
-
+    query = model_query(context, models.Volume, read_deleted="yes")
     query = query.filter(or_(models.Volume.deleted_at == None,
                              models.Volume.deleted_at > begin))
     if end:
@@ -1894,10 +1891,8 @@ def volume_glance_metadata_copy_to_volume(context, volume_id, snapshot_id):
 
 @require_context
 def volume_glance_metadata_delete_by_volume(context, volume_id):
-    session = get_session()
-    session.query(models.VolumeGlanceMetadata).\
+    model_query(context, models.VolumeGlanceMetadata, read_deleted='no').\
         filter_by(volume_id=volume_id).\
-        filter_by(deleted=False).\
         update({'deleted': True,
                 'deleted_at': timeutils.utcnow(),
                 'updated_at': literal_column('updated_at')})
@@ -1905,10 +1900,8 @@ def volume_glance_metadata_delete_by_volume(context, volume_id):
 
 @require_context
 def volume_glance_metadata_delete_by_snapshot(context, snapshot_id):
-    session = get_session()
-    session.query(models.VolumeGlanceMetadata).\
+    model_query(context, models.VolumeGlanceMetadata, read_deleted='no').\
         filter_by(snapshot_id=snapshot_id).\
-        filter_by(deleted=False).\
         update({'deleted': True,
                 'deleted_at': timeutils.utcnow(),
                 'updated_at': literal_column('updated_at')})
@@ -1976,14 +1969,12 @@ def backup_update(context, backup_id, values):
 
 @require_admin_context
 def backup_destroy(context, backup_id):
-    session = get_session()
-    with session.begin():
-        session.query(models.Backup).\
-            filter_by(id=backup_id).\
-            update({'status': 'deleted',
-                    'deleted': True,
-                    'deleted_at': timeutils.utcnow(),
-                    'updated_at': literal_column('updated_at')})
+    model_query(context, models.Backup).\
+        filter_by(id=backup_id).\
+        update({'status': 'deleted',
+                'deleted': True,
+                'deleted_at': timeutils.utcnow(),
+                'updated_at': literal_column('updated_at')})
 
 
 ###############################