Online migration with gh-ost integrated with tools like flyway/liquibase

831 views Asked by At

I've been investigating how to use gh-ost and it seems that is not yet integrated with tools like flyway/liquibase. gh-ost has to be run like this:

./gh-ost --host=XXX--user=XXXX--password=XXXX--database=XXX--table=XXX --alter="ADD COLUMN XXX INT NOT NULL DEFAULT '0'"

It seems that the table name and the "alter" sql commands are part of gh-ost command parameters.

Is there any way I can use gh-ost benefits(online schema migration) with what a tool like flyway/liquibase has to offer?

2

There are 2 answers

3
Hamish Carpenter On BEST ANSWER

It doesn't look trivial. With Flyway, you could use the Custom Migration resolvers & executors to wrap special files in the gh-ost command. For a proof-of-concept you could use a java class migration to call out to the operating system to run the Gh-ost command.

0
PetruC On
private Flyway flyway = new Flyway();

GhostMigrationResolver ghostMigrationResolver = new GhostMigrationResolver();
flyway.setResolvers(ghostMigrationResolver);

public class GhostMigrationResolver extends BaseMigrationResolver {

    private Set<String> ghostScriptFiles = new HashSet<>();

    public void addGhostScript(String ghostUpdateScript) {
        ghostScriptFiles.add(ghostUpdateScript);
    }

    @Override
    public Collection<ResolvedMigration> resolveMigrations() {
        Set<ResolvedMigration> resolvedMigrations = new HashSet<>();
        for (String ghostScriptFile : ghostScriptFiles) {
            GhostResolvedMigration ghostResolvedMigration = new GhostResolvedMigration();
            ghostResolvedMigration.setScript(ghostScriptFile);
            GhostExecutor executor = new GhostExecutor();
            executor.setGhostScriptFile(ghostScriptFile);
            ghostResolvedMigration.setExecutor(executor);
            ghostResolvedMigration.setDescription(ghostScriptFile);
            ghostResolvedMigration.setVersion(MigrationVersion.fromVersion(ghostScriptFile.substring(1, ghostScriptFile.indexOf("__"))));
            ghostResolvedMigration.setType(MigrationType.CUSTOM);
            resolvedMigrations.add(ghostResolvedMigration);
        }
        return resolvedMigrations;
    }
}

public class GhostExecutor implements MigrationExecutor {

    @Override
    public void execute(Connection connection) throws SQLException {
    //call ghost here
    }
}