private static MigrationVersion getDatabaseVersion(DataSource dataSource) throws FlywayException { Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); MigrationInfoService info = flyway.info(); MigrationVersion currentVersion = MigrationVersion.EMPTY; if (info.current() != null) { currentVersion = info.current().getVersion(); } return currentVersion; }
public void info() throws FlywayException { MigrationInfoService info = flyway.info(); System.out.println(MigrationInfoDumper.dumpToAsciiTable(info.all())); }
private static VersionedDatabaseJobHistoryStore findVersionedDatabaseJobHistoryStore(MigrationVersion requiredVersion) throws IllegalAccessException, InstantiationException, ClassNotFoundException { Class<?> foundClazz = null; Class<?> defaultClazz = null; MigrationVersion defaultVersion = MigrationVersion.EMPTY; for (Class<?> clazz : Sets.intersection(reflections.getTypesAnnotatedWith(SupportedDatabaseVersion.class), reflections.getSubTypesOf(VersionedDatabaseJobHistoryStore.class))) { SupportedDatabaseVersion annotation = clazz.getAnnotation(SupportedDatabaseVersion.class); String version = annotation.version(); MigrationVersion actualVersion = MigrationVersion.fromVersion(Strings.isNullOrEmpty(version) ? null : version); if (annotation.isDefault() && actualVersion.compareTo(defaultVersion) > 0) { defaultClazz = clazz; defaultVersion = actualVersion; } if (actualVersion.compareTo(requiredVersion) == 0) { foundClazz = clazz; } } if (foundClazz == null) { foundClazz = defaultClazz; } if (foundClazz == null) { throw new ClassNotFoundException( String.format("Could not find an instance of %s which supports database " + "version %s.", VersionedDatabaseJobHistoryStore.class.getSimpleName(), requiredVersion.toString())); } return (VersionedDatabaseJobHistoryStore) foundClazz.newInstance(); } }
private FlywayMigration(MigrationInfo info) { this.type = info.getType(); this.checksum = info.getChecksum(); this.version = nullSafeToString(info.getVersion()); this.description = info.getDescription(); this.script = info.getScript(); this.state = info.getState(); this.installedBy = info.getInstalledBy(); this.installedRank = info.getInstalledRank(); this.executionTime = info.getExecutionTime(); this.installedOn = nullSafeToInstant(info.getInstalledOn()); }
private void printFlywayInfo(MigrationInfo... infos){ MigrationInfo[] all = ArrayUtils.nullToEmpty(infos); if(all.length > 0){ for (MigrationInfo migrationInfo : all) { out.println("[Hibernate Service] Flyway Information:"); out.println("Version: " + migrationInfo.getVersion().toString()); out.println("Description: " + migrationInfo.getDescription()); out.println("Date: " + migrationInfo.getInstalledOn()); out.println("State: " + migrationInfo.getState().name()); out.println("Type: " + migrationInfo.getType().name()); out.println("[Hibernate Service] -------------------"); } } } }
@Override public String getSchemaVersion() throws TopiaMigrationServiceException { MigrationInfo currentOrNull = flyway.info().current(); if (currentOrNull == null) { throw new TopiaMigrationServiceException("schema version is unknown"); } String schemaVersion = currentOrNull.getVersion().getVersion(); return schemaVersion; }
@Override public void migrate(Flyway flyway) { MigrationInfo current = flyway.info().current(); if (current != null && current.getVersion().equals(INITIAL) && current.getType() == MigrationType.SQL) { logger.info("Detected initial version based on SQL scripts, doing repair to switch to Java based migrations."); flyway.repair(); } flyway.migrate(); } }
@Override public void afterEachMigrate(Connection connection, MigrationInfo info) { super.afterEachMigrate(connection, info); try { connection.commit(); } catch (SQLException e) { Assert.fail(e.getMessage()); } for (MigrationTest test : tests) { if (test.getTargetMigration().equals(info.getVersion().getVersion())) { try { test.runAssertions(); } catch (Exception e) { Assert.fail(e.getMessage()); } assertionsRan[0]++; } } } });
@BeforeClass public static void setUpDatabase() throws Exception { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); database = builder.build(); Flyway flyway = new Flyway(); flyway.setBaselineVersion(MigrationVersion.fromVersion("1.5.2")); flyway.setLocations("classpath:/org/cloudfoundry/identity/uaa/db/hsqldb/"); flyway.setDataSource(database); flyway.migrate(); }
/** * {@inheritDoc} * @see org.flywaydb.core.api.callback.FlywayCallback#beforeEachMigrate(java.sql.Connection, org.flywaydb.core.api.MigrationInfo) */ @Override public void beforeEachMigrate(Connection connection, MigrationInfo info) { out.println("[Hibernate Service] Flyway migrate: " + info.getVersion().toString()); }
/** * Adds a jar or a directory with this name to the classpath. * * @param name The name of the jar or directory to add. * @throws IOException when the jar or directory could not be found. */ private static void addJarOrDirectoryToClasspath(final String name) throws IOException { LOG.debug("Adding location to classpath: " + name); try { final URL url = new File(name).toURI().toURL(); final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); final Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(sysloader, url); } catch (final Exception e) { throw new FlywayException("Unable to load " + name, e); } }
final FileSystemResource sqlScriptResource = new FileSystemResource(migration.getResolvedMigration().getPhysicalLocation()); final MigrationExecutor migrationExecutor = new CapturingSqlMigrationExecutor(sqlStatements, dbSupport, doMigrate(migration, migrationExecutor, migrationText); } catch (final SQLException e) { throw new FlywayException("Unable to apply migration", e); migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), null, null,
@Override public void addAppliedMigration(final AppliedMigration appliedMigration) { final MigrationVersion version = appliedMigration.getVersion(); final String versionStr = version == null ? null : version.toString(); final int calculateInstalledRank; try { .append("'").append(versionStr).append("',") .append("'").append(appliedMigration.getDescription()).append("',") .append("'").append(appliedMigration.getType().name()).append("',") .append("'").append(appliedMigration.getScript()).append("',") .append(appliedMigration.getChecksum()).append(",")
private static MigrationVersion getDatabaseVersion(DataSource dataSource) throws FlywayException { Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); MigrationInfoService info = flyway.info(); MigrationVersion currentVersion = MigrationVersion.EMPTY; if (info.current() != null) { currentVersion = info.current().getVersion(); } return currentVersion; }
flyway.validate(); } else if ("info".equals(operation)) { LOG.info("\n" + MigrationInfoDumper.dumpToAsciiTable(flyway.info().all())); } else if ("repair".equals(operation)) { flyway.repair();
@BeforeClass public static void init() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); database = builder.build(); flyway = new Flyway(); flyway.setBaselineVersion(MigrationVersion.fromVersion("1.5.2")); flyway.setLocations("classpath:/org/cloudfoundry/identity/uaa/db/hsqldb/"); flyway.setDataSource(database); flyway.migrate(); }
/** * Loads the configuration from the configuration file. If a configuration file is specified using the -configfile * argument it will be used, otherwise the default config file (conf/flyway.properties) will be loaded. * * @param properties The properties object to load to configuration into. * @param file The configuration file to load. * @param encoding The encoding of the configuration file. * @param failIfMissing Whether to fail if the file is missing. * @return Whether the file was loaded successfully. * @throws FlywayException when the configuration file could not be loaded. */ private static boolean loadConfigurationFile(final Properties properties, final String file, final String encoding, final boolean failIfMissing) throws FlywayException { final File configFile = new File(file); final String errorMessage = "Unable to load config file: " + configFile.getAbsolutePath(); if (!configFile.isFile() || !configFile.canRead()) { if (!failIfMissing) { LOG.debug(errorMessage); return false; } throw new FlywayException(errorMessage); } LOG.debug("Loading config file: " + configFile.getAbsolutePath()); try { final String contents = FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(configFile), encoding)); properties.load(new StringReader(contents.replace("\\", "\\\\"))); return true; } catch (final IOException e) { throw new FlywayException(errorMessage, e); } }
private static MigrationVersion getDatabaseVersion(DataSource dataSource) throws FlywayException { Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); MigrationInfoService info = flyway.info(); MigrationVersion currentVersion = MigrationVersion.EMPTY; if (info.current() != null) { currentVersion = info.current().getVersion(); } return currentVersion; }
@ReadOperation public ApplicationFlywayBeans flywayBeans() { ApplicationContext target = this.context; Map<String, ContextFlywayBeans> contextFlywayBeans = new HashMap<>(); while (target != null) { Map<String, FlywayDescriptor> flywayBeans = new HashMap<>(); target.getBeansOfType(Flyway.class).forEach((name, flyway) -> flywayBeans .put(name, new FlywayDescriptor(flyway.info().all()))); ApplicationContext parent = target.getParent(); contextFlywayBeans.put(target.getId(), new ContextFlywayBeans(flywayBeans, (parent != null) ? parent.getId() : null)); target = parent; } return new ApplicationFlywayBeans(contextFlywayBeans); }
@Test public void sleepAfterFailedMigration() { props.addProperty("databaseServer", "foo"); Flyway flyway = mock(Flyway.class); when(flyway.migrate()).thenThrow(new FlywayException()); migrator.flyway = flyway; migrator.flywayFailedSemaphore.release(); migrator.migrate(); } }