{"id":3772,"date":"2025-04-16T03:40:42","date_gmt":"2025-04-16T01:40:42","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3772"},"modified":"2025-04-16T03:43:09","modified_gmt":"2025-04-16T01:43:09","slug":"what-is-flyway","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2025\/04\/16\/what-is-flyway\/","title":{"rendered":"What is Flyway?"},"content":{"rendered":"\n<p>Flyway is an open-source database migration tool that helps manage and automate the process of applying changes to a database schema. It provides a structured way to track, version, and execute database migrations, ensuring that any changes applied to the database are consistent across different environments.<\/p>\n\n\n\n<p>Flyway is not specific to Spring Boot. It can be integrated into various Java applications to manage and automate database changes.However, Spring Boot offers easy integration with Flyway by automatically executing migration scripts at application startup when Flyway is included in the project configuration. This makes it particularly convenient for developers using Spring Boot.<\/p>\n\n\n\n<h4>Use of Flyway<\/h4>\n\n\n\n<p>Flyway is primarily used for:<\/p>\n\n\n\n<ul><li><strong>Version Control<\/strong>: Keeping track of database schema changes over time.<\/li><li><strong>Automation<\/strong>: Automatically applying database changes as part of your deployment process.<\/li><li><strong>Consistency<\/strong>: Ensuring that all environments (development, testing, production) have the same schema.<\/li><\/ul>\n\n\n\n<h4>When to Use Flyway<\/h4>\n\n\n\n<p>We should use Flyway when:<\/p>\n\n\n\n<ul><li>We need to manage complex database schemas that are frequently updated.<\/li><li>Our team collaborates on a project, and we need to ensure everyone is using the same database version.<\/li><li>We want to automate the deployment of database changes as part of a continuous integration\/continuous deployment (CI\/CD) pipeline.<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>How to Use Flyway in a Spring Boot Project<\/strong><\/span><\/h4>\n\n\n\n<h4>Maven Configuration<\/h4>\n\n\n\n<p>To integrate Flyway into a Spring Boot project using Maven, we need to add the Flyway Maven plugin to our <code>pom.xml<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;project&gt;\n    ...\n    &lt;build&gt;\n        &lt;plugins&gt;\n            &lt;plugin&gt;\n                &lt;groupId&gt;org.flywaydb&lt;\/groupId&gt;\n                &lt;artifactId&gt;flyway-maven-plugin&lt;\/artifactId&gt;\n                &lt;version&gt;11.2.0&lt;\/version&gt;\n            &lt;\/plugin&gt;\n        &lt;\/plugins&gt;\n    &lt;\/build&gt;\n&lt;\/project&gt;<\/code><\/pre>\n\n\n\n<ul><li><strong>groupId<\/strong>&nbsp;and&nbsp;<strong>artifactId<\/strong>: Identify the Flyway Maven plugin.<\/li><li><strong>version<\/strong>: Specifies the version of Flyway to use.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Spring Boot Configuration<\/h4>\n\n\n\n<p>Spring Boot automatically integrates with Flyway if it&#8217;s on the classpath.  For doing that, we could\/should configure it in the <code>application.properties<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/mydb\nspring.datasource.username=myuser\nspring.datasource.password=mypassword\nspring.flyway.enabled=true<\/code><\/pre>\n\n\n\n<ul><li><strong>spring.datasource.url<\/strong>: Specifies the database connection URL.<\/li><li><strong>spring.datasource.username<\/strong>&nbsp;and&nbsp;<strong>password<\/strong>: Database credentials.<\/li><li><strong>spring.flyway.enabled<\/strong>: Enables Flyway for the application.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Creating Migration Scripts<\/h4>\n\n\n\n<p>Migration scripts should be placed in the <code><strong><em>src\/main\/resources\/db\/migration<\/em><\/strong><\/code> directory. Each script should be named according to the Flyway naming convention, such as <code>V1__Initial_setup.sql<\/code>.<\/p>\n\n\n\n<p>Example of a migration script (<code>V1__Initial_setup.sql<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE example_table (\n    id INT PRIMARY KEY,\n    name VARCHAR(100)\n);<\/code><\/pre>\n\n\n\n<ul><li><strong>V1__Initial_setup.sql<\/strong>: The file name indicates it is the first version of the migration.<\/li><li><strong>CREATE TABLE<\/strong>: SQL statement to create a new table.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Running Migrations<\/h4>\n\n\n\n<p>To apply migrations, let us run the Flyway Maven plugin:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn flyway:migrate<\/code><\/pre>\n\n\n\n<ul><li><strong>mvn flyway:migrate<\/strong>: Executes all pending migrations against the database.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Example Project Structure<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"554\" height=\"244\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/04\/image.png\" alt=\"\" class=\"wp-image-3775\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/04\/image.png 554w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/04\/image-300x132.png 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Real Case Example with Flyway<\/strong><\/span><\/h4>\n\n\n\n<p>Let&#8217;s consider a scenario where we need to manage versioning for a database schema in a Spring Boot application using Flyway. We will also explore how to perform a rollback.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>Adding Versioned Migrations<\/strong><\/span><\/h4>\n\n\n\n<p>Let us create a directory <code>src\/main\/resources\/db\/migration<\/code> and add migration scripts. Each script should be prefixed with a version number.<\/p>\n\n\n\n<h5>Version 1: Initial Setup<\/h5>\n\n\n\n<p>Create <code>V1__Initial_setup.sql<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employee (\n    id INT PRIMARY KEY,\n    name VARCHAR(100),\n    department VARCHAR(50)\n);<\/code><\/pre>\n\n\n\n<h5>Version 2: Add Column<\/h5>\n\n\n\n<p>Create <code>V2__Add_email_to_employee.sql<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employee ADD email VARCHAR(100);<\/code><\/pre>\n\n\n\n<h5>Applying Migration<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn flyway:migrate<\/code><\/pre>\n\n\n\n<p>This command will apply all pending migrations to the database.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>Rollback Strategy<\/strong><\/span><\/h4>\n\n\n\n<p>Flyway does not support automatic rollbacks. However, we can manually create a rollback script.<\/p>\n\n\n\n<h5>Rollback Version 2<\/h5>\n\n\n\n<p>Create <code>U2__Remove_email_from_employee.sql<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employee DROP COLUMN email;<\/code><\/pre>\n\n\n\n<p>this script removes the&nbsp;<code>email<\/code>&nbsp;column from the&nbsp;<code>employee<\/code>&nbsp;table. This action effectively reverses the change made in the migration that added the&nbsp;<code>email<\/code>&nbsp;column.<\/p>\n\n\n\n<p>To rollback, we could manually run the rollback script using the database client or create a custom Maven command.<\/p>\n\n\n\n<h5>Example Rollback Execution<\/h5>\n\n\n\n<p>To simulate a rollback, we can manually execute the rollback script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn flyway:migrate -Dflyway.sqlMigrationPrefix=U<\/code><\/pre>\n\n\n\n<p>This command will apply rollback scripts prefixed with <code>U<\/code>. <\/p>\n\n\n\n<p>The prefix<strong> <code>U<\/code><\/strong> in the Flyway migration command is used to specify rollback scripts. In Flyway, migration scripts typically have a prefix like <code>V<\/code> for versioned migrations. By using the prefix <code>U<\/code>, we indicate that these scripts are meant to undo or rollback changes made by corresponding <code><strong>V<\/strong><\/code> scripts.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>PS:<\/strong> It is not obligatory to use the prefix <code>U<\/code> for undo scripts in Flyway. The prefix is configurable, and we can choose any prefix that suits our naming convention. The default prefix for versioned migrations is <code>V<\/code>, but for undo scripts, we can define a different prefix (like <code>U<\/code>) using the <code>flyway.sqlMigrationPrefix<\/code> property when executing the command. This allows flexibility in organizing and identifying our scripts.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5>Conclusion<\/h5>\n\n\n\n<p>Flyway is a powerful tool for managing database migrations in Spring Boot applications. By integrating Flyway, we can ensure that our database schema is versioned, consistent, and easily manageable across different environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flyway is an open-source database migration tool that helps manage and automate the process of applying changes to a database schema. It provides a structured way to track, version, and execute database migrations, ensuring that any changes applied to the database are consistent across different environments. Flyway is not specific to Spring Boot. It can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3773,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1006,1007],"tags":[1003,1000,999,1005,1002,1004,1001],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3772"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3772"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3772\/revisions"}],"predecessor-version":[{"id":3777,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3772\/revisions\/3777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/3773"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}