Skip to content

Commit

Permalink
Support SQLAlchemy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Dec 6, 2022
1 parent f8a8028 commit fb6ef47
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions beaker/ext/sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ def do_open(self, flags, replace):
if self.loaded:
self.flags = flags
return
select = sa.select([self.table.c.data],
(self.table.c.namespace == self.namespace))
result = self.bind.execute(select).fetchone()
select = sa.select(self.table.c.data).where(self.table.c.namespace == self.namespace)
with self.bind.connect() as conn:
result = conn.execute(select).fetchone()
if not result:
self._is_new = True
self.hash = {}
else:
self._is_new = False
try:
self.hash = result['data']
self.hash = result.data
except (IOError, OSError, EOFError, pickle.PickleError,
pickle.PickleError):
log.debug("Couln't load pickle data, creating new storage")
Expand All @@ -90,19 +90,21 @@ def do_open(self, flags, replace):

def do_close(self):
if self.flags is not None and (self.flags == 'c' or self.flags == 'w'):
if self._is_new:
insert = self.table.insert()
self.bind.execute(insert, namespace=self.namespace, data=self.hash,
accessed=datetime.now(), created=datetime.now())
self._is_new = False
else:
update = self.table.update(self.table.c.namespace == self.namespace)
self.bind.execute(update, data=self.hash, accessed=datetime.now())
with self.bind.begin() as conn:
if self._is_new:
insert = self.table.insert()
conn.execute(insert, dict(namespace=self.namespace, data=self.hash,
accessed=datetime.now(), created=datetime.now()))
self._is_new = False
else:
update = self.table.update().where(self.table.c.namespace == self.namespace)
conn.execute(update, dict(data=self.hash, accessed=datetime.now()))
self.flags = None

def do_remove(self):
delete = self.table.delete(self.table.c.namespace == self.namespace)
self.bind.execute(delete)
delete = self.table.delete().where(self.table.c.namespace == self.namespace)
with self.bind.begin() as conn:
conn.execute(delete)
self.hash = {}
self._is_new = True

Expand Down

0 comments on commit fb6ef47

Please sign in to comment.