org.flywaydb.core.api.MigrationInfo Java Examples

The following examples show how to use org.flywaydb.core.api.MigrationInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PreviewMigrateCommand.java    From keywhiz with Apache License 2.0 6 votes vote down vote up
@Override protected void run(Bootstrap<KeywhizConfig> bootstrap, Namespace namespace,
    KeywhizConfig config) throws Exception {
  DataSource dataSource = config.getDataSourceFactory()
      .build(new MetricRegistry(), "migration-preview-datasource");

  Flyway flyway = Flyway.configure().dataSource(dataSource).locations(config.getMigrationsDir()).table(config.getFlywaySchemaTable()).load();
  MigrationInfoService info = flyway.info();

  MigrationInfo current = info.current();
  if (current == null) {
    logger.info("No migrations have been run yet.");
  } else {
    logger.info("Currently applied migration:");
    logger.info("* {} - {}", current.getVersion(), current.getDescription());
  }

  if (info.pending().length > 0) {
    logger.info("Pending migrations:");
    for (MigrationInfo migration : info.pending()) {
      logger.info("* {} - {}", migration.getVersion(), migration.getDescription());
    }
  } else {
    logger.info("No pending migrations");
  }
}
 
Example #2
Source File: FlywayExtensionBaselineOnMigrateNamedDataSourceTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Create history table correctly")
public void testFlywayInitialBaselineInfo() {
    MigrationInfo baselineInfo = flyway.info().applied()[0];

    assertEquals("0.0.1", baselineInfo.getVersion().getVersion());
    assertEquals("Initial description for test", baselineInfo.getDescription());
}
 
Example #3
Source File: MigrationResource.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>listInfo.</p>
 *
 * @return a {@link java.util.Map} object.
 */
@GET
public Map<String, MigrationInfo[]> listInfo() {
    Map<String, MigrationInfo[]> infoMap = Maps.newLinkedHashMap();
    for (String dbName : DataSourceManager.getDataSourceNames()) {
        Flyway flyway = locator.getService(Flyway.class, dbName);
        infoMap.put(dbName, flyway.info().all());
    }
    return infoMap;
}
 
Example #4
Source File: FlywayMigrationsTask.java    From web-budget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void run() {

    checkNotNull(this.dataSource, "No datasource found for migrations");

    final Flyway flyway = Flyway.configure()
            .dataSource(this.dataSource)
            .locations("db/migrations")
            .baselineOnMigrate(true)
            .baselineVersion("0")
            .sqlMigrationPrefix("")
            .load();

    final MigrationInfo migrationInfo = flyway.info().current();

    if (migrationInfo == null) {
        this.logger.info("No existing database at the actual datasource");
    } else {
        this.logger.info("Current version: {}", migrationInfo.getVersion() + " : " + migrationInfo.getDescription());
    }

    try {
        flyway.migrate();
        this.logger.info("Successfully migrated to version: {}", flyway.info().current().getVersion());
    } catch (FlywayException ex) {
        this.logger.info("Flyway migrations failed!", ex);
    }
}
 
Example #5
Source File: MigrationResource.java    From ameba with MIT License 5 votes vote down vote up
/**
 * Retrieves the full set of infos about the migrations resolved on the classpath.
 *
 * @return The resolved migrations. An empty array if none.
 */
private List<MigrationInfo> resolved(MigrationInfo[] migrationInfos) {
    if (migrationInfos.length == 0)
        throw new NotFoundException();
    List<MigrationInfo> resolvedMigrations = Lists.newArrayList();
    for (MigrationInfo migrationInfo : migrationInfos) {
        if (migrationInfo.getState().isResolved()) {
            resolvedMigrations.add(migrationInfo);
        }
    }

    return resolvedMigrations;
}
 
Example #6
Source File: MigrationResource.java    From ameba with MIT License 5 votes vote down vote up
/**
 * Retrieves the full set of infos about the migrations that failed.
 *
 * @return The failed migrations. An empty array if none.
 */
private List<MigrationInfo> failed(MigrationInfo[] migrationInfos) {
    if (migrationInfos.length == 0)
        throw new NotFoundException();
    List<MigrationInfo> failedMigrations = Lists.newArrayList();
    for (MigrationInfo migrationInfo : migrationInfos) {
        if (migrationInfo.getState().isFailed()) {
            failedMigrations.add(migrationInfo);
        }
    }

    return failedMigrations;
}
 
Example #7
Source File: FlywayMigrationsTask.java    From library with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void run() {

    checkNotNull(this.dataSource, "No datasource found for migrations");

    final Flyway flyway = Flyway.configure()
            .dataSource(this.dataSource)
            .locations("db/migrations")
            .baselineOnMigrate(true)
            .baselineVersion("0")
            .sqlMigrationPrefix("")
            .load();

    final MigrationInfo migrationInfo = flyway.info().current();

    if (migrationInfo == null) {
        this.logger.info("No existing database at the actual datasource");
    } else {
        this.logger.info("Current version: {}", migrationInfo.getVersion() + " : " + migrationInfo.getDescription());
    }

    try {
        flyway.migrate();
        this.logger.info("Successfully migrated to version: {}", flyway.info().current().getVersion());
    } catch (FlywayException ex) {
        this.logger.info("Flyway migrations failed!", ex);
    }
}
 
Example #8
Source File: SkipperFlywayMigrationStrategy.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #9
Source File: FlywayExtensionMigrateAtStartTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Migrates at start correctly")
public void testFlywayConfigInjection() {
    MigrationInfo migrationInfo = flyway.info().current();
    assertNotNull(migrationInfo, "No Flyway migration was executed");

    String currentVersion = migrationInfo
            .getVersion()
            .toString();
    // Expected to be 1.0.0 as migration runs at start
    assertEquals("1.0.0", currentVersion);
}
 
Example #10
Source File: FlywayExtensionBaselineOnMigrateTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Create history table correctly")
public void testFlywayInitialBaselineInfo() {
    MigrationInfo baselineInfo = flyway.info().applied()[0];

    assertEquals("0.0.1", baselineInfo.getVersion().getVersion());
    assertEquals("Initial description for test", baselineInfo.getDescription());
}
 
Example #11
Source File: FlywayExtensionMigrateAtStartNamedDataSourceTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Migrates at start for datasource named 'users' correctly")
public void testFlywayConfigInjection() {
    MigrationInfo migrationInfo = flywayUsers.info().current();
    assertNotNull(migrationInfo, "No Flyway migration was executed");

    String currentVersion = migrationInfo
            .getVersion()
            .toString();
    // Expected to be 1.0.0 as migration runs at start
    assertEquals("1.0.0", currentVersion);
}
 
Example #12
Source File: FlywayExtensionCleanAtStartTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Clean at start correctly")
public void testFlywayConfigInjection() throws SQLException {

    try (Connection connection = defaultDataSource.getConnection(); Statement stat = connection.createStatement()) {
        try (ResultSet executeQuery = stat.executeQuery("select * from fake_existing_tbl")) {
            fail("fake_existing_tbl should not exist");
        } catch (JdbcSQLException e) {
            // expected fake_existing_tbl does not exist
        }
    }
    MigrationInfo current = flyway.info().current();
    assertNull(current, "Info is not null");
}
 
Example #13
Source File: StatisticsFlywayWrapper.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public MigrationInfo[] getPendingMigrationsInfo() {
    try {
        readWriteLock.readLock().lock();
        return pendingMigrationsInfo;
    } finally {
        readWriteLock.readLock().unlock();
    }
}
 
Example #14
Source File: StatisticsFlywayWrapper.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public MigrationInfo getCurrentDbVersionInfo() {
    try {
        readWriteLock.readLock().lock();
        return currentDbVersionInfo;
    } finally {
        readWriteLock.readLock().unlock();
    }
}
 
Example #15
Source File: MigrationResource.java    From ameba with MIT License 5 votes vote down vote up
/**
 * Retrieves the full set of infos about future migrations applied to the DB.
 *
 * @return The future migrations. An empty array if none.
 */
private List<MigrationInfo> future(MigrationInfo[] migrationInfos) {
    if (migrationInfos.length == 0)
        throw new NotFoundException();
    List<MigrationInfo> futureMigrations = Lists.newArrayList();
    for (MigrationInfo migrationInfo : migrationInfos) {
        if ((migrationInfo.getState() == MigrationState.FUTURE_SUCCESS)
                || (migrationInfo.getState() == MigrationState.FUTURE_FAILED)) {
            futureMigrations.add(migrationInfo);
        }
    }

    return futureMigrations;
}
 
Example #16
Source File: FlywayMigrationsTask.java    From web-budget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void run() {

    checkNotNull(this.dataSource, "No datasource found for migrations");

    final Flyway flyway = Flyway.configure()
            .dataSource(this.dataSource)
            .locations("db/migrations")
            .baselineOnMigrate(true)
            .baselineVersion("0")
            .sqlMigrationPrefix("")
            .load();

    final MigrationInfo migrationInfo = flyway.info().current();

    if (migrationInfo == null) {
        this.logger.info("No existing database at the actual datasource");
    } else {
        this.logger.info("Current version: {}", migrationInfo.getVersion() + " : " + migrationInfo.getDescription());
    }

    try {
        flyway.migrate();
        this.logger.info("Successfully migrated to version: {}", flyway.info().current().getVersion());
    } catch (FlywayException ex) {
        this.logger.info("Flyway migrations failed!", ex);
    }
}
 
Example #17
Source File: ManualFlywayMigrationIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
private void assertAllMigrationsAre(MigrationState expectedState) {
    for (MigrationInfo migrationInfo : flyway.info().all()) {
        assertThat(migrationInfo.getState()).isEqualTo(expectedState);
    }
}
 
Example #18
Source File: ExampleFlywayCallback.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void afterEachMigrate(Connection connection, MigrationInfo info) {
    log.info("> afterEachMigrate");
}
 
Example #19
Source File: ExampleFlywayCallback.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
    log.info("> beforeEachMigrate");
}
 
Example #20
Source File: FlywayReport.java    From micronaut-flyway with Apache License 2.0 4 votes vote down vote up
/**
 * @param name       The name of the data source
 * @param changeSets The list of changes
 */
public FlywayReport(String name, List<MigrationInfo> changeSets) {
    this.name = name;
    this.migrations = changeSets;
}
 
Example #21
Source File: StatisticsService.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public MigrationInfo[] getPendingMigrationsInfo() {
	return statisticsFlywayWrapper.getPendingMigrationsInfo();
}
 
Example #22
Source File: StatisticsService.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public MigrationInfo getCurrentDbVersionInfo() {
	return statisticsFlywayWrapper.getCurrentDbVersionInfo();
}
 
Example #23
Source File: StatisticsConfigBean.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public String getDbVersion() {

        if (BeanUtil.getSfContext().getStatisticsService().isConnected()) {

            MigrationInfo versionInfo = BeanUtil.getSfContext().getStatisticsService().getCurrentDbVersionInfo();

            if (versionInfo != null) {

                return versionInfo.getVersion().getVersion();

            }

        }

        return "";

    }
 
Example #24
Source File: FlywayReport.java    From micronaut-flyway with Apache License 2.0 4 votes vote down vote up
/**
 * @return The list of change migrations
 */
@JsonSerialize(contentAs = MigrationInfo.class)
public List<MigrationInfo> getMigrations() {
    return migrations;
}
 
Example #25
Source File: DatabaseClient.java    From datamill with ISC License 2 votes vote down vote up
@Override
public void afterEachMigrate(Connection connection, MigrationInfo info) {

}
 
Example #26
Source File: DatabaseClient.java    From datamill with ISC License 2 votes vote down vote up
@Override
public void beforeEachMigrate(Connection connection, MigrationInfo info) {

}
 
Example #27
Source File: StatisticsFlywayWrapper.java    From sailfish-core with Apache License 2.0 2 votes vote down vote up
public void init(HibernateStorageSettings settings) {
    try {
        readWriteLock.writeLock().lock();
        migrationRequired = false;

        sfUpdateRequired = false;

        flyway = new Flyway();

        flyway.getPlaceholders().put("db_name", settings.getDbName());

        String[] initSqls = new String[0];

        if (settings.getDbms().equals(DbmsType.PostgreSQL.getValue())) {
            flyway.setLocations(PSQL_SCRIPT_LOCATION);
        }

        if (settings.getDbms().equals(DbmsType.MySql.getValue())) {
            flyway.setLocations(MYSQL_SCRIPT_LOCATION);
            initSqls = MYSQL_INIT_SQL;
        }

        flyway.setDataSource(settings.buildConnectionUrl(), settings.getUsername(), settings.getPassword(), initSqls);

        flyway.setBaselineOnMigrate(false);

        // Get info about migrations

        MigrationInfoService info = flyway.info();

        currentDbVersionInfo = info.current();

        pendingMigrationsInfo = info.pending();

        MigrationInfo[] all = info.all();

        // Checks

        if (currentDbVersionInfo == null) {
            migrationRequired = true;

            throw new OlderSchemaException("DB initialization is required");

        }

        if (pendingMigrationsInfo.length != 0) {
            migrationRequired = true;

            throw new OlderSchemaException("Migration to version "
                    + pendingMigrationsInfo[pendingMigrationsInfo.length - 1].getVersion().getVersion()
                    + " is required");

        }

        if (all.length != 0) {
            MigrationInfo lastKnown = all[all.length - 1];

            if(lastKnown.getState() == MigrationState.FUTURE_SUCCESS) {

                sfUpdateRequired = true;

                throw new NewerSchemaException("DB schema has newer version " + lastKnown.getVersion().getVersion()
                        + ". Upgrade this Sailfish instance to use it.");
            }
        }
    } finally {
        readWriteLock.writeLock().unlock();
    }
}