How Flyway Works
The easiest scenario is when you point flyway to an empty database.
The easiest solution is to Flyway point to an empty database.
It would try to locate its metadata table. As the database is empty. Flyway won ' t find it and would create it instead.
Flyway will attempt to find its metadata table. Because the database is empty. Flyway cannot find its metadata table, and then it will create its metadata table.
You are now having a database with a single empty table called Scheme_version by default:
Now, we have a database that contains a default empty table Scheme_version:
This table is used to track the state of the databse.
This table will be used to monitor the status of the database.
Immediately afterwards Flyway would begin scanning the filesystem or the classpath of the application for migrations. They can is written in either SQL or Java.
Shortly thereafter, Flyway will begin scanning the file system or the application's classpath for migration. These migration files can be SQL scripts or Java programs.
The migrations is then sorted besed on their version number and applied in order.
These migration files are sorted by version number and executed according to this sort.
As each migration gets applied,the metadata table is updated accordingly:
When each migration file is executed, the metadata table is updated in the format.
Schema_version
With the Metabata and the initial state in Palce,we can now talk about migrating to newer versions.
This allows us to upgrade the database based on the state of the metadata and the initialization of the database.
Flyway would once Agein scan the filesystem or the classpath of the application. The migrations is checked against the metadata table. If their version number is lower or equal to the one of the version maked as current, they is ignored.
When Flyway scans the migration again, it checks the metadata table for the migrated version, and if the version of the migration script to be executed is less than or equal to the current version, Flyway will be ignored and no longer repeated.
The remaining migrations is the pending migrations:available, but not applied.
The remaining migration scripts will be migrated: a script that can be obtained and not executed.
They is then sorted by version number and executed in order:
These migration files are sorted by version number and executed sequentially.
The metadata table is updated accordingly:
The metadata table will also be updated accordingly:
And that ' s it! Every time the need to evolve the database arises, whether structure (DDL) or reference data (DML), simply create a new mi Gration with a version number higher than the current one. The next time Flyway starts, it'll find it and upgrade the database accordingly.
In this case, every time a database upgrade is performed, whether it is executing a DDL statement or executing a DML statement, all of the issues that are resolved are to create a migration file that is higher than the current version. The next time Flyway starts running, it will automatically discover the upgrade script, execute and update the metadata table.
Database versioning Tools Flyway (4.0.3)---working mechanism (translated)