[#1726][model] Harvest source reference compatibility

The 'source' property of harvest objects now comes from the actual
foreign key. For compatibility with old harvesters, an before insert
event listener has beeen added to check if the source id has been set,
and set it automatically from the job if not.
Note that this requires SQLAlchemy 0.7 (ie CKAN 1.5.1)
This commit is contained in:
amercader 2012-02-01 12:52:52 +00:00
parent 004210935a
commit 4c81c7c3a7
1 changed files with 20 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import logging import logging
import datetime import datetime
from sqlalchemy import event
from sqlalchemy import distinct from sqlalchemy import distinct
from sqlalchemy.engine.reflection import Inspector from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.orm import backref, relation from sqlalchemy.orm import backref, relation
@ -120,10 +121,6 @@ class HarvestObject(HarvestDomainObject):
''' '''
@property
def source(self):
return self.job.source
class HarvestGatherError(HarvestDomainObject): class HarvestGatherError(HarvestDomainObject):
'''Gather errors are raised during the **gather** stage of a harvesting '''Gather errors are raised during the **gather** stage of a harvesting
job. job.
@ -136,6 +133,18 @@ class HarvestObjectError(HarvestDomainObject):
''' '''
pass pass
def harvest_object_before_insert_listener(mapper,connection,target):
'''
For compatibility with old harvesters, check if the source id has
been set, and set it automatically from the job if not.
'''
if not target.harvest_source_id or not target.source:
if not target.job:
raise Exception('You must define a Harvest Job for each Harvest Object')
target.source = target.job.source
target.harvest_source_id = target.job.source.id
def define_harvester_tables(): def define_harvester_tables():
global harvest_source_table global harvest_source_table
@ -228,6 +237,12 @@ def define_harvester_tables():
lazy=True, lazy=True,
backref=u'objects', backref=u'objects',
), ),
'source': relation(
HarvestSource,
lazy=True,
backref=u'objects',
),
}, },
) )
@ -253,6 +268,7 @@ def define_harvester_tables():
}, },
) )
event.listen(HarvestObject, 'before_insert', harvest_object_before_insert_listener)
def migrate_v2(): def migrate_v2():
log.debug('Migrating harvest tables to v2. This may take a while...') log.debug('Migrating harvest tables to v2. This may take a while...')