Example #1
0
def get_migration_files(host, repo, **kw):
    engine = kw['engine']
    try:
        schema = ControlledSchema(engine, repo)
    except DatabaseNotControlledError:
        ControlledSchema.create(engine, repo)
        schema = ControlledSchema(engine, repo)
    changeset = schema.changeset()
    scripts = []
    for ver_num, script in changeset.items():
        needs = None
        if isinstance(script, PythonScript):
            needs = (script.module.needs
                     if hasattr(script.module, 'needs') else None)
        elif isinstance(script, SqlScript):
            with open(script.path) as f:
                first_line = f.readline()
                if first_line.startswith('-- needs: '):
                    needs = tuple(
                        map(
                            tuple,
                            loads(
                                first_line.strip('\n').replace(
                                    '-- needs: ', ''))))
        scripts.append((repo, ver_num, script, needs))
    scripts.sort()
    return scripts
Example #2
0
 def version_control(engine, version=None):
     ControlledSchema.create(engine, self.repo_path, version)
Example #3
0
 def version_control(engine, version=None):
     ControlledSchema.create(engine, self.repo_path, version)
def main():
    import camelot
    import logging
    logging.basicConfig(level=logging.INFO)
    #logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
    parser = CommandOptionParser(usage=usage,
                                 description=description,
                                 version=camelot.__version__)
    (_options, args) = parser.parse_args()
    if not args:
        parser.print_help()
    elif args[0] == 'console':
        setup_model()
        sh = Shell()
        sh.interact()
    elif args[0] == 'schema_display':
        setup_model()
        schema_display()
    elif args[0] in ('version_control', 'db_version', 'version', 'upgrade'):
        import settings
        from migrate.versioning.repository import Repository
        from migrate.versioning.schema import ControlledSchema
        from migrate.versioning.exceptions import DatabaseNotControlledError
        from sqlalchemy.exceptions import NoSuchTableError
        migrate_engine = settings.ENGINE()
        repository = Repository(settings.REPOSITORY)
        schema = None
        try:
            schema = ControlledSchema(migrate_engine, repository)
        except (NoSuchTableError, DatabaseNotControlledError):
            print 'database not yet under version control, putting it under version_control first.'
        if args[0] == 'version_control' or schema is None:
            migrate_connection = migrate_engine.connect()
            transaction = migrate_connection.begin()
            try:
                schema = ControlledSchema.create(migrate_engine, repository)
                transaction.commit()
            except:
                transaction.rollback()
                raise
            finally:
                migrate_connection.close()
            print 'database was put under version control'
        if schema:
            if args[0] == 'db_version':
                print schema.version
            elif args[0] == 'version':
                print repository.latest
            elif args[0] == 'upgrade':
                migrate_connection = migrate_engine.connect()
                if len(args) >= 2:
                    version = int(args[1])
                else:
                    version = repository.latest
                #
                # perform each upgrade step in a separate transaction, since
                # one upgrade might depend on an other being fully executed
                #
                try:
                    if schema.version == version:
                        print 'database is allready at requested version'
                    if schema.version <= version:
                        step = 1
                    else:
                        step = -1
                    for i in range(schema.version + step, version + step,
                                   step):
                        transaction = migrate_connection.begin()
                        try:
                            schema.upgrade(i)
                            transaction.commit()
                            if step == 1:
                                print 'upgrade %s' % i
                            else:
                                print 'downgrade %s' % i
                        except:
                            transaction.rollback()
                            raise
                finally:
                    migrate_connection.close()
    else:
        parser.print_help()
Example #5
0
def main():
    import camelot
    import logging
    logging.basicConfig(level=logging.INFO)
    #logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
    parser = CommandOptionParser(usage=usage,
                                 description=description, 
                                 version=camelot.__version__)
    (_options, args) = parser.parse_args()
    if not args:
        parser.print_help()
    elif args[0]=='console':
        setup_model()
        sh = Shell()
        sh.interact()
    elif args[0]=='schema_display':
        setup_model()
        schema_display()
    elif args[0] in ('version_control', 'db_version', 'version', 'upgrade'):
        import settings
        from migrate.versioning.repository import Repository
        from migrate.versioning.schema import ControlledSchema
        from migrate.versioning.exceptions import DatabaseNotControlledError
        from sqlalchemy.exceptions import NoSuchTableError
        migrate_engine = settings.ENGINE()
        repository = Repository(settings.REPOSITORY)
        schema = None
        try:
            schema = ControlledSchema(migrate_engine, repository)
        except (NoSuchTableError, DatabaseNotControlledError):
            print 'database not yet under version control, putting it under version_control first.'
        if args[0]=='version_control' or schema is None:
            migrate_connection = migrate_engine.connect()
            transaction = migrate_connection.begin()
            try:
                schema = ControlledSchema.create(migrate_engine, repository)
                transaction.commit()
            except:
                transaction.rollback()
                raise
            finally:
                migrate_connection.close()
            print 'database was put under version control'
        if schema:
            if args[0]=='db_version':
                print schema.version
            elif args[0]=='version':
                print repository.latest
            elif args[0]=='upgrade':
                migrate_connection = migrate_engine.connect()
                if len(args)>=2:
                    version = int(args[1])
                else:
                    version = repository.latest
                #
                # perform each upgrade step in a separate transaction, since
                # one upgrade might depend on an other being fully executed
                #
                try:
                    if schema.version == version:
                        print 'database is allready at requested version'
                    if schema.version <= version:
                        step = 1
                    else:
                        step = -1
                    for i in range(schema.version+step, version+step, step):
                        transaction = migrate_connection.begin()
                        try:
                            schema.upgrade(i)
                            transaction.commit()
                            if step==1:
                                print 'upgrade %s'%i
                            else:
                                print 'downgrade %s'%i
                        except:
                            transaction.rollback()
                            raise
                finally:
                    migrate_connection.close()
    else:
        parser.print_help()