예제 #1
0
def test_connection(args, config, connection=None):
    engine = get_engine(config, connection)

    db_connection = engine.get_connection()

    print('Connection OK')
    db_connection.close()
예제 #2
0
def migrate(args, config, connection=None):
    fake = args.fake
    revert = args.revert
    till_migration_name = args.migration
    engine = get_engine(config, connection)

    applied_migrations = engine.get_applied_migrations()

    if applied_migrations and revert:
        last_applied_migration = applied_migrations[-1][0]
        try:
            with open(os.path.join('migrations',
                                   last_applied_migration,
                                   'down.sql')) as f:
                down = f.read()
        except IOError:
            raise MigrationIrreversible('Migration %s does not '
                                        'have down.sql - reverting '
                                        'impossible' % last_applied_migration)

        print('Un-Applying migration %s... ' % last_applied_migration, end='')
        if fake:
            print('(fake run) ', end='')
        try:
            engine.unapply_migration(last_applied_migration, down, fake)
        except ApplyMigrationFailed:
            print('Error, rolled back')
        else:
            print('done')
        return

    elif not revert:
        migration_list = unapplied_migrations(
            sorted(glob.glob('migrations/*')),
            applied_migrations)
    else:
        # no migrations at all
        migration_list = sorted(glob.glob('migrations/*'))

    for migration in migration_list:
        with open(os.path.join('migrations', migration, 'up.sql')) as f:
            up = f.read()

        migration_name = migration.split('/')[-1]
        print('Applying migration %s... ' % migration_name, end='')
        if fake:
            print('(fake run) ', end='')
        try:
            engine.apply_migration(migration_name, up, fake)
        except ApplyMigrationFailed:
            print('Error, rolled back')
            break
        else:
            print('done')
        if till_migration_name \
                and migration_name == till_migration_name:
            break
예제 #3
0
def migrate(args, config, connection=None):
    fake = args.fake
    revert = args.revert
    till_migration_name = args.migration
    engine = get_engine(config, connection)

    applied_migrations = engine.get_applied_migrations()

    if applied_migrations and revert:
        last_applied_migration = applied_migrations[-1][0]
        try:
            with open(
                    os.path.join('migrations', last_applied_migration,
                                 'down.sql')) as f:
                down = f.read()
        except IOError:
            raise MigrationIrreversible('Migration %s does not '
                                        'have down.sql - reverting '
                                        'impossible' % last_applied_migration)

        print('Un-Applying migration %s... ' % last_applied_migration, end='')
        if fake:
            print('(fake run) ', end='')
        try:
            engine.unapply_migration(last_applied_migration, down, fake)
        except ApplyMigrationFailed:
            print('Error, rolled back')
        else:
            print('done')
        return

    elif not revert:
        migration_list = unapplied_migrations(
            sorted(glob.glob('migrations/*')), applied_migrations)
    else:
        # no migrations at all
        migration_list = sorted(glob.glob('migrations/*'))

    for migration in migration_list:
        with open(os.path.join('migrations', migration, 'up.sql')) as f:
            up = f.read()

        migration_name = migration.split('/')[-1]
        print('Applying migration %s... ' % migration_name, end='')
        if fake:
            print('(fake run) ', end='')
        try:
            engine.apply_migration(migration_name, up, fake)
        except ApplyMigrationFailed:
            print('Error, rolled back')
            break
        else:
            print('done')
        if till_migration_name \
                and migration_name == till_migration_name:
            break
예제 #4
0
def status(args, config, connection=None):
    """
    1. get applied migrations
    2. get all migrations
    3. check unapplied migrations
    """

    engine = get_engine(config, connection)

    applied_migrations = {m[0] for m in engine.get_applied_migrations()}
    all_migrations = sorted(os.listdir('migrations/'))
    for i, migration in enumerate(all_migrations):
        if migration in applied_migrations:
            print('Migration %s - applied' % migration)
        else:
            print('Migration %s - NOT applied' % migration)
예제 #5
0
def initdb(args, config, connection=None):
    engine = get_engine(config, connection)

    print('Creating db...')
    engine.create_migrations_table()
    print('Done.')