FIX session usage in DB (but in the dirtier way)

This commit is contained in:
boyska 2014-04-08 18:03:08 +02:00
parent 98626b6099
commit e42e1a3af5
3 changed files with 23 additions and 11 deletions

View file

@ -59,6 +59,7 @@ def mp3_join(named_intervals, target):
This function make the (quite usual) assumption that the only start_cut (if This function make the (quite usual) assumption that the only start_cut (if
any) is at the first file, and the last one is at the last file any) is at the first file, and the last one is at the last file
''' '''
# TODO: ffmpeg binary should be configurable
ffmpeg = 'ffmpeg' # binary name ffmpeg = 'ffmpeg' # binary name
startskip = None startskip = None
endskip = None endskip = None

View file

@ -1,6 +1,8 @@
import sys import sys
import logging import logging
from sqlalchemy import inspect
from config_manager import get_config from config_manager import get_config
from techrec import RecDB from techrec import RecDB
@ -16,7 +18,10 @@ def cleanold_cmd(options):
else: else:
for rec in res: for rec in res:
logging.info("Deleting " + str(rec)) logging.info("Deleting " + str(rec))
db.session.delete(rec) s = inspect(rec).session
db.commit() s.delete(rec)
s.commit()
logging.info("Cleanold complete: %d deleted" % count) logging.info("Cleanold complete: %d deleted" % count)
sys.exit(0) sys.exit(0)
# vim: set ai ts=4 sw=4 et:

View file

@ -4,7 +4,7 @@ from datetime import datetime, timedelta
import sys import sys
from sqlalchemy import create_engine, Column, Integer, String, DateTime from sqlalchemy import create_engine, Column, Integer, String, DateTime, inspect
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -72,8 +72,9 @@ class RecDB:
self.err = "" self.err = ""
def add(self, simplerecord): def add(self, simplerecord):
print self.session.add( simplerecord ) s = self.get_session()
self.commit() s.add( simplerecord )
s.commit()
self.log.info("New Record: %s" % simplerecord) self.log.info("New Record: %s" % simplerecord)
return ( simplerecord ) return ( simplerecord )
@ -94,7 +95,7 @@ class RecDB:
_rlist[0].name = rec["name"] _rlist[0].name = rec["name"]
self.log.debug("DB:: Update: data AFTER %s" % _rlist[0]) self.log.debug("DB:: Update: data AFTER %s" % _rlist[0])
self.commit() inspect(_rlist[0]).session.commit()
self.log.debug("DB:: Update complete") self.log.debug("DB:: Update complete")
return _rlist[0] return _rlist[0]
@ -112,15 +113,20 @@ class RecDB:
self.err = "multiple ID Found %s" % (_rlist) self.err = "multiple ID Found %s" % (_rlist)
return False return False
self.session.delete(_rlist[0]) s = self.get_session()
s.delete(_rlist[0])
logging.info("DB: Delete: delete complete") logging.info("DB: Delete: delete complete")
self.commit() s.commit()
return True return True
def commit(self): def commit(self):
logging.info("DB: Commit!!") logging.info("DB: Commit!!")
self.session.commit() self.session.commit()
def get_session(self):
Session = sessionmaker(bind=self.engine)
return Session()
def get_all(self, page=0, page_size=PAGESIZE): def get_all(self, page=0, page_size=PAGESIZE):
return self._search(page=page, page_size=page_size) return self._search(page=page, page_size=page_size)
@ -135,14 +141,14 @@ class RecDB:
def _query_ongoing(self, query=None): def _query_ongoing(self, query=None):
if query is None: if query is None:
query = self.session.query(Rec) query = self.get_session().query(Rec)
return query.filter(Rec.filename == None) return query.filter(Rec.filename == None)
def _query_older(self, delta, query=None): def _query_older(self, delta, query=None):
if query is None: if query is None:
query = self.session.query(Rec) query = self.get_session().query(Rec)
return query.filter(Rec.endtime < datetime.now() - delta) return query.filter(Rec.endtime < datetime.now() - delta)
@ -174,7 +180,7 @@ class RecDB:
"DB: Search => id:%s name:%s starttime:%s endtime=%s" % "DB: Search => id:%s name:%s starttime:%s endtime=%s" %
(_id, name, starttime, endtime)) (_id, name, starttime, endtime))
query = self.session.query(Rec) query = self.get_session().query(Rec)
query = self._query_generic(query, _id, name, starttime, query = self._query_generic(query, _id, name, starttime,
endtime) endtime)
query = self._query_page(query, page, page_size) query = self._query_page(query, page, page_size)