How Flyway worksjava
The easiest scenario is when you point Flyway to an empty database.数据库
最容易的方案是Flyway指向一个空的数据库。app
It will try to locate its metadata table. As the database is empty. Flyway won't find it and will create it instead.spa
FlyWay将试图查找它的元数据表。由于数据库是空的。Flyway找不到它的元数据表,而后就会去建立它的元数据表。3d
You now have a database with a single empty table called SCHEME_VERSION by default:blog
如今呢,咱们就有一个数据库,这个数据库包含一个缺省的空表SCHEME_VERSION :排序
This table will be used to track the state of the databse.rem
这张表将被用于监测数据库的状态。get
Immediately afterwards Flyway will begin scanning the filesystem or the classpath of the application for migrations.They can be written in either Sql or Java.it
紧接着Flyway将开始扫描文件系统或者应用的类路径进行迁移。这些迁移文件能够是SQL脚本或者是java程序。
The migrations are then sorted besed on their version number and applied in order.
这些迁移文件按照版本号进行排序而且按照这个排序去执行。
As each migration gets applied,the metadata table is updated accordingly:
当每个迁移文件被执行后,元数据的表就会按照格式进行更新。
schema_version
With the metabata and the initial state in palce,we can now talk about migrating to newer versions.
这样咱们就能够根据元数据和数据库初始化的状态,进行数据库升级了。
Flyway will once agein scan the filesystem or the classpath of the application of the migrations. The migrations are checked against the metadata table. If their version number is lower or equal to the one of the version maked as current, they are ignored.
当Flyway 再次扫描迁移的时候,它就会检查元数据表中迁移版本,若是要执行的迁移脚本的版本小于或者等于当前版本,Flyway将会忽略,再也不重复执行。
The remaining migrations are the pending migrations : available , but not applied.
剩下的迁移脚本将会被执行迁移:可获取的而且没有被执行过的脚本。
They are then sorted by version number and executed in order:
这些迁移文件根据版本号进行排序而且有序执行。
The metadata table is 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 migration with a version number higher than the current one. The next time Flyway starts, it will find it and upgrade the database accordingly.
这样的话,每次要作数据库升级,不管是执行DDL语句仍是执行DML语句,全部的解决的问题的就是建立一个高于如今版本的迁移文件。当下一次Flyway开始运行的时候,它将会自动发现升级脚本,执行而且更新元数据表。