使用 gh-ost 与 flyway/liquibase 等工具集成的在线迁移
Online migration with gh-ost integrated with tools like flyway/liquibase
我一直在研究如何使用 gh-ost,但似乎尚未与 flyway/liquibase 等工具集成。 gh-ost 必须像这样 运行:
./gh-ost --host=XXX--user=XXXX--password=XXXX--database=XXX--table=XXX --alter="ADD COLUMN XXX INT NOT NULL DEFAULT '0'"
似乎 table 名称和 "alter" sql 命令是 gh-ost 命令参数的一部分。
有什么方法可以让我使用 gh-ost 的好处(在线模式迁移)以及 flyway/liquibase 之类的工具所提供的功能?
看起来不简单。使用 Flyway,您可以使用 Custom Migration resolvers & executors 将特殊文件包装在 gh-ost 命令中。对于概念验证,您可以使用 java class 迁移来调用操作系统 运行 Gh-ost 命令。
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
}
}
我一直在研究如何使用 gh-ost,但似乎尚未与 flyway/liquibase 等工具集成。 gh-ost 必须像这样 运行:
./gh-ost --host=XXX--user=XXXX--password=XXXX--database=XXX--table=XXX --alter="ADD COLUMN XXX INT NOT NULL DEFAULT '0'"
似乎 table 名称和 "alter" sql 命令是 gh-ost 命令参数的一部分。
有什么方法可以让我使用 gh-ost 的好处(在线模式迁移)以及 flyway/liquibase 之类的工具所提供的功能?
看起来不简单。使用 Flyway,您可以使用 Custom Migration resolvers & executors 将特殊文件包装在 gh-ost 命令中。对于概念验证,您可以使用 java class 迁移来调用操作系统 运行 Gh-ost 命令。
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
}
}