2011-09-26 14:19:34 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
import time
|
2014-02-26 16:40:27 -06:00
|
|
|
from appy.fields.file import FileInfo
|
2011-09-26 14:19:34 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Migrator:
|
|
|
|
'''This class is responsible for performing migrations, when, on
|
|
|
|
installation, we've detected a new Appy version.'''
|
|
|
|
def __init__(self, installer):
|
|
|
|
self.installer = installer
|
2012-02-02 10:30:54 -06:00
|
|
|
self.logger = installer.logger
|
|
|
|
self.app = installer.app
|
2014-02-26 03:40:27 -06:00
|
|
|
self.tool = self.app.config.appy()
|
2011-09-26 14:19:34 -05:00
|
|
|
|
2014-02-26 16:40:27 -06:00
|
|
|
@staticmethod
|
|
|
|
def migrateFileFields(obj):
|
|
|
|
'''Ensures all file fields on p_obj are FileInfo instances.'''
|
|
|
|
migrated = 0 # Count the number of migrated fields
|
|
|
|
for field in obj.fields:
|
|
|
|
if field.type != 'File': continue
|
|
|
|
oldValue = getattr(obj, field.name)
|
|
|
|
if oldValue and not isinstance(oldValue, FileInfo):
|
|
|
|
# A legacy File object. Convert it to a FileInfo instance and
|
|
|
|
# extract the binary to the filesystem.
|
|
|
|
setattr(obj, field.name, oldValue)
|
|
|
|
migrated += 1
|
|
|
|
return migrated
|
|
|
|
|
2014-02-26 03:40:27 -06:00
|
|
|
def migrateTo_0_9_0(self):
|
|
|
|
'''Migrates this DB to Appy 0.9.x.'''
|
2014-02-26 16:40:27 -06:00
|
|
|
# Put all binaries to the filesystem
|
|
|
|
tool = self.tool
|
|
|
|
tool.log('Migrating file fields...')
|
|
|
|
context = {'migrate': self.migrateFileFields, 'nb': 0}
|
|
|
|
for className in tool.o.getAllClassNames():
|
|
|
|
tool.compute(className, context=context, noSecurity=True,
|
|
|
|
expression="ctx['nb'] += ctx['migrate'](obj)")
|
|
|
|
tool.log('Migrated %d File field(s).' % context['nb'])
|
2011-09-26 14:19:34 -05:00
|
|
|
|
2014-02-26 16:40:27 -06:00
|
|
|
def run(self, force=False):
|
|
|
|
'''Executes a migration when relevant, or do it for sure if p_force is
|
|
|
|
True.'''
|
2014-02-26 03:40:27 -06:00
|
|
|
appyVersion = self.tool.appyVersion
|
2014-02-26 16:40:27 -06:00
|
|
|
if force or not appyVersion or (appyVersion < '0.9.0'):
|
2012-02-02 10:30:54 -06:00
|
|
|
# Migration is required.
|
|
|
|
startTime = time.time()
|
2014-02-26 03:40:27 -06:00
|
|
|
self.migrateTo_0_9_0()
|
2012-02-02 10:30:54 -06:00
|
|
|
stopTime = time.time()
|
|
|
|
elapsed = (stopTime-startTime) / 60.0
|
|
|
|
self.logger.info('Migration done in %d minute(s).' % elapsed)
|
2011-09-26 14:19:34 -05:00
|
|
|
# ------------------------------------------------------------------------------
|