Browse Source

Fixing tenant deletes bug

Closes-Bug: 1686809

Change-Id: Ib2a7152ac5187b5b7f941963e94b49b66eb9c224
Kaustubh Phatak 2 years ago
parent
commit
46c6395372
1 changed files with 20 additions and 17 deletions
  1. 20
    17
      mors/persistence.py

+ 20
- 17
mors/persistence.py View File

@@ -13,10 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 13
 See the License for the specific language governing permissions and
14 14
 limitations under the License.
15 15
 """
16
+import functools
17
+import logging
18
+
19
+from sqlalchemy import Table, MetaData
20
+from sqlalchemy import create_engine
16 21
 from sqlalchemy.pool import QueuePool
17
-from sqlalchemy import create_engine, text
18
-from sqlalchemy import Table, Column, Integer, String, MetaData, DateTime
19
-import logging, functools
20 22
 
21 23
 logger = logging.getLogger(__name__)
22 24
 
@@ -25,7 +27,7 @@ def db_connect(transaction=False):
25 27
     """
26 28
     Generates a decorator that get connection from a pool and returns
27 29
     it to the pool when the internal function is done
28
-    :param transaction bool: should this function create and end transaction.
30
+    :param transaction: (boolean) should this function create and end transaction.
29 31
     """
30 32
 
31 33
     def _db_connect(fun):
@@ -36,6 +38,7 @@ def db_connect(transaction=False):
36 38
 
37 39
         @functools.wraps(fun)
38 40
         def newfun(self, *args, **kwargs):
41
+            trans = None
39 42
             conn = self.engine.connect()
40 43
             if transaction:
41 44
                 trans = conn.begin()
@@ -44,7 +47,7 @@ def db_connect(transaction=False):
44 47
                 if transaction:
45 48
                     trans.commit()
46 49
                 return ret
47
-            except Exception as e:
50
+            except Exception:
48 51
                 if transaction:
49 52
                     trans.rollback()
50 53
                 logger.exception("Error during transaction ")
@@ -81,26 +84,26 @@ class DbPersistence:
81 84
     @db_connect(transaction=True)
82 85
     def update_tenant_lease(self, conn, tenant_uuid, expiry_mins, updated_by, updated_at):
83 86
         logger.debug("Updating tenant lease %s %d %s %s", tenant_uuid, expiry_mins, str(updated_at), updated_by)
84
-        conn.execute(self.tenant_lease.update().\
85
-                            where(self.tenant_lease.c.tenant_uuid == tenant_uuid).\
86
-                                    values(expiry_mins=expiry_mins,
87
-                                           updated_at=updated_at, updated_by=updated_by))
87
+        conn.execute(self.tenant_lease.update().where(
88
+            self.tenant_lease.c.tenant_uuid == tenant_uuid).
89
+                     values(expiry_mins=expiry_mins,
90
+                            updated_at=updated_at, updated_by=updated_by))
88 91
 
89 92
     @db_connect(transaction=True)
90 93
     def delete_tenant_lease(self, conn, tenant_uuid):
91 94
         # Should we just soft delete ?
92 95
         logger.debug("Deleting tenant lease %s", tenant_uuid)
93
-        conn.execute(self.tenant_lease.delete().where(tenant_uuid == tenant_uuid))
94
-        conn.execute(self.instance_lease.delete().where(tenant_uuid == tenant_uuid))
96
+        conn.execute(self.tenant_lease.delete().where(self.tenant_lease.c.tenant_uuid == tenant_uuid))
97
+        conn.execute(self.instance_lease.delete().where(self.instance_lease.c.tenant_uuid == tenant_uuid))
95 98
 
96 99
     @db_connect(transaction=False)
97 100
     def get_instance_leases_by_tenant(self, conn, tenant_uuid):
98
-        return conn.execute(self.instance_lease.select(\
101
+        return conn.execute(self.instance_lease.select(
99 102
                 self.instance_lease.c.tenant_uuid == tenant_uuid)).fetchall()
100 103
  
101 104
     @db_connect(transaction=False)
102 105
     def get_instance_lease(self, conn, instance_uuid):
103
-        return conn.execute(self.instance_lease.select((\
106
+        return conn.execute(self.instance_lease.select((
104 107
                         self.instance_lease.c.instance_uuid == instance_uuid))).first()
105 108
 
106 109
     @db_connect(transaction=True)
@@ -113,10 +116,10 @@ class DbPersistence:
113 116
     @db_connect(transaction=True)
114 117
     def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at):
115 118
         logger.debug("Updating instance lease %s %s %s %s", instance_uuid, tenant_uuid, expiry, updated_by)
116
-        conn.execute(self.instance_lease.update().\
117
-                        where(self.instance_lease.c.instance_uuid == instance_uuid).\
118
-                        values(tenant_uuid=tenant_uuid, expiry=expiry,
119
-                               updated_at=updated_at, updated_by=updated_by))
119
+        conn.execute(self.instance_lease.update().where(
120
+            self.instance_lease.c.instance_uuid == instance_uuid).values
121
+                     (tenant_uuid=tenant_uuid, expiry=expiry,
122
+                      updated_at=updated_at, updated_by=updated_by))
120 123
 
121 124
     @db_connect(transaction=True)
122 125
     def delete_instance_leases(self, conn, instance_uuids):

Loading…
Cancel
Save