<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Trust Birungi</title>
    <description>The latest articles on DEV Community by Trust Birungi (@trustbirungi).</description>
    <link>https://dev.to/trustbirungi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F459153%2Fd6fc33c5-43ad-49a5-9dfa-fddb845d557b.png</url>
      <title>DEV Community: Trust Birungi</title>
      <link>https://dev.to/trustbirungi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trustbirungi"/>
    <language>en</language>
    <item>
      <title>Getting Started With Flyway In Spring Boot</title>
      <dc:creator>Trust Birungi</dc:creator>
      <pubDate>Thu, 27 Aug 2020 11:59:40 +0000</pubDate>
      <link>https://dev.to/trustbirungi/getting-started-with-flyway-in-spring-boot-po1</link>
      <guid>https://dev.to/trustbirungi/getting-started-with-flyway-in-spring-boot-po1</guid>
      <description>&lt;p&gt;Flyway is an open-source tool for version controlling databases. It’s like the Git for the databases.&lt;br&gt;&lt;br&gt;
Flyway enables you to write SQL scripts for your schemas and tables and version them to ensure consistency and offers the ability to roll back as needed.  &lt;/p&gt;

&lt;p&gt;This article assumes that you have basic familiarity with Spring Boot. If not, refer to this official guide from the creators of Spring Boot:  &lt;a href="https://spring.io/guides/gs/spring-boot/"&gt;https://spring.io/guides/gs/spring-boot/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuring Flyway&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If setting up a new Spring Boot project, you can add the Flyway dependency through the Spring Initializr at  &lt;a href="https://start.spring.io/"&gt;https://start.spring.io&lt;/a&gt;  and adding Flyway as a dependency as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dhvdBZCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1587/1%2ANv6r49fxZnAV1y_ujn1d2Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dhvdBZCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1587/1%2ANv6r49fxZnAV1y_ujn1d2Q.png" alt="Image for post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re working with an existing Spring Boot project, you can just add it manually to your pom.xml file as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Add the Flyway Core dependency:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;  
    &amp;lt;groupId&amp;gt;org.flywaydb&amp;lt;/groupId&amp;gt;  
    &amp;lt;artifactId&amp;gt;flyway-maven-plugin&amp;lt;/artifactId&amp;gt;  
    &amp;lt;version&amp;gt;6.5.5&amp;lt;/version&amp;gt;  
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Add the Flyway maven plugin (this will enable you to run Flyway using Maven):&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;plugin&amp;gt; 
    &amp;lt;groupId&amp;gt;org.flywaydb&amp;lt;/groupId&amp;gt; 
    &amp;lt;artifactId&amp;gt;flyway-maven-plugin&amp;lt;/artifactId&amp;gt; 
    &amp;lt;version&amp;gt;6.5.5&amp;lt;/version&amp;gt; 
    &amp;lt;configuration&amp;gt; 
        &amp;lt;url&amp;gt;your_db_url&amp;lt;/url&amp;gt; 
        &amp;lt;user&amp;gt;your_db_username&amp;lt;/user&amp;gt; 
        &amp;lt;password&amp;gt;your_db_password&amp;lt;/password
        &amp;lt;locations&amp;gt;classpath:db/migration&amp;lt;/locations&amp;gt; 
    &amp;lt;/configuration&amp;gt;
&amp;lt;/plugin&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Running the Migrations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you’ve configured your pom.xml, you now need to create your first migration file. By default, Flyway will check for migration files inside  &lt;code&gt;src/main/resources/db/migration&lt;/code&gt;. If you used the Spring Initializr to add Flyway, this directory has already been created for you. If not, create that directory and add your first migration file.  &lt;/p&gt;

&lt;p&gt;The migration file should be named as such:  &lt;code&gt;V1__Create_first_table.sql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After creating your migration file, you can now run the following commands from the root directory of your project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;mvn clean install&lt;/code&gt; to build your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;mvn flyway:migrate&lt;/code&gt;  to run the migration(s).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Common Errors&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project flyway-demo: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password! -&amp;gt; [Help 1]&lt;/code&gt;&lt;br&gt;
To solve this error, double-check your pom.xml under the Flyway plugin configuration to ensure that the URL, username, and password are correctly specified.&lt;br&gt;&lt;br&gt;
Also, ensure that the database you’re trying to access exists and that you have sufficient privileges to access it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;FlywayException: Detected failed migration to version 1.0&lt;/code&gt;&lt;br&gt;&lt;br&gt;
This is usually caused by an SQL error in a migration file. To solve this error, first fix the SQL error in your migration and then run the following command to repair the migration:  &lt;code&gt;mvn flyway:repair&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Once the migration is repaired, you can run  &lt;code&gt;mvn flyway:migrate&lt;/code&gt;  to rerun the migration.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>springboot</category>
      <category>flyway</category>
      <category>java</category>
      <category>database</category>
    </item>
  </channel>
</rss>
