flyway的配置文件
https://documentation.red-gate.com/fd/configuration-files-224003079.html
由于官方文档写的比较不好,所以你可以在测试命令的时候加上 -X
注意flyway没有help,直接执行flyway相当于help
下面这个可以先忽略下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
➜ flyway flyway -configFiles="conf/flyway.toml" info -X
DEBUG: Loading config file: /snap/flyway/4/conf/flyway.conf
DEBUG: Unable to load config file: /home/liuliancao/flyway.conf
DEBUG: Unable to load config file: /home/liuliancao/projects/flyway/flyway.conf
DEBUG: Loading config file: /home/liuliancao/projects/flyway/conf/flyway.toml
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/aws-secretsmanager-jdbc-1.0.5.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/derby-10.15.2.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/derbyclient-10.15.2.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/derbyshared-10.15.2.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/derbytools-10.15.2.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/google-cloud-spanner-jdbc-1.16.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/google-cloud-storage-1.111.2.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/h2-1.4.200.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/hsqldb-2.5.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/jaybird-jdk18-3.0.8.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/jna-4.5.2.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/jna-platform-4.5.2.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/jtds-1.3.1.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/mariadb-java-client-2.6.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/mssql-jdbc-7.2.0.jre8.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/mysql-connector-java-8.0.17.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/ojdbc8-19.6.0.0.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/postgresql-42.2.14.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/snowflake-jdbc-3.11.1.jar
DEBUG: Adding location to classpath: /snap/flyway/4/drivers/sqlite-jdbc-3.30.1.jar
DEBUG: Using configuration:
DEBUG: [environments.default] ->
DEBUG: [flyway] ->
DEBUG: flyway.configFiles -> conf/flyway.toml
DEBUG: flyway.jarDirs -> /snap/flyway/4/jars
DEBUG: flyway.locations -> filesystem:sql
DEBUG: locations -> ["filesystem:migrations"]
DEBUG: password -> "12345"
DEBUG: url -> "jdbc:mysql://172.17.0.2:3366/flywaytest"
DEBUG: user -> "root"
DEBUG: Scanning for classpath resources at 'classpath:db/callback' ...
DEBUG: Determining location urls for classpath:db/callback using ClassLoader java.net.URLClassLoader@13a5fe33 ...
DEBUG: Unable to resolve location classpath:db/callback.
Flyway Community Edition 7.0.2 by Redgate
ERROR: Unexpected error
org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
at org.flywaydb.core.internal.configuration.ConfigurationValidator.validate(ConfigurationValidator.java:36)
at org.flywaydb.core.Flyway.execute(Flyway.java:447)
at org.flywaydb.core.Flyway.info(Flyway.java:356)
at org.flywaydb.commandline.Main.executeOperation(Main.java:249)
at org.flywaydb.commandline.Main.main(Main.java:152)
|
后来发现是配置文件问题,我直接命令行可以,所以建议大家刚开始也用命令行
测试
要测试flyway,首先应当注意目录,配置文件会从这里读
1
2
3
4
|
DEBUG: Loading config file: /snap/flyway/4/conf/flyway.conf
DEBUG: Unable to load config file: /home/liuliancao/flyway.conf
DEBUG: Unable to load config file: /home/liuliancao/projects/flyway/flyway.conf
DEBUG: Loading config file: /home/liuliancao/projects/flyway/conf/flyway.toml
|
如果是命令行 第一次会提示没有migrations
1
2
3
4
5
6
7
8
9
10
11
12
|
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" -placeholders.abc=migrations info
Flyway Community Edition 7.0.2 by Redgate
ERROR: Skipping filesystem location:sql (not found).
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@59d016c9 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@59d016c9 is 10.4.
Schema version: << Empty Schema >>
+----------+---------+-------------+------+--------------+-------+
| Category | Version | Description | Type | Installed On | State |
+----------+---------+-------------+------+--------------+-------+
| No migrations found |
+----------+---------+-------------+------+--------------+-------+
|
这里我们可以进行migrate,我上一个命令migrate位置应该部队,不过我发现是
从sql目录去读的,所以新建一个sql目录,放一个表
1
2
3
4
5
|
sql/V1__Create_person_table.sql
create table PERSON (
ID int not null,
NAME varchar(100) not null
);
|
执行migrate
1
2
3
4
5
6
7
8
|
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" migrate
Flyway Community Edition 7.0.2 by Redgate
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 is 10.4.
Successfully validated 1 migration (execution time 00:00.027s)
Current version of schema `flywaytest`: << Empty Schema >>
Migrating schema `flywaytest` to version "1 - Create person table"
Successfully applied 1 migration to schema `flywaytest` (execution time 00:00.266s)
|
这个时候注意不能修改原来的sql名字或者内容,否则会报错
1
2
3
4
5
6
7
8
|
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" migrate
Flyway Community Edition 7.0.2 by Redgate
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 is 10.4.
ERROR: Validate failed:
Migration checksum mismatch for migration version 1
-> Applied to database : 1715188512
-> Resolved locally : -1907442265. Either revert the changes to the migration, or run repair to update the schema history.
|
这个时候再放一个表,
1
2
3
4
5
|
➜ flyway cat sql/V1.0.0.1__Create_man_table.sql
create table man (
ID int not null,
NAME varchar(100) not null
);
|
再次migrate
1
2
3
4
5
6
7
8
|
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" migrate
Flyway Community Edition 7.0.2 by Redgate
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 is 10.4.
Successfully validated 2 migrations (execution time 00:00.015s)
Current version of schema `flywaytest`: 1
Migrating schema `flywaytest` to version "1.0.0.1 - Create man table"
Successfully applied 1 migration to schema `flywaytest` (execution time 00:00.293s)
|
然后你可以info看一下
1
2
3
4
5
6
7
8
9
10
11
12
|
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" info
Flyway Community Edition 7.0.2 by Redgate
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 is 10.4.
Schema version: 1.0.0.1
+-----------+---------+---------------------+------+---------------------+---------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+---------------------+------+---------------------+---------+
| Versioned | 1 | Create person table | SQL | 2024-01-24 10:51:51 | Success |
| Versioned | 1.0.0.1 | Create man table | SQL | 2024-01-24 10:56:29 | Success |
+-----------+---------+---------------------+------+---------------------+---------+
|
如果这个时候你放一个低版本的sql,就会报错
1
2
3
4
5
6
7
8
9
|
➜ sql ls
V0.0.0.1__Create_hello_table.sql V1.0.0.1__Create_man_table.sql V1__Create_person_table.sql
➜ sql cd ..
➜ flyway flyway -user=root -password=12345 -url="jdbc:mysql://172.17.0.3:3306/flywaytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT" migrate
Flyway Community Edition 7.0.2 by Redgate
Database: jdbc:mysql://172.17.0.3:3306/flywaytest (MariaDB 11.2)
WARNING: Flyway upgrade recommended: org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 11.2 is newer than this version of Flyway and support has not been tested. The latest supported version of org.flywaydb.core.internal.database.mysql.MariaDBDatabaseType@48aaecc3 is 10.4.
ERROR: Validate failed:
Detected resolved migration not applied to database: 0.0.0.1. To ignore this migration, set ignoreIgnoredMigrations to true. To allow executing this migration, set outOfOrder to true.
|