1

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?

PetruC
  • 23
  • 6

2 Answers2

1
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
    }
}
PetruC
  • 23
  • 6
0

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.

  • 1
    I've add a new MigrationResolver and for his flyway executor I've added a new MigrationExecutor. In this new executor I simply made a system call to gh-ost. If I'm identifying any exception then I simply throw a SQLException so that it can be picked up by flyway so that it can mark the migration as not successful. I'll probably have to tweak it more and test it against a real life database but so far it looks good. – PetruC Sep 14 '17 at 11:54
  • Petru, Do you have a repo I can take a look at, as an example? I can't find many docs or examples about how to implement an ExecutionMigrator. TIA. – Mark Melville Nov 29 '18 at 16:25
  • @MarkMelville I don't have a pulbic repo. See the comment I've added (sorry for late response) – PetruC Dec 12 '18 at 10:49