The experience of using the database version management tool flyway

I tested the function of flyway today. I encountered several problems during use, which took me all morning. I couldn't find a corresponding solution on the Internet. I groped for a whole morning and finally got it.

My project is a web project built with maven and depends on the code:

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>3.2.1</version>
</dependency>

 Add the following configuration in spring:

    <bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">  
	    <property name="dataSource" ref="dataSource"/>
	</bean>

 Please configure the dataSource of the database according to the actual situation.

 

Create the sql file V1.0.0__20151125.sql file under the project's resources/db/migration, and it will be normal after restarting.

 

At this time, if you create a new sql file, such as V1.0.1__20151126.sql, and deliberately write the sql statement incorrectly, such as the wrong table name, when you restart it, you will find an error message at startup, indicating that the table name is wrong.  If the sql is modified correctly and restarted again, the following errors will be found:

 

com.googlecode.flyway.core.migration.MigrationException: Migration to version 1.0.1 failed! Please restore backups and roll back database and code!
at com.googlecode.flyway.core.migration.DbMigrator.migrate(DbMigrator.java:158)

When this problem occurred, I was confused at the time, and I didn’t know how to solve it. No matter how I restarted, it was this error. There was no final solution on the Internet. Finally, I looked at the source code and found that there was a repair method, so I configured the flyway in applicationContext.xml. Modify, change migrate to repair, start again, start successfully, refresh the schema_version table again, and find that the record in error has been deleted; then restore the repair in the configuration file to migrate and start it, this time the startup is successful, and V1 The statement in .0.1 has been executed.

Go back to the source code to see the comments of Repair, which means roughly:

Repairs the Flyway metadata table. This will perform the following actions:
Remove any failed migrations on databases without DDL transactions (User objects left behind must still be cleaned up manually)
Correct wrong checksums

 Modify the metadata table of flyway, that is, modify the data in the schema_version table. This operation will do the following:

1. Delete all failed non-transactional database ddl operations; (user objects such as tables, views, etc. need to be manually cleared)

2. Correct the wrong sql file check code (when the file is modified, the generated check code will change).

 

If you encounter the same error when deploying in the production environment later, you can consider using the command line to call repair to repair the version data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326233553&siteId=291194637