<?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: leopardorossi</title>
    <description>The latest articles on DEV Community by leopardorossi (@leopardorossi).</description>
    <link>https://dev.to/leopardorossi</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%2F632426%2Fbef5abf8-bc15-4f7e-b7fb-45637571f15a.jpeg</url>
      <title>DEV Community: leopardorossi</title>
      <link>https://dev.to/leopardorossi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leopardorossi"/>
    <language>en</language>
    <item>
      <title>SQL Server and Flyway: a Docker approach</title>
      <dc:creator>leopardorossi</dc:creator>
      <pubDate>Tue, 14 Mar 2023 17:59:12 +0000</pubDate>
      <link>https://dev.to/leopardorossi/sql-server-and-flyway-a-docker-approach-kdj</link>
      <guid>https://dev.to/leopardorossi/sql-server-and-flyway-a-docker-approach-kdj</guid>
      <description>&lt;p&gt;For a personal project I wanted to realise a development environment with the following characteristics: easy to setup, easy to maintain and easy to share with my teammates. With this problem to solve, three tools immediately came to my mind: Docker, SQL Server and Flyway.&lt;/p&gt;

&lt;p&gt;In this article I will describe how I glued them together in order to create a flexible development environment. &lt;/p&gt;

&lt;p&gt;Let's dive into it!&lt;/p&gt;

&lt;h2&gt;
  
  
  TLTR
&lt;/h2&gt;

&lt;p&gt;If you like to setup SQL Server and Flyway with Docker:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clone &lt;a href="https://github.com/leopardorossi/sqlserver-and-flyway"&gt;this repository&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;Create an &lt;code&gt;.env&lt;/code&gt; file at root level. In it define the environmental variables that will be used in the &lt;code&gt;docker-compose.yaml&lt;/code&gt; file. &lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The keys you use &lt;strong&gt;must&lt;/strong&gt; match the ones used in the &lt;code&gt;docker-compose.yaml&lt;/code&gt; file&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Modify Flyway configuration file in &lt;code&gt;flyway/conf&lt;/code&gt; folder with your parameters. &lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The database host name &lt;strong&gt;must&lt;/strong&gt; match the SQL Server service name in the &lt;code&gt;docker-compose.yaml&lt;/code&gt; file&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Put your migration files in &lt;code&gt;flyway/sql&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;Open a terminal window and execute &lt;code&gt;docker compose up -d&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Project structure
&lt;/h2&gt;

&lt;p&gt;The structure of the project is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project-folder/
├─ db/
│  ├─ scripts/
│  │  ├─ entrypoint.sh
│  │  ├─ db-init.sh
│  │  ├─ init.sql
├─ flyway/
│  ├─ conf/
│  │  ├─ flyway.conf
│  ├─ sql/
│  │  ├─ &amp;lt;migrations goes here&amp;gt;
├─ docker-compose.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;db/scripts&lt;/code&gt; folder contains a set of shell and SQL scripts used in SQL Server container initialisation phase.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flyway&lt;/code&gt; folder contains the information Flyway needs: configuration and migration files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker-compose.yaml&lt;/code&gt; is the Docker compose file where SQL Server and Flyway containers are defined and linked together.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The compose file
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;docker-compose.yaml&lt;/code&gt; file is where the development environment is formally described and it looks like the following code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mcr.microsoft.com/mssql/server:2022-latest&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sqlserver-2022-database&lt;/span&gt; 
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/bin/bash /scripts/entrypoint.sh&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;dbdata:/var/opt/mssql&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./db/scripts:/scripts&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ACCEPT_EULA=Y&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;MSSQL_SA_PASSWORD=${DB_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;1433:1433&lt;/span&gt;
  &lt;span class="na"&gt;flyway&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redgate/flyway&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;flyway&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;migrate -user=${DB_USER} -password=${DB_PASSWORD} -connectRetries=60&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./flyway/conf/:/flyway/conf&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./flyway/sql/:/flyway/sql&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;dbdata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two services defined in the file, one for the SQL Server container (&lt;code&gt;db&lt;/code&gt;) and the other for Flyway container (&lt;code&gt;flyway&lt;/code&gt;). They both use volumes to persist data and to copy files from the host machine into the containers themselves. In particular, SQL Server service copies &lt;code&gt;scripts&lt;/code&gt; folder; while the Flyway one binds the &lt;code&gt;conf&lt;/code&gt; and &lt;code&gt;sql&lt;/code&gt; directories. &lt;/p&gt;

&lt;p&gt;Until here, nothing special.&lt;/p&gt;

&lt;p&gt;Before launching our services it must be considered that, in order to apply migrations, Flyway looks for an &lt;strong&gt;existing database&lt;/strong&gt;. Unluckily, this condition is not satisfied the first time our containers are executed. Therefore, to fully automate the development environment, an initialisation procedure is needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  The initialisation procedure
&lt;/h3&gt;

&lt;p&gt;The desired order of operations is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Launch SQL Server container;&lt;/li&gt;
&lt;li&gt;Create the target database and schema (if they not exist);&lt;/li&gt;
&lt;li&gt;Launch Flyway container and apply migrations to the database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything begins with the &lt;code&gt;entrypoint.sh&lt;/code&gt; file, which is executed when the &lt;code&gt;db&lt;/code&gt; service starts (see the &lt;code&gt;command&lt;/code&gt; tag defined for it in the compose file). &lt;/p&gt;

&lt;p&gt;It is defined below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x /scripts/db-init.sh &amp;amp; &lt;span class="se"&gt;\&lt;/span&gt;
/scripts/db-init.sh &amp;amp; &lt;span class="se"&gt;\&lt;/span&gt;
/opt/mssql/bin/sqlservr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here three things are happening. First of all, execution permission is granted to &lt;code&gt;db-init.sh&lt;/code&gt; file (we will dive into it in a second). Then, &lt;code&gt;db-init.sh&lt;/code&gt; is executed and eventually SQL Server is started. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is important to notice that &lt;code&gt;db-init.sh&lt;/code&gt; and SQL Server are executed in parallel.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, it is time to initialise our database! And this is exactly what &lt;code&gt;db-init.sh&lt;/code&gt; does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sleep 30s

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${MSSQL_SA_PASSWORD} -d master -i /scripts/init.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script waits a fixed amount of time (30 seconds in this case) to be sure that SQL Server is up and running. Then, it runs the SQL script defined in &lt;code&gt;init.sql&lt;/code&gt; file. The script is quite simple, indeed it checks if the database exists and creates it if it doesn't:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="n"&gt;DB_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'&amp;lt;YOUR_DB_NAME&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;YOUR_DB_NAME&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;GO&lt;/span&gt;
  &lt;span class="n"&gt;USE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;YOUR_DB_NAME&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;GO&lt;/span&gt;
  &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;YOUR_SCHEMA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;GO&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is the initialisation procedure. Thanks to it when SQL Server container is launched for the first time everything is automatically setup.&lt;/p&gt;

&lt;p&gt;At this point, our containers can finally be executed with the &lt;code&gt;docker compose up&lt;/code&gt; command. Once they are up, if you look into the Flyway container's logs you can notice that at the beginning it fails to connect to the database: this is because SQL Server container is still starting. Despite this, after a while, the migration scripts are successfully executed: the initialisation procedure did its job. You can verify yourself by connecting to your database!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Putting SQL Server and Flyway together in a Docker friendly way is nothing special. At the same time, it is a delicate operation which requires SQL Server container to be ready for Flyway execution. With a bunch of scripts and a bit of synchronisation among containers, your development environment is ready for your awesome projects.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>database</category>
    </item>
  </channel>
</rss>
