Ejemplo n.º 1
0
def migration_script(connection):
    log.info("script", "Running the migration script.")
    for application in applications.get_application_registry().itervalues():
        keys = ApplicationVersions.key_for(application.name)
        version_doc = yield connection.query_view(ApplicationVersions, **keys)
        if not version_doc:
            to_run = application.get_migrations()
            version_doc = ApplicationVersion(name=unicode(application.name))
        else:
            version_doc = version_doc[0]
            to_run = [(version, migration)
                      for version, migration in application.get_migrations()
                      if version > version_doc.version]
        if not to_run:
            log.info("script", "There are no migrations for application %s "
                     "from version %s to %s", application.name,
                     version_doc.version, application.version)
            continue
        try:
            for version, migration in to_run:
                yield migration.run(connection._database)
                if isinstance(version, str):
                    version = unicode(version)
                version_doc.version = version
                yield connection.save_document(version_doc)
                log.info("script", "Successfully applied migration %r",
                         migration)

        except Exception as e:
            error.handle_exception("script", e, "Failed applying migration %r",
                                   migration)
            continue
Ejemplo n.º 2
0
    def testComplexMigration(self):
        mig = migration.Migration()

        self.run = False

        def handler(connection, unparsed):
            doc = connection._unserializer.convert(unparsed)
            self.run = True
            doc.create_attachment('attachment', 'Hi!')
            return connection.save_document(doc)

        mig.registry.register(VersionedTest2)
        mig.migrate_type(VersionedTest2, handler)
        r = applications.get_application_registry()
        snapshot = r.get_snapshot()
        self.addCleanup(r.reset, snapshot)
        r.reset([])
        app = applications.Application()
        app.name = u'test'
        r.register(app)
        app.register_migration('1.0', mig)

        serialization.register(VersionedTest2)
        doc = yield self.connection.save_document(VersionedTest2())
        self.assertEqual('default', doc.field1)

        yield tools.migration_script(self.connection)
        self.assertTrue(self.run)

        doc = yield self.connection.get_document('testdoc')
        self.assertEqual('default', doc.field1)
        self.assertTrue('attachment' in doc.attachments)

        version_doc = yield self.connection.query_view(
            tools.ApplicationVersions, key='test', include_docs=True)
        self.assertEquals(1, len(version_doc))
        self.assertEquals('1.0', version_doc[0].version)
        self.assertEquals('test', version_doc[0].name)
Ejemplo n.º 3
0
 def get_applications(self):
     return list(applications.get_application_registry().itervalues())
Ejemplo n.º 4
0
 def get_app(self, name):
     return applications.get_application_registry().lookup(name)