3

Basic Knowledge For Using Flyway

 3 years ago
source link: https://blog.knoldus.com/basic-knowledge-for-using-flyway/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Reading Time: 2 minutes

 In this blog I’m going to talk about integrating Flyway in your Spring Boot application. It is a migrating library that automates and keeps track of database changes, it’s basically version control for your database.

Flyway[1]

How Flyway works

In order to use Flyway you need to add the dependency to your pom.xml or build.gradle file. For maven use the following dependency.

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

For Gradle use the following dependency.

compile group: 'org.flywaydb', name: 'flyway-core', version: '7.0.1'

You then need to create a directory called db and another directory called migration which is the default location for migration script files. Create sql files with names following the format of a capital V followed by the version number followed by two underscores followed by the file name.

Flyway db migration folder - Stack Overflow[2]

Finally you need to add the following lines to your application.properties file. Make sure not to modify you sql files. If you need to modify them add a new version or if you really want to modify the files drop the database and recreate it.

spring.jpa.hibernate.ddl-auto = validate
spring.datasource.validationQuery= SELECT 1
spring.flyway.baseline-on-migrate=true
spring.flyway.locations=classpath:db/migration



[3]

Conclusion

By leveraging a database migration library such as Flyway, you can effectively streamline the software upgrade process and get rid of manual database upgrade steps from the implementation.

You also won’t need to precede each creates a table statement with a drop table statement to ensure that the database structure is recreated each time you start your application as this is not optimal.

References

[1] https://www.flywaydb.org/

[2] https://www.stackoverflow.com/questions/22819493/flyway-db-migration-folder

[3] https://www.dzone.com/articles/flyway-21-released-automatic

Happy blogging!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK