def test_execution_continues_with_ignore_errors_in_transaction(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) migrations.apply() cursor = conn.cursor() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == [(2, )]
def test_execution_continues_with_ignore_errors_in_transaction(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) migrations.apply() cursor = conn.cursor() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == [(2,)]
def test_migrations_can_import_step_and_transaction(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM test") assert cursor.fetchall() == [(1,)]
def test_specify_migration_table(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM another_migration_table") assert cursor.fetchall() == [('0',)]
def test_rollbacks_happen_in_reverse(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) migrations.apply() cursor = conn.cursor() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == [(2,)] migrations.rollback() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == []
def test_rollbacks_happen_in_reverse(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) migrations.apply() cursor = conn.cursor() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == [(2, )] migrations.rollback() cursor.execute("SELECT * FROM test") assert cursor.fetchall() == []
def test_migrations_can_import_step_and_transaction(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM test") assert cursor.fetchall() == [(1, )]
def test_specify_migration_table(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM another_migration_table") assert cursor.fetchall() == [('0', )]
def test_migration_functions_have_namespace_access(tmpdir): """ Test that functions called via step have access to the script namespace """ conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM foo_test") assert cursor.fetchall() == [(1,)]
def test_transaction_is_not_committed_on_error(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) try: migrations.apply() except DatabaseError: # Expected pass else: raise AssertionError("Expected a DatabaseError") cursor = conn.cursor() cursor.execute("SELECT count(1) FROM test") assert cursor.fetchone() == (0,)
def test_transaction_is_not_committed_on_error(tmpdir): conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir) try: migrations.apply() except DatabaseError: # Expected pass else: raise AssertionError("Expected a DatabaseError") cursor = conn.cursor() cursor.execute("SELECT count(1) FROM test") assert cursor.fetchone() == (0, )
def test_migration_functions_have_namespace_access(tmpdir): """ Test that functions called via step have access to the script namespace """ conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, tmpdir, migration_table='another_migration_table') migrations.apply() cursor = conn.cursor() cursor.execute("SELECT id FROM foo_test") assert cursor.fetchall() == [(1, )]
def _migrate_database(self): db_config = { 'host':'localhost', 'user':'******', 'passwd':'', 'name':'vilya-test'} db_config.update(self.app.config['DATABASE']) migrations_dir = self.app.config['MIGRATIONS_DIR'] if db_config['passwd']: db_config['user'] += ':' + db_config['passwd'] if db_config.get('port'): db_config['host'] += ':' + db_config['port'] db_connection_url = "mysql://{user}@{host}/{name}".format(**db_config) connection, paramstyle = connect(db_connection_url) migrations = yoyo.read_migrations(connection, paramstyle, migrations_dir) migrations.to_apply().apply() connection.commit() self.migrate_info = (connection, paramstyle, migrations_dir)
def read_config(self): db_config = { 'host':'localhost', 'port':'', 'user':'******', 'passwd':'', } db_config.update(app.config['DATABASE']) if db_config['passwd']: db_config['user'] += ':' + db_config['passwd'] if db_config['port']: db_config['host'] += ':' + db_config['port'] logging.debug(db_config) db_connection_url = "mysql://{user}@{host}/{name}".format(**db_config) self.db_config = db_config self.connection, self.paramstyle = connect(db_connection_url) self.migrations_dir = os.path.abspath(app.config['MIGRATIONS_DIR']) self.mg = MigrationGenerator(self.migrations_dir)
def main(): conn, paramstyle = connect(env.DATABASE_URI) schema_path = os.path.join(env.APP_ROOT, "schema/db") migrations = read_migrations(conn, paramstyle, schema_path) migrations.to_apply().apply() conn.commit()
def main(argv=None): argparser = make_argparser() args = argparser.parse_args(argv) if args.verbosity_level: verbosity_level = args.verbosity_level else: verbosity_level = args.verbose verbosity_level = min(verbosity_level, max(verbosity_levels)) verbosity_level = max(verbosity_level, min(verbosity_levels)) configure_logging(verbosity_level) command = args.command migrations_dir = os.path.normpath(os.path.abspath(args.migrations_dir)) dburi = args.database config_path = os.path.join(migrations_dir, '.yoyo-migrate') config = readconfig(config_path) if dburi is None and args.cache: try: logger.debug("Looking up connection string for %r", migrations_dir) dburi = config.get('DEFAULT', 'dburi') except (ValueError, NoSectionError, NoOptionError): pass if args.migration_table: migration_table = args.migration_table else: try: migration_table = config.get('DEFAULT', 'migration_table') except (ValueError, NoSectionError, NoOptionError): migration_table = None # Earlier versions had a bug where the migration_table could be set to the # string 'None'. if migration_table in (None, 'None'): migration_table = default_migration_table config.set('DEFAULT', 'migration_table', migration_table) if dburi is None: argparser.error( "Please specify command, migrations directory and " "database connection string arguments" ) if args.prompt_password: password = getpass('Password for %s: ' % dburi) scheme, username, _, host, port, database, db_params = parse_uri(dburi) dburi = unparse_uri((scheme, username, password, host, port, database, db_params)) # Cache the database this migration set is applied to so that subsequent # runs don't need the dburi argument. Don't cache anything in batch mode - # we can't prompt to find the user's preference. if args.cache and not args.batch: if not config.has_option('DEFAULT', 'dburi'): response = prompt( "Save connection string to %s for future migrations?\n" "This is saved in plain text and " "contains your database password." % (config_path,), "yn" ) if response == 'y': config.set('DEFAULT', 'dburi', dburi) elif config.get('DEFAULT', 'dburi') != dburi: response = prompt( "Specified connection string differs from that saved in %s. " "Update saved connection string?" % (config_path,), "yn" ) if response == 'y': config.set('DEFAULT', 'dburi', dburi) config.set('DEFAULT', 'migration_table', migration_table) saveconfig(config, config_path) conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, migrations_dir, migration_table=migration_table) if args.match: migrations = migrations.filter( lambda m: re.search(args.match, m.id) is not None) if not args.all: if command in ['apply']: migrations = migrations.to_apply() elif command in ['reapply', 'rollback']: migrations = migrations.to_rollback() if not args.batch: migrations = prompt_migrations(conn, paramstyle, migrations, command) if not args.batch and migrations: if prompt(command.title() + plural(len(migrations), " %d migration", " %d migrations") + " to %s?" % dburi, "Yn") != 'y': return 0 if command == 'reapply': migrations.rollback(args.force) migrations.apply(args.force) elif command == 'apply': migrations.apply(args.force) elif command == 'rollback': migrations.rollback(args.force)
def main(argv=None): argparser = make_argparser() args = argparser.parse_args(argv) if args.verbosity_level: verbosity_level = args.verbosity_level else: verbosity_level = args.verbose verbosity_level = min(verbosity_level, max(verbosity_levels)) verbosity_level = max(verbosity_level, min(verbosity_levels)) configure_logging(verbosity_level) command = args.command migrations_dir = os.path.normpath(os.path.abspath(args.migrations_dir)) dburi = args.database config_path = os.path.join(migrations_dir, '.yoyo-migrate') config = readconfig(config_path) if dburi is None and args.cache: try: logger.debug("Looking up connection string for %r", migrations_dir) dburi = config.get('DEFAULT', 'dburi') except (ValueError, NoSectionError, NoOptionError): pass if args.migration_table: migration_table = args.migration_table else: try: migration_table = config.get('DEFAULT', 'migration_table') except (ValueError, NoSectionError, NoOptionError): migration_table = None # Earlier versions had a bug where the migration_table could be set to the # string 'None'. if migration_table in (None, 'None'): migration_table = default_migration_table config.set('DEFAULT', 'migration_table', migration_table) if dburi is None: argparser.error("Please specify command, migrations directory and " "database connection string arguments") if args.prompt_password: password = getpass('Password for %s: ' % dburi) scheme, username, _, host, port, database, db_params = parse_uri(dburi) dburi = unparse_uri( (scheme, username, password, host, port, database, db_params)) # Cache the database this migration set is applied to so that subsequent # runs don't need the dburi argument. Don't cache anything in batch mode - # we can't prompt to find the user's preference. if args.cache and not args.batch: if not config.has_option('DEFAULT', 'dburi'): response = prompt( "Save connection string to %s for future migrations?\n" "This is saved in plain text and " "contains your database password." % (config_path, ), "yn") if response == 'y': config.set('DEFAULT', 'dburi', dburi) elif config.get('DEFAULT', 'dburi') != dburi: response = prompt( "Specified connection string differs from that saved in %s. " "Update saved connection string?" % (config_path, ), "yn") if response == 'y': config.set('DEFAULT', 'dburi', dburi) config.set('DEFAULT', 'migration_table', migration_table) saveconfig(config, config_path) conn, paramstyle = connect(dburi) migrations = read_migrations(conn, paramstyle, migrations_dir, migration_table=migration_table) if args.match: migrations = migrations.filter( lambda m: re.search(args.match, m.id) is not None) if not args.all: if command in ['apply']: migrations = migrations.to_apply() elif command in ['reapply', 'rollback']: migrations = migrations.to_rollback() if not args.batch: migrations = prompt_migrations(conn, paramstyle, migrations, command) if not args.batch and migrations: if prompt( command.title() + plural(len(migrations), " %d migration", " %d migrations") + " to %s?" % dburi, "Yn") != 'y': return 0 if command == 'reapply': migrations.rollback(args.force) migrations.apply(args.force) elif command == 'apply': migrations.apply(args.force) elif command == 'rollback': migrations.rollback(args.force)