版本
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.0.8</version>
</dependency>
支持的属性,前缀 spring.flyway
FlywayProperties:
baselineDescription: String
baselineOnMigrate: boolean
baselineVersion: String
batch: Boolean
checkLocation: boolean
cleanDisabled: boolean
cleanOnValidationError: boolean
connectRetries: int
createDataSource: boolean
defaultSchema: String
dryRunOutput: File
enabled: boolean
encoding: Charset
errorOverrides: String[]
group: boolean
ignoreFutureMigrations: boolean
ignoreIgnoredMigrations: boolean
ignoreMissingMigrations: boolean
ignorePendingMigrations: boolean
initSqls: List<String>
installedBy: String
licenseKey: String
locations: List<String>
mixed: boolean
oracleSqlplus: Boolean
oracleSqlplusWarn: Boolean
outOfOrder: boolean
password: String
placeholderPrefix: String
placeholderReplacement: boolean
placeholders: Map<String, String>
placeholderSuffix: String
repeatableSqlMigrationPrefix: String
schemas: List<String>
skipDefaultCallbacks: boolean
skipDefaultResolvers: boolean
sqlMigrationPrefix: String
sqlMigrationSeparator: String
sqlMigrationSuffixes: List<String>
stream: Boolean
table: String
tablespace: String
target: String
undoSqlMigrationPrefix: String
url: String
user: String
validateMigrationNaming: boolean
validateOnMigrate: boolean
使用 flyway 报错
报错一:找不到对应的表 flyway_schema_history
代码:
@Test
public void testFlyway_001() {
String url = "jdbc:mysql://localhost:3306/scala_demo?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false";
String user = "";
String password = "xiaohui";
Flyway flyway = Flyway.configure().dataSource(url, user, password).connectRetries(1).load();
flyway.migrate();
}
错误信息:
org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `scala_demo` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
根据提示,需要设置 .baselineOnMigrate(true)
报错二:DB: INSERT, CREATE command denied to user ‘’@’localhost’ for table ‘flyway_schema_history’ (SQL State: 42000 - Error Code: 1142)
代码:
@Test
public void testFlyway_001() {
String url = "jdbc:mysql://localhost:3306/scala_demo?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false";
String user = "";
String password = "xiaohui";
Flyway flyway = Flyway.configure().dataSource(url, user, password).baselineOnMigrate(true).connectRetries(1).load();
flyway.migrate();
}
错误信息:
21:49:05.581 [main] DEBUG org.flywaydb.core.internal.parser.Parser - Parsing ...
21:49:05.589 [main] DEBUG org.flywaydb.core.internal.sqlscript.ParserSqlScript - Found statement at line 1: CREATE TABLE `scala_demo`.`flyway_schema_history` (
`installed_rank` INT NOT NULL,
`version` VARCHAR(50),
`description` VARCHAR(200) NOT NULL,
`type` VARCHAR(20) NOT NULL,
`script` VARCHAR(1000) NOT NULL,
`checksum` INT,
`installed_by` VARCHAR(100) NOT NULL,
`installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`execution_time` INT NOT NULL,
`success` BOOL NOT NULL,
CONSTRAINT `flyway_schema_history_pk` PRIMARY KEY (`installed_rank`)
) ENGINE=InnoDB AS SELECT 1 as "installed_rank", '1' as "version", '<< Flyway Baseline >>' as "description", 'BASELINE' as "type", '<< Flyway Baseline >>' as "script", NULL as "checksum", '' as "installed_by", CURRENT_TIMESTAMP as "installed_on", 0 as "execution_time", TRUE as "success"
21:49:05.589 [main] DEBUG org.flywaydb.core.internal.sqlscript.ParserSqlScript - Found statement at line 15: CREATE INDEX `flyway_schema_history_s_idx` ON `scala_demo`.`flyway_schema_history` (`success`)
21:49:05.589 [main] DEBUG org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - Executing SQL: CREATE TABLE `scala_demo`.`flyway_schema_history` (
`installed_rank` INT NOT NULL,
`version` VARCHAR(50),
`description` VARCHAR(200) NOT NULL,
`type` VARCHAR(20) NOT NULL,
`script` VARCHAR(1000) NOT NULL,
`checksum` INT,
`installed_by` VARCHAR(100) NOT NULL,
`installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`execution_time` INT NOT NULL,
`success` BOOL NOT NULL,
CONSTRAINT `flyway_schema_history_pk` PRIMARY KEY (`installed_rank`)
) ENGINE=InnoDB AS SELECT 1 as "installed_rank", '1' as "version", '<< Flyway Baseline >>' as "description", 'BASELINE' as "type", '<< Flyway Baseline >>' as "script", NULL as "checksum", '' as "installed_by", CURRENT_TIMESTAMP as "installed_on", 0 as "execution_time", TRUE as "success"
21:49:05.593 [main] WARN org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - DB: INSERT, CREATE command denied to user ''@'localhost' for table 'flyway_schema_history' (SQL State: 42000 - Error Code: 1142)
21:49:05.593 [main] DEBUG org.flywaydb.core.internal.jdbc.TransactionTemplate - Rolling back transaction...
21:49:05.594 [main] DEBUG org.flywaydb.core.internal.jdbc.TransactionTemplate - Transaction rolled back
21:49:05.595 [main] DEBUG org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory - Schema History table creation failed. Retrying in 1 sec ...
查看用户及权限:
select current_user(), VERSION();
show grants;
-- 结果
user version
@localhost 5.7.21
--
GRANT USAGE ON *.* TO ''@'localhost'
GRANT SELECT (age, id, name) ON `scala_demo`.`user_domain` TO ''@'localhost'
修改权限
-- 使用超管用户 root 授权
grant all PRIVILEGES on scala_demo.* to ''@localhost with grant option;
-- 登录匿名用户,查看新的权限
GRANT USAGE ON *.* TO ''@'localhost'
GRANT ALL PRIVILEGES ON `scala_demo`.* TO ''@'localhost' WITH GRANT OPTION
GRANT SELECT (age, id, name) ON `scala_demo`.`user_domain` TO ''@'localhost'
报错三:没有权限查询 performance_schema.user_variables_by_thread
org.flywaydb.core.internal.database.mysql.MySQLConnection - Disabled user variable reset as performance_schema.user_variables_by_threadcannot be queried (SQL State: 42000, Error Code: 1142)
授权查询 performance_schema.user_variables_by_thread
-- 使用超管权限授权
grant select on performance_schema.user_variables_by_thread to ''@localhost with grant option;
-- 登录当前业务用户查看权限
show grants;
-- 结果
GRANT USAGE ON *.* TO ''@'localhost'
GRANT ALL PRIVILEGES ON `scala_demo`.* TO ''@'localhost' WITH GRANT OPTION
GRANT SELECT (age, id, name) ON `scala_demo`.`user_domain` TO ''@'localhost'
GRANT SELECT ON `performance_schema`.`user_variables_by_thread` TO ''@'localhost' WITH GRANT OPTION
问题
baseline-on-migrate=true,后版本从 2 开始,不设定为 true,不会自动创建 flyway_schema_history
flyway.baseline-description= # 执行基线时标记已有Schema的描述
flyway.baseline-version=1 # 基线版本默认开始序号默认为1.
flyway.baseline-on-migrate=false # 针对非空数据库是否默认调用基线版本,这也是我们上面版本号从2开始的原因
flyway.check-location=false # 是否开启脚本检查,检查脚本是否存在,默认false
flyway.clean-on-validation-error=false # 验证错误时,是否自动清除数据库,高危操作!!!
flyway.enabled=true # 是否启用 flyway.
flyway.encoding=UTF-8 # 脚本编码.
flyway.ignore-failed-future-migration=true # 在读元数据表时,是否忽略失败的后续迁移.
flyway.init-sqls= # S获取连接后立即执行初始化的SQL语句
flyway.locations=classpath:db/migration # 脚本位置, 默认为classpath: db/migration.
flyway.out-of-order=false # 是否允许乱序(out of order)迁移
flyway.placeholder-prefix= # 设置每个占位符的前缀。 默认值: ${
flyway.placeholder-replacement=true # 是否要替换占位符。 默认值:true
flyway.placeholder-suffix=} # 设置占位符的后缀。 默认值: }
flyway.placeholders.*= # 设置占位符的值。
flyway.schemas= # Flyway管理的Schema列表,区分大小写。默认连接对应的默认Schema
flyway.sql-migration-prefix=V # 迁移脚本的文件名前缀, 默认值:V
flyway.sql-migration-separator=__ # 迁移脚本的分割符 默认双下划线
flyway.sql-migration-suffix=.sql # 迁移脚本的后缀 默认 .sql
flyway.table=schema_version # Flyway使用的Schema元数据表名称 默认schema_version
flyway.url= # 待迁移的数据库的JDBC URL。如果没有设置,就使用配置的主数据源
flyway.user= # 待迁移数据库的登录用户
flyway.password= # 待迁移数据库的登录用户密码
flyway.validate-on-migrate=true # 在运行迁移时是否要自动验证。默认值:true
只好调整 .baselineVersion(“0”) 的值,默认值是 1,当 baseline-on-migrate=true ,只会迁移 大于 baselineVersion 版本的脚本文件。
代码
```java @Test public void testFlyway_001() { String url = “jdbc:mysql://localhost:3306/scala_demo?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false”;
String user = “”; String password = “xiaohui”; Flyway flyway = Flyway.configure().dataSource(url, user, password)
/**
* 是否在针对没有架构历史记录表的非空架构执行迁移时自动调用基线。 然后,在执行迁移之前,此架构将使用基线版本作为baselineVersion 。 然后只会应用高于baselineVersion迁移。
* 这对于具有现有数据库的项目的初始 Flyway 生产部署很有用。
* 启用此功能时要小心,因为它删除了确保 Flyway 不会在配置错误的情况下迁移错误数据库的安全网!
*
* 参数:
* BaselineOnMigrate – 如果基线应该为非空模式调用,则为true否则为false 。 (默认: false )
*/
.baselineOnMigrate(true)
/**
* 设置版本以在执行基线时标记现有模式。
*
* 参数:
* 基线版本 – 执行基线时标记现有模式的版本。 (默认值:1)
*/
.baselineVersion("0")
// .schemas("mysql", "scala_demo")
.connectRetries(1).load();
flyway.migrate();
} ```