take tables out of global scope at import time

This commit is contained in:
David Raznick 2011-05-31 18:02:07 +01:00
parent 2b98080266
commit 264b606c48
1 changed files with 113 additions and 97 deletions

View File

@ -19,9 +19,18 @@ __all__ = [
] ]
harvest_source_table = None
harvest_job_table = None
harvest_object_table = None
harvest_gather_error_table = None
harvest_object_error_table = None
def setup(): def setup():
if harvest_source_table is None:
create_harvester_tables()
metadata.create_all() metadata.create_all()
class HarvestError(Exception): class HarvestError(Exception):
pass pass
@ -92,8 +101,15 @@ class HarvestObjectError(HarvestDomainObject):
''' '''
pass pass
def create_harvester_tables():
harvest_source_table = Table('harvest_source', metadata, global harvest_source_table
global harvest_job_table
global harvest_object_table
global harvest_gather_error_table
global harvest_object_error_table
harvest_source_table = Table('harvest_source', metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid), Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('url', types.UnicodeText, nullable=False), Column('url', types.UnicodeText, nullable=False),
Column('description', types.UnicodeText, default=u''), Column('description', types.UnicodeText, default=u''),
@ -103,18 +119,18 @@ harvest_source_table = Table('harvest_source', metadata,
Column('active',types.Boolean,default=True), Column('active',types.Boolean,default=True),
Column('user_id', types.UnicodeText, default=u''), Column('user_id', types.UnicodeText, default=u''),
Column('publisher_id', types.UnicodeText, default=u''), Column('publisher_id', types.UnicodeText, default=u''),
) )
# Was harvesting_job # Was harvesting_job
harvest_job_table = Table('harvest_job', metadata, harvest_job_table = Table('harvest_job', metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid), Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('created', DateTime, default=datetime.datetime.utcnow), Column('created', DateTime, default=datetime.datetime.utcnow),
Column('gather_started', DateTime), Column('gather_started', DateTime),
Column('gather_finished', DateTime), Column('gather_finished', DateTime),
Column('source_id', types.UnicodeText, ForeignKey('harvest_source.id')), Column('source_id', types.UnicodeText, ForeignKey('harvest_source.id')),
Column('status', types.UnicodeText, default=u'New', nullable=False), Column('status', types.UnicodeText, default=u'New', nullable=False),
) )
# Was harvested_document # Was harvested_document
harvest_object_table = Table('harvest_object', metadata, harvest_object_table = Table('harvest_object', metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid), Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('guid', types.UnicodeText, default=''), Column('guid', types.UnicodeText, default=''),
Column('gathered', DateTime, default=datetime.datetime.utcnow), Column('gathered', DateTime, default=datetime.datetime.utcnow),
@ -125,24 +141,24 @@ harvest_object_table = Table('harvest_object', metadata,
Column('retry_times',types.Integer), Column('retry_times',types.Integer),
Column('harvest_job_id', types.UnicodeText, ForeignKey('harvest_job.id')), Column('harvest_job_id', types.UnicodeText, ForeignKey('harvest_job.id')),
Column('package_id', types.UnicodeText, ForeignKey('package.id'), nullable=True), Column('package_id', types.UnicodeText, ForeignKey('package.id'), nullable=True),
) )
# New table # New table
harvest_gather_error_table = Table('harvest_gather_error',metadata, harvest_gather_error_table = Table('harvest_gather_error',metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid), Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('harvest_job_id', types.UnicodeText, ForeignKey('harvest_job.id')), Column('harvest_job_id', types.UnicodeText, ForeignKey('harvest_job.id')),
Column('message', types.UnicodeText), Column('message', types.UnicodeText),
Column('created', DateTime, default=datetime.datetime.utcnow), Column('created', DateTime, default=datetime.datetime.utcnow),
) )
# New table # New table
harvest_object_error_table = Table('harvest_object_error',metadata, harvest_object_error_table = Table('harvest_object_error',metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid), Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('harvest_object_id', types.UnicodeText, ForeignKey('harvest_object.id')), Column('harvest_object_id', types.UnicodeText, ForeignKey('harvest_object.id')),
Column('message',types.UnicodeText), Column('message',types.UnicodeText),
Column('stage', types.UnicodeText), Column('stage', types.UnicodeText),
Column('created', DateTime, default=datetime.datetime.utcnow), Column('created', DateTime, default=datetime.datetime.utcnow),
) )
mapper( mapper(
HarvestSource, HarvestSource,
harvest_source_table, harvest_source_table,
properties={ properties={
@ -152,14 +168,14 @@ mapper(
order_by=harvest_job_table.c.created, order_by=harvest_job_table.c.created,
), ),
}, },
) )
mapper( mapper(
HarvestJob, HarvestJob,
harvest_job_table, harvest_job_table,
) )
mapper( mapper(
HarvestObject, HarvestObject,
harvest_object_table, harvest_object_table,
properties={ properties={
@ -172,9 +188,9 @@ mapper(
backref=u'objects', backref=u'objects',
), ),
}, },
) )
mapper( mapper(
HarvestGatherError, HarvestGatherError,
harvest_gather_error_table, harvest_gather_error_table,
properties={ properties={
@ -183,9 +199,9 @@ mapper(
backref='gather_errors' backref='gather_errors'
), ),
}, },
) )
mapper( mapper(
HarvestObjectError, HarvestObjectError,
harvest_object_error_table, harvest_object_error_table,
properties={ properties={
@ -194,4 +210,4 @@ mapper(
backref='errors' backref='errors'
), ),
}, },
) )