[model] Clearer table initialization

This commit is contained in:
amercader 2012-01-30 17:09:28 +00:00
parent a997e45470
commit f086e908bc
1 changed files with 25 additions and 11 deletions

View File

@ -27,12 +27,26 @@ harvest_gather_error_table = None
harvest_object_error_table = None harvest_object_error_table = None
def setup(): def setup():
if harvest_source_table is None: if harvest_source_table is None:
create_harvester_tables() define_harvester_tables()
log.debug('Harvest tables defined in memory') log.debug('Harvest tables defined in memory')
if model.repo.are_tables_created(): if model.repo.are_tables_created():
metadata.create_all() if not harvest_source_table.exists():
# Create each table individually rahter than
# using metadata.create_all()
harvest_source_table.create()
harvest_job_table.create()
harvest_object_table.create()
harvest_gather_error_table.create()
harvest_object_error_table.create()
log.debug('Harvest tables created') log.debug('Harvest tables created')
else:
log.debug('Harvest tables already exist')
else: else:
log.debug('Harvest table creation deferred') log.debug('Harvest table creation deferred')
@ -46,20 +60,20 @@ class HarvestDomainObject(DomainObject):
key_attr = 'id' key_attr = 'id'
@classmethod @classmethod
def get(self, key, default=None, attr=None): def get(cls, key, default=None, attr=None):
'''Finds a single entity in the register.''' '''Finds a single entity in the register.'''
if attr == None: if attr == None:
attr = self.key_attr attr = cls.key_attr
kwds = {attr: key} kwds = {attr: key}
o = self.filter(**kwds).first() o = cls.filter(**kwds).first()
if o: if o:
return o return o
else: else:
return default return default
@classmethod @classmethod
def filter(self, **kwds): def filter(cls, **kwds):
query = Session.query(self).autoflush(False) query = Session.query(cls).autoflush(False)
return query.filter_by(**kwds) return query.filter_by(**kwds)
@ -107,7 +121,7 @@ class HarvestObjectError(HarvestDomainObject):
''' '''
pass pass
def create_harvester_tables(): def define_harvester_tables():
global harvest_source_table global harvest_source_table
global harvest_job_table global harvest_job_table