org.flywaydb.core.api.FlywayException Java Examples

The following examples show how to use org.flywaydb.core.api.FlywayException. 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: DatabaseMigrationResolver.java    From ameba with MIT License 6 votes vote down vote up
static int calculateChecksum(String str) {
    Hasher hasher = Hashing.murmur3_32().newHasher();

    BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
    try {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            hasher.putString(line.trim(), Charsets.UTF_8);
        }
    } catch (IOException e) {
        String message = "Unable to calculate checksum";
        throw new FlywayException(message, e);
    }

    return hasher.hash().asInt();
}
 
Example #2
Source File: AbstractFlywayCommand.java    From dropwizard-flyway with Apache License 2.0 6 votes vote down vote up
@Override
protected void run(final Bootstrap<T> bootstrap, final Namespace namespace, final T configuration) throws Exception {
    final PooledDataSourceFactory datasourceFactory = databaseConfiguration.getDataSourceFactory(configuration);
    final FlywayFactory flywayFactory = flywayConfiguration.getFlywayFactory(configuration);

    // Give subclasses an option to set additional config flags for flyway.
    setAdditionalOptions(flywayFactory, namespace);
    
    final Flyway flyway = flywayFactory.build(datasourceFactory.build(bootstrap.getMetricRegistry(), "Flyway"));

    try {
        run(namespace, flyway);
    } catch (FlywayException e) {
        LOG.error("Error while running database command", e);
        throw e;
    }
}
 
Example #3
Source File: ShellMigrationExecutor.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Connection connection) throws SQLException {
    String scriptLocation = this.shellScriptResource.getLocationOnDisk();
    try {
        List<String> args = new ArrayList<String>();
        args.add(scriptLocation);
        ProcessBuilder builder = new ProcessBuilder(args);
        builder.redirectErrorStream(true);
        Process process = builder.start();
        Scanner in = new Scanner(process.getInputStream());
        System.out.println(StringUtils.repeat("+",200));
        while (in.hasNextLine()) {
            System.out.println(in.nextLine());
        }
        int returnCode = process.waitFor();
        System.out.println(StringUtils.repeat("+",200));
        if (returnCode != 0) {
            throw new FlywayException("script exited with value : " + returnCode);
        }
    } catch (Exception e) {
        LOG.error(e.toString());
        // Only if SQLException or FlywaySqlScriptException is thrown flyway will mark the migration as failed in the metadata table
        throw new SQLException(String.format("Failed to run script \"%s\", %s", scriptLocation, e.getMessage()), e);
    }
}
 
Example #4
Source File: SqlScriptCreator.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create a new instance of script based on location and resource.
 *
 * @param location root location of the given resource
 * @param resource script resource
 * @return a new instance of sql script based on location and resource
 * @throws FlywayException when script can't be created from the resource
 */
SqlScript createScript(Location location, Resource resource) {
  final String separator = location.isClassPath() ? "/" : File.separator;
  // '/root-location/5.0.0-M7/v1__init.sql' -> '5.0.0-M7/v1__init.sql'
  final String relLocation = resource.getLocation().substring(location.getPath().length() + 1);
  final String[] paths = relLocation.split(separator);
  // 5.0.0-M1/v1__init.sql
  if (paths.length == 2) {
    return new SqlScript(resource, location, paths[0], null, paths[1]);
  }
  // 5.0.0-M1/postgresql/v1__init.sql
  if (paths.length == 3) {
    return new SqlScript(resource, location, paths[0], paths[1], paths[2]);
  }
  throw new FlywayException(
      format(
          "Sql script location must be either in 'location-root/version-dir' "
              + "or in 'location-root/version-dir/provider-name', but script '%s' is not in root '%s'",
          resource.getLocation(), location.getPath()));
}
 
Example #5
Source File: VersionResolver.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates migration version based on script data.
 *
 * @param script script for which to resolve the version
 * @param configuration flyway configuration used for resolution parameters
 */
MigrationVersion resolve(SqlScript script, FlywayConfiguration configuration) {
  String normalizedDir = normalizedDirs.get(script.dir);
  if (normalizedDir == null) {
    // 5.0.0-M1 -> 5.0.0.M1 -> 5.0.0.1
    normalizedDir =
        NOT_VERSION_CHARS_PATTERN.matcher(script.dir.replace("-", ".")).replaceAll("");
    normalizedDirs.put(script.dir, normalizedDir);
  }

  // separate version from the other part of the name
  final int sepIdx = script.name.indexOf(configuration.getSqlMigrationSeparator());
  if (sepIdx == -1) {
    throw new FlywayException(
        format(
            "sql script name '%s' is not valid, name must contain '%s'",
            script.name, configuration.getSqlMigrationSeparator()));
  }

  // check whether part before separator is not empty
  String version = script.name.substring(0, sepIdx);
  if (version.isEmpty()) {
    throw new FlywayException(
        format(
            "sql script name '%s' is not valid, name must provide version like "
                + "'%s4%smigration_description.sql",
            configuration.getSqlMigrationPrefix(),
            script.name,
            configuration.getSqlMigrationSeparator()));
  }

  // extract sql script version without prefix
  final String prefix = configuration.getSqlMigrationPrefix();
  if (!isNullOrEmpty(prefix) && script.name.startsWith(prefix)) {
    version = version.substring(prefix.length());
  }
  return MigrationVersion.fromVersion(normalizedDir + '.' + version);
}
 
Example #6
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 #7
Source File: TinkerTimeLauncher.java    From TinkerTime with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
	// Perform Database Migration
	Flyway flyway = new Flyway();
	flyway.setBaselineOnMigrate(true);
	flyway.setLocations("io/andrewohara/tinkertime/db/migration");
	flyway.setDataSource(connectionString.getUrl(), null, null);
	try {
		flyway.migrate();
	} catch (FlywayException e){
		flyway.repair();
		throw e;
	}
}
 
Example #8
Source File: DatabaseJobHistoryStore.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
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 #10
Source File: VersionResolverTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test(dataProvider = "invalidScripts", expectedExceptions = FlywayException.class)
public void failsToResolveVersions(String dir, String name) throws Exception {
  final SqlScript script =
      new SqlScript(
          new FileSystemResource("sql/" + dir + "/" + name),
          new Location("filesystem:sql"),
          dir,
          null,
          name);
  resolver.resolve(script, flyway);
}
 
Example #11
Source File: SqlScriptCreatorTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test(expectedExceptions = FlywayException.class)
public void failsToCreateResourceWhenPathIsInvalid() throws Exception {
  final Location location = new Location("filesystem:schema");
  final Resource resource = new FileSystemResource("schema/v1__init.sql");

  new SqlScriptCreator().createScript(location, resource);
}
 
Example #12
Source File: SchemaMigratorIntegrationTest.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #13
Source File: V2_31_2__Job_configuration_param_to_jsonb.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Serializes an object to JSON.
 *
 * @param object the object to convert.
 * @return JSON content.
 */
private String convertObjectToJson( Object object )
{
    try
    {
        return writer.writeValueAsString( object );
    }
    catch ( IOException e )
    {
        log.error( "Flyway java migration error:", e );
        throw new FlywayException( e );
    }
}
 
Example #14
Source File: CustomFlywayConfiguration.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the database type from the given data source.
 *
 * @param dataSource the data source
 * @return the database type
 */
private DatabaseType getDatabaseType(final DataSource dataSource) {
    try (final Connection connection = dataSource.getConnection()) {
        return DatabaseType.fromJdbcConnection(connection);
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        throw new FlywayException("Unable to obtain connection from Flyway DataSource", e);
    }
}
 
Example #15
Source File: CustomFlywayMigrationStrategy.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the database represented by this data source is being initialized for the first time based on
 * whether or not the table named 'BUCKET' or 'bucket' already exists.
 *
 * @param dataSource the data source
 * @return true if the database has never been initialized before, false otherwise
 */
private boolean isNewDatabase(final DataSource dataSource) {
    try (final Connection connection = dataSource.getConnection();
         final ResultSet rsUpper = connection.getMetaData().getTables(null, null, "BUCKET", null);
         final ResultSet rsLower = connection.getMetaData().getTables(null, null, "bucket", null)) {
        return !rsUpper.next() && !rsLower.next();
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        throw new FlywayException("Unable to obtain connection from Flyway DataSource", e);
    }
}
 
Example #16
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 #17
Source File: OptimizedFlywayTestExecutionListener.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
protected void originalDbReset(TestContext testContext, FlywayTest annotation) {
    try {
        String dbResetMethodName = repeatableAnnotationPresent ? "dbResetWithAnnotation" : "dbResetWithAnotation";
        invokeMethod(this, dbResetMethodName, testContext, annotation);
    } catch (FlywayException e) {
        if (e.getCause() instanceof SQLException) {
            String errorCode = ((SQLException) e.getCause()).getSQLState();
            if (errorCode != null && errorCode.matches("(42723|42P06|42P07|42712|42710)")) {
                logger.error("\n\nHINT: Check that you have correctly set org.flywaydb.core.Flyway#schemaNames property!!!\n");
            }
        }
        throw e;
    }
}
 
Example #18
Source File: CustomSqlMigrationResolver.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
private List<ResolvedMigration> resolveSqlMigrations() throws IOException, SQLException {
  LOG.info(
      "Searching for SQL scripts in locations {}",
      Arrays.toString(flywayConfiguration.getLocations()));
  final Map<Location, List<Resource>> allResources = finder.findResources(flywayConfiguration);
  LOG.debug("Found scripts: {}", allResources);

  final Map<String, Map<String, SqlScript>> scriptsInDir = new HashMap<>();
  for (Location location : allResources.keySet()) {
    final List<Resource> resources = allResources.get(location);
    for (Resource resource : resources) {
      final SqlScript newScript = scriptsCreator.createScript(location, resource);
      if (!scriptsInDir.containsKey(newScript.dir)) {
        scriptsInDir.put(newScript.dir, new HashMap<>(4));
      }
      final Map<String, SqlScript> existingScripts = scriptsInDir.get(newScript.dir);
      final SqlScript existingScript = existingScripts.get(newScript.name);
      if (existingScript == null) {
        existingScripts.put(newScript.name, newScript);
      } else if (Objects.equals(existingScript.vendor, newScript.vendor)) {
        throw new FlywayException(
            format(
                "More than one script with name '%s' is registered for "
                    + "database vendor '%s', script '%s' conflicts with '%s'",
                newScript.name, existingScript.vendor, newScript, existingScript));
      } else if (vendorName.equals(newScript.vendor)) {
        existingScripts.put(newScript.name, newScript);
      }
    }
  }

  final Map<MigrationVersion, ResolvedMigration> migrations = new HashMap<>();
  for (SqlScript script :
      scriptsInDir
          .values()
          .stream()
          .flatMap(scripts -> scripts.values().stream())
          .collect(toList())) {
    final ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
    migration.setVersion(versionResolver.resolve(script, flywayConfiguration));
    migration.setScript(script.resource.getLocation());
    migration.setPhysicalLocation(script.resource.getLocationOnDisk());
    migration.setType(MigrationType.SQL);
    migration.setDescription(script.name);
    migration.setChecksum(
        ByteSource.wrap(script.resource.loadAsBytes()).hash(Hashing.crc32()).asInt());
    migration.setExecutor(
        new SqlMigrationExecutor(
            dbSupport, script.resource, placeholderReplacer, flywayConfiguration));
    if (migrations.put(migration.getVersion(), migration) != null) {
      throw new FlywayException("Two migrations with the same version detected");
    }
  }
  return new ArrayList<>(migrations.values());
}
 
Example #19
Source File: V2_33_1__Job_configuration_job_type_column_to_varchar.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void migrate( final Context context ) throws Exception
{
    //1. Check whether migration is needed at all. Maybe it was already applied. -> Achieves that script can be
    // run multiple times without worries
    boolean continueWithMigration = false;
    String sql = "SELECT data_type FROM information_schema.columns " +
        "WHERE table_name = 'jobconfiguration' AND column_name = 'jobtype';";
    try ( Statement stmt = context.getConnection().createStatement();
          ResultSet rs = stmt.executeQuery( sql ); )
    {
        if ( rs.next() && rs.getString( "data_type" ).equals( "bytea" ))
        {
            continueWithMigration = true;
        }
    }

    if ( continueWithMigration )
    {
        //2. Create a new JobType column of type VARCHAR in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS jobtypevarchar VARCHAR(120)" );
        }

        //3. Move existing jobtype from bytearray column into varchar column
        Map<Integer, byte[]> jobTypeByteMap = new HashMap<>();
        sql = "SELECT jobconfigurationid, jobtype FROM jobconfiguration WHERE jobtype IS NOT NULL";
        try ( Statement stmt = context.getConnection().createStatement();
              ResultSet rs = stmt.executeQuery( sql ); )
        {
            while ( rs.next() )
            {
                jobTypeByteMap.put( rs.getInt( "jobconfigurationid" ), rs.getBytes( "jobtype" ) );
            }
        }

        jobTypeByteMap.forEach( ( id, jobTypeByteArray ) -> {

            JobType jobType = (JobType) SerializationUtils.deserialize( jobTypeByteArray );
            if ( jobType == null )
            {
                log.error( "Flyway java migration error: Parsing JobType byte array failed." );
                throw new FlywayException( "Parsing JobType byte array failed." );
            }

            try ( PreparedStatement ps = context.getConnection().prepareStatement(
                "UPDATE jobconfiguration SET jobtypevarchar = ? WHERE jobconfigurationid = ?" ) )
            {
                ps.setObject( 1, jobType.name() );
                ps.setInt( 2, id );

                ps.execute();

            }
            catch ( SQLException e )
            {
                log.error( "Flyway java migration error:", e );
                throw new FlywayException( e );
            }
        } );

        //4. Delete old byte array column for JobType in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration DROP COLUMN jobtype" );
        }

        //5. Rename new jobtypevarchar column to the name of the now deleted column
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration RENAME COLUMN jobtypevarchar TO jobtype" );
        }
    }
}
 
Example #20
Source File: V2_34_5__Convert_job_configuration_binary_columns_into_varchar_data_type.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void migrateLastExecutedStatusColumn( final Context context ) throws Exception
{
    //1. Check whether migration is needed at all. Maybe it was already applied. -> Achieves that script can be
    // run multiple times without worries
    boolean continueWithMigration = false;
    try ( Statement stmt = context.getConnection().createStatement();
          ResultSet rs = stmt.executeQuery( CHECK_LAST_EXECUTED_STATUS_DATA_TYPE_SQL ); )
    {
        if ( rs.next() && rs.getString( "data_type" ).equals( "bytea" ) )
        {
            continueWithMigration = true;
        }
    }

    if ( continueWithMigration )
    {
        //2. Create a new LastExecutedStatus column of type VARCHAR in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS lastexecutedstatusvarchar VARCHAR(120)" );
        }

        //3. Move existing lastexecutedstatus from bytearray column into varchar column
        Map<Integer, byte[]> lastExecutedStatusByteMap = new HashMap<>();
        String sql = "SELECT jobconfigurationid, lastexecutedstatus FROM jobconfiguration " +
            "WHERE lastexecutedstatus IS NOT NULL";
        try ( Statement stmt = context.getConnection().createStatement();
              ResultSet rs = stmt.executeQuery( sql ); )
        {
            while ( rs.next() )
            {
                lastExecutedStatusByteMap.put( rs.getInt( "jobconfigurationid" ),
                    rs.getBytes( "lastexecutedstatus" ) );
            }
        }

        lastExecutedStatusByteMap.forEach( ( id, lastExecutedStatusByteArray ) -> {

            JobStatus lastExecutedStatus = (JobStatus) SerializationUtils.deserialize( lastExecutedStatusByteArray );
            if ( lastExecutedStatus == null )
            {
                log.error( "Flyway java migration error: Parsing LastExecutedStatus byte array failed." );
                throw new FlywayException( "Parsing LastExecutedStatus byte array failed." );
            }

            try ( PreparedStatement ps = context.getConnection().prepareStatement(
                "UPDATE jobconfiguration SET lastexecutedstatusvarchar = ? WHERE jobconfigurationid = ?" ) )
            {
                ps.setObject( 1, lastExecutedStatus.name() );
                ps.setInt( 2, id );

                ps.execute();

            }
            catch ( SQLException e )
            {
                log.error( "Flyway java migration error:", e );
                throw new FlywayException( e );
            }
        } );

        //4. Delete old byte array column for LastExecutedStatus in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration DROP COLUMN lastexecutedstatus" );
        }

        //5. Rename new lastexecutedstatusvarchar column to the name of the now deleted column
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration RENAME COLUMN lastexecutedstatusvarchar TO lastexecutedstatus" );
        }

        //6. Set default values where NULL is present
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "UPDATE jobconfiguration SET lastexecutedstatus = 'NOT_STARTED' WHERE lastexecutedstatus IS NULL" );
        }
    }
}
 
Example #21
Source File: V2_34_5__Convert_job_configuration_binary_columns_into_varchar_data_type.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void migrateJobStatusColumn( final Context context ) throws Exception
{
    //1. Check whether migration is needed at all. Maybe it was already applied. -> Achieves that script can be
    // run multiple times without worries
    boolean continueWithMigration = false;
    try ( Statement stmt = context.getConnection().createStatement();
          ResultSet rs = stmt.executeQuery( CHECK_JOB_STATUS_DATA_TYPE_SQL ); )
    {
        if ( rs.next() && rs.getString( "data_type" ).equals( "bytea" ))
        {
            continueWithMigration = true;
        }
    }

    if ( continueWithMigration )
    {
        //2. Create a new JobStatus column of type VARCHAR in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS jobstatusvarchar VARCHAR(120)" );
        }

        //3. Move existing jobstatus from bytearray column into varchar column
        Map<Integer, byte[]> jobStatusByteMap = new HashMap<>();
        String sql = "SELECT jobconfigurationid, jobstatus FROM jobconfiguration WHERE jobstatus IS NOT NULL";
        try ( Statement stmt = context.getConnection().createStatement();
              ResultSet rs = stmt.executeQuery( sql ); )
        {
            while ( rs.next() )
            {
                jobStatusByteMap.put( rs.getInt( "jobconfigurationid" ), rs.getBytes( "jobstatus" ) );
            }
        }

        jobStatusByteMap.forEach( ( id, jobStatusByteArray ) -> {

            JobStatus jobStatus = (JobStatus) SerializationUtils.deserialize( jobStatusByteArray );
            if ( jobStatus == null )
            {
                log.error( "Flyway java migration error: Parsing JobStatus byte array failed." );
                throw new FlywayException( "Parsing JobStatus byte array failed." );
            }

            try ( PreparedStatement ps = context.getConnection().prepareStatement(
                "UPDATE jobconfiguration SET jobstatusvarchar = ? WHERE jobconfigurationid = ?" ) )
            {
                ps.setObject( 1, jobStatus.name() );
                ps.setInt( 2, id );

                ps.execute();

            }
            catch ( SQLException e )
            {
                log.error( "Flyway java migration error:", e );
                throw new FlywayException( e );
            }
        } );

        //4. Delete old byte array column for JobStatus in jobconfiguration table
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration DROP COLUMN jobstatus" );
        }

        //5. Rename new jobstatusvarchar column to the name of the now deleted column
        try ( Statement stmt = context.getConnection().createStatement() )
        {
            stmt.executeUpdate( "ALTER TABLE jobconfiguration RENAME COLUMN jobstatusvarchar TO jobstatus" );
        }
    }
}
 
Example #22
Source File: DatabaseJobHistoryStoreSchemaManager.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
public void migrate() throws FlywayException {
  flyway.migrate();
}