我的Java迁移目前没有实现MigrationChecksumProvider接口。我想知道他们是否应该。
我不明白校验球在Flyway中扮演什么角色。请问有人向我解释一下吗?此外,如果我实现MigrationChecksumProvider,如何为给定的迁移计算校验和?
谢谢。
答案 0 :(得分:1)
我认为大多数时候你想根据相关类文件的内容来计算校验和。我们这样做......
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
import org.flywaydb.core.internal.util.FileCopyUtils;
import org.flywaydb.core.internal.util.logging.Log;
import org.flywaydb.core.internal.util.logging.LogFactory;
/**
* Utility class for checksum calculations.
*
*/
public final class Checksums {
private static final Log LOG = LogFactory.getLog(Checksums.class);
private Checksums() {
super(); // singleton
}
/**
* Calculates a checksum based on the given JdbcMigration. It builds the checksum from the byte content of the given
* migration class file using the same {@link CRC32} method as used by
* {@link org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver}
*
* @param migration
* @return the checksum
*/
public static final Integer checksum(JdbcMigration migration) {
Integer checksum = null;
Class<?> migrationClass = migration.getClass();
String migrationClassFilePath = migrationClass.getName().replace(".", System.getProperty("file.separator")) + ".class";
InputStream inputStream = null;
try {
inputStream = ClassLoader.getSystemResourceAsStream(migrationClassFilePath);
byte[] classContent = FileCopyUtils.copyToByteArray(inputStream);
final CRC32 crc32 = new CRC32();
crc32.update(classContent);
checksum = (int) crc32.getValue();
} catch (IOException ioe) {
LOG.error("Problem calculating checksum for class " + migrationClass.getName(), ioe);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ioe) {
LOG.error("Problem closing input stream to " + migrationClassFilePath, ioe);
}
}
}
return checksum;
}
}
然后你可以创建一个类似于
的基类public abstract class JdbcMigrationWithChecksum implements JdbcMigration, MigrationChecksumProvider {
@Override
public Integer getChecksum() {
return Checksums.checksum(this);
}
}
答案 1 :(得分:0)
校验和用于迁移的(意外)更改检测,这将无法保证可以完全重新创建架构。
对于Java迁移,天空是您可以执行的操作的限制。因此,您可以调用如何实现校验和以检测更改。