As you can see, we will stop using ddl-auto of JPA by set none value, because we want to control our table structures by Flyway with versions, not from the Entities relationships.
In flyway configuration, we will need to reuse configurations from the datasource such as url, username, password and driverClassName. By default, the flyway.enabled will be true.
In the locations field, you will put your path from your resources to the directory that contains migration files.
For the baselineOnMigrate we will have another topic about it.
We are using Versioned Migration so we will set the name of migration file as below.
1
V1.0_20220426223800__Initial_Table.sql
- View more about naming in Flyway SQL-based Migrations.
- In which:
- V is the prefix which used for Versioned Migration.
- 1.0_20220426223800 is the version with timestamp.
- __ is the separator.
- Initial_Table is the description.
- .sql is the suffix.
Then, in the migration file, we should put the the SQL scripts for creating table as below:
Now, let's create another migration version like 1.1 and because in the software development we want to extend the current table with two more column createdDate and updatedDate into our table.
So we will set the new migration file with name as below.
1
V1.1_20220430223800__Add_New_2_Columns.sql
In which:
V is the prefix which used for Versioned Migration.
1.1_20220430223800 is the new version with timestamp.
__ is the separator.
Add_New_2_Columns is the description.
.sql is the suffix.
Then let's add the SQL scripts below to add more two column into the table.
Now, restart your spring boot service again, then check the customers and flyway_schema_history.
mysql> describe customers;
Field
Type
Null
Key
Default
Extra
id
varchar(255)
NO
PRI
NULL
address
varchar(255)
YES
NULL
dob
datetime(6)
YES
NULL
email
varchar(255)
YES
NULL
fullName
varchar(255)
YES
NULL
phone
varchar(255)
YES
NULL
created_date
timestamp
YES
NULL
updated_date
timestamp
YES
NULL
full_name
varchar(255)
YES
NULL
mysql> select * from flyway_schema_history;
installed_rank
version
description
type
script
checksum
installed_by
installed_on
execution_time
success
1
1.0.20220426223800
Initial Table
SQL
V1.0/V1.0_20220426223800__Initial_Table.sql
1800508254
root
2022-04-30 14:45:16
56
1
2
1.1.20220430223800
Add New 2 Columns
SQL
V1.1/V1.1_20220430223800__Add_New_2_Columns.sql
222139554
root
2022-04-30 14:45:16
68
1
As you can see, new two columns created_date and updated_date have been added into the customers table. Then you also see the new record in flyway_schema_history has been added: