CORE
HOME > JAVA > J2SE > CORE
2016.11.03 / 10:11

flyway - Java ¿ë Database migration framework

±â¸®¾Æºü
Ãßõ ¼ö 544

¼Ò°³

°³¹ßÀ̳ª ¿î¿µÀ» Çϸ鼭 º¯°æ ³»¿ªÀ» ¹Ý¿µÇÒ ¶§ °¡Àå ¸¹ÀÌ ½Ç¼öÇÏ´Â ºÎºÐÀº Local À̳ª ƯÁ¤ ȯ°æ¿¡¼­¸¸ µÇ°Ô °³¹ß/ÆÐŰ¡ÇÏ¿© ½ÇÁ¦ ¿î¿µÈ¯°æ¿¡¼­´Â ÀǵµÇÑ´ë·Î µ¿ÀÛÇÏÁö ¾Ê´Â °ÍÀ̾ú´Ù.

Build/packaging/deploy ½Ã ¿î¿µ¿¡¼­ ¹ú¾îÁö´Â ½Ç¼öµéÀº Çü»ó°ü¸® ¹× CI ¼­¹ö, Å×½ºÆ® ÀÚµ¿È­, ¹èÆ÷ ÀÚµ¿È­µîÀÇ °³¹ß ÇÁ·Î¼¼½º¸¦ Àû¿ëÇÏ¿© °ÅÀÇ ´Ù ÇØ°áÀÌ µÇ¾úÁö¸¸

¿©ÀüÈ÷ ÀÚÁÖ ¹ß»ýÇÏ´Â ½Ç¼ö´Â °³¹ßÀ̳ª Å×½ºÆ® ½Ã½ºÅÛÀÇ DB  ½ºÅ°¸¶³ª µ¥ÀÌŸ¸¸ ¹Ù²Ù°í ½ÇÁ¦ ¿î¿µ¿¡¼­´Â ÇØ´ç ½ºÅ°¸¶³ª µ¥ÀÌŸ Àû¿ë ÀýÂ÷¸¦ »©¸Ô´Â °ÍÀÌ´Ù.

 

À§¿Í °°Àº »óȲÀÌ ÀϾ´Â ÀÌÀ¯´Â Source ÄÚµå ¹× db schema °¡ Á¤¸®µÈ ERD ³ª SQL ÆÄÀÏÀº Çü»ó °ü¸®°¡ °¡´ÉÇÏÁö¸¸ ÀÌ ÆÄÀÏÀÌ DBMS ¿¡ ¹Ý¿µµÆ´ÂÁö ÀÚü´Â µû·Î °ü¸®°¡ µÇÁö ¾Ê±â ¶§¹®ÀÌ´Ù.

¹°·Ð ERD °°Àº ¼Ö·ç¼Ç¿¡¼­ Reverse Engineering À» Çؼ­ Å×½ºÆ® DB ¿Í ¿î¿µ DB ÀÇ ½ºÅ°¸¶¸¦ ºñ±³ÇѴٰųª ÇÏ´Â ³ë°¡´Ù¸¦ ÇÏ¸é °¡´ÉÇÏÁö¸¸ ¿î¿µ DB ¿¡ ´ëÇؼ­ Àú·± ÀÛ¾÷À» ÇÏ´Â°Ç ¹Î°¨ÇÏ°í ºÎ´ãÀÌ µÇ´Â ÀÛ¾÷ÀÌ´Ù.

                   

http://flywaydb.org/getstarted/whyDatabaseMigrations.html ¿¡¼­ ¹ßÃé

 

ruby on rails ³ª laravel frameworkÀ» º¸°í °¡Àå ³î¶ú´ø°Í Áß¿¡ Çϳª´Â db:migration ±â´ÉÀ̾ú´Ù.

schema ÆÄÀÏÀ» sqlplus ³ª mysql client °°Àº °÷¿¡¼­ ½ÇÇàÇؼ­ ¹Ý¿µÇÏÁö ¾Ê°í DBMS schema versionÀ» °ü¸®ÇÏ´Â Å×À̺íÀ» ¸¸µé°í db:migrate version ¸í·É¾î¸¦ ½ÇÇàÇÏ¸é ¹öÀü¿¡ ¸Â´Â ½ºÅ°¸¶ ¹× SQL ¹®À» Àû¿ëÇÏ´Â ¹æ½ÄÀ¸·Î DBMS ¸¦ °ü¸®ÇÏ´Â db:migration ±â´ÉÀº ¼­ºñ½º °³¹ß/À¯Áöº¸¼öÇÒ¶§ ²À ÇÊ¿äÇÑ ±â´ÉÀ̶ó´Â »ý°¢ÀÌ µé¾ú´Ù.

 

flyway ´Â Java ¿¡¼­ Ruby on Rails ³ª Laravel ó·³ DB migration À» ÇÒ¼ö ÀÖ´Â ÇÁ·¹ÀÓ¿÷ÀÌ´Ù.

 

µ¿ÀÛ ¹æ½Ä

http://flywaydb.org/getstarted/howFlywayWorks.html

»ç¿ëÇÏ´Â DBMS ¿¡ SCHEMA_VERSION ¶ó´Â Å×À̺íÀ» ¸¸µé°í ¿©±â¿¡ DBMS ÀÇ ¹öÀü ¹× º¯°æ ½ºÅ©¸³Æ®, º¯°æÀÏ, º¯°æ°á°ú µîÀ» ±â·ÏÇÑ´Ù.


migration sql file 

 http://flywaydb.org/documentation/migration/sql.html

file name

  • ÆÄÀϸíÀº ´ë¹®ÀÚ V¿Í ¼ýÀÚ·Î ½ÃÀÛÇØ¾ß flyway °¡ schema ÆÄÀÏ·Î ÀνÄÇÔ
  • V µÚ¿¡ ¼ýÀÚ(Dot µµ °¡´É - 1.1.3)·Î ¹öÀü¸íÀ» ÁöÁ¤ÇÏ°í underbar(_) ¸¦ µÎ °³ ºÙ¿©¾ß ÇÔ.
  • À̽´°ü¸® ½Ã½ºÅÛÀ» »ç¿ëÇÑ´Ù¸é ÆÄÀÏ¸í¿¡ Issue number ³ª ticket ¸íÀ» ºÙÀ̸é history °ü¸®½Ã ´õ Æí¸®ÇÒ °Í °°À½(Ex: V3__PRJ-123_Create_person_table.sql)

Sql Script syntax

  • Single- or multi-line statements
  • Flexible placeholder replacement
  • Single- (--) or Multi-line (/**/) comments spanning complete lines
  • Database-specific SQL syntax extensions (PL/SQL, T-SQL, ...)
Sample
/* Single line comment */
CREATE TABLE test_user (
  name VARCHAR(25) NOT NULL,
  PRIMARY KEY(name)
);
 
/*
Multi-line
comment
*/
 
-- Placeholder
INSERT INTO ${tableName} (nameVALUES ('Mr. T');

 

maven Àû¿ë ¿¹Á¦

  • JDK 5+ ¹× maven 2, 3 ÇÊ¿ä

Create the project

  1. maven archetype À¸·Î ¿¹Á¦ ÇÁ·ÎÁ§Æ® »ý¼º

    mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.1 -DgroupId=foo -DartifactId=bar -Dversion=1.0-SNAPSHOT -Dpackage=foobar
  2. cd bar

 

pom ¿¡ ÀÇÁ¸¼º Ãß°¡

<project ...>
    ...
    <dependencies>
        <dependency>
            <groupId>com.googlecode.flyway</groupId>
            <artifactId>flyway-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.170</version>
        </dependency>
        ...
    </dependencies>
    ...
</project>

Flyway¿Í ÅëÇÕ

src/main/java/foobar/App.java ¿¡ Flyway migration  ¹× »ç¿ëÇÒ DBMS Á¤º¸ ¼³Á¤

package foobar;
 
import com.googlecode.flyway.core.Flyway;
 
public class App {
    public static void main(String[] args) {
        // Create the Flyway instance
        Flyway flyway = new Flyway();
 
        // Point it to the database
        flyway.setDataSource("jdbc:h2:file:target/foobar""sa"null);
 
        // Start the migration
        flyway.migrate();
    }
}

 

ù¹ø° migration »ý¼º

  1. migration directory »ý¼º

    mkdir -p src/main/resources/db/migration
  2. src/main/resources/db/migration/V1__Create_person_table.sql migration schema »ý¼º

    create table PERSON (
        ID int not null,
        NAME varchar(100) not null
    );

ù¹ø° migration ½ÇÇà

mvn ¸í·É¾î·Î ù¹ø° migration ½ÇÇà

bar> mvn package exec:java -Dexec.mainClass=foobar.App
2014115 ¿ÀÀü 2:12:22 com.googlecode.flyway.core.metadatatable.MetaDataTableImpl createIfNotExists
Á¤º¸: Creating Metadata table: "PUBLIC"."schema_version"
2014115 ¿ÀÀü 2:12:22 com.googlecode.flyway.core.command.DbMigrate migrate
Á¤º¸: Current version of schema "PUBLIC": << Empty Schema >>
2014115 ¿ÀÀü 2:12:22 com.googlecode.flyway.core.command.DbMigrate applyMigration
Á¤º¸: Migrating schema "PUBLIC" to version 1
2014115 ¿ÀÀü 2:12:22 com.googlecode.flyway.core.command.DbMigrate logSummary
Á¤º¸: Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.052s).

µÎ¹ø° migration ½ÇÇà

src/main/resources/db/migration/V2__Add_people.sql ¿¡ µÎ¹ø° migration schema ÀÛ¼º

insert into PERSON (ID, NAME) values (1'Axel');
insert into PERSON (ID, NAME) values (2'Mr. Foo');
insert into PERSON (ID, NAME) values (3'Ms. Bar');
bar> mvn package exec:java -Dexec.mainClass=foobar.App
½ÇÇà °á°ú
2014115 ¿ÀÀü 2:14:21 com.googlecode.flyway.core.command.DbMigrate migrate
Á¤º¸: Current version of schema "PUBLIC"1
2014115 ¿ÀÀü 2:14:21 com.googlecode.flyway.core.command.DbMigrate applyMigration
Á¤º¸: Migrating schema "PUBLIC" to version 2
2014115 ¿ÀÀü 2:14:21 com.googlecode.flyway.core.command.DbMigrate logSummary
Á¤º¸: Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.035s).

 

Command-line tools ·Î migration

http://flywaydb.org/getstarted/firststeps/commandline.html

Maven plugin, API, Ant µîÀÌ ÀÖÁö¸¸ Command-line ÀÌ Á¦ÀÏ Æí¸®ÇÑ°Í °°¾Æ¼­ cmd-tool·Î db migration Á¤¸®

¼³Ä¡

  1. http://flywaydb.org/getstarted/firststeps/commandline.html ¿¡¼­ ´Ù¿î·Îµå
  2. unzip flyway-commandline-2.3.zip
  3. H2 driver ¸¦ »ç¿ëÇÑ´Ù¸é http://repo1.maven.org/maven2/com/h2database/h2/1.3.170/h2-1.3.170.jar ¿¡¼­ ´Ù¿î·ÎµåÈÄ¿¡ flywayÀÇ jars Æú´õ¿¡ º¹»ç
  4. MySQL Àº http://dev.mysql.com/downloads/connector/j/ ¿¡¼­ driver ´Ù¿î·ÎµåÈÄ jars ¿¡ º¹»ç
  5. ¾ÐÃà Ç®¸° Æú´õ¸¦ /usr/local µî¿¡ À̵¿ÇÏ°í profile ¿¡ Æнº ¼³Á¤Çϰųª alias ·Î ¿¬°á

    alias flyway='/usr/local/flyway-2.3/flyway'
    export PATH=$PATH:/usr/local/flyway-2.3/

¼³Á¤

conf/flyway.properties ¿¡ ÇÊ¿äÇÑ ¼³Á¤ Ãß°¡

# Jdbc url to use to connect to the database
flyway.url=jdbc:h2:file:./foobardb
## MySQL
#flyway.url=flyway.url=jdbc:mysql://localhost:3306/lesstif?useUnicode=true&characterEncoding=utf8
 
# User to use to connect to the database (default: <<null>>)
flyway.user=SA
  
## schema sql À» ÀúÀåÇÒ Æú´õ. ±âº»ÀûÀ¸·Î flyway ¼³Ä¡Æú´õ¹Ø¿¡ sql ¿¡¼­ ã°Ô µÈ´Ù. ¿©·¯°³¸¦ ÁöÁ¤ÇÒ °æ¿ì , ·Î ±¸ºÐÇÑ´Ù.
## filesystem ¶Ç´Â classpath ·Î ÁöÁ¤ÇÑ´Ù. ${HOME} °°ÀÌ property ±â¹Ý ¼³Á¤Àº µ¿ÀÛÇÏÁö ¾Ê´Â´Ù.
flyway.locations=filesystem:/home/lesstif/work/bar/sql

»ç¿ë¹ý

flyway [options] command

command

Commands
Description
 
clean½ºÅ°¸¶ Á¤º¸ Å×À̺íÀÇ migration µÈ ¸ðµç ¿ÀºêÁ§Æ®¸¦ Áö¿î´Ù. ¿î¿µ ȯ°æÀ̶ó¸é ½É°¢ÇÑ ¹®Á¦¸¦ ÀÏÀ¸Å³ ¼ö ÀÖÀ¸´Ï ÁÖÀÇ. 
initmetadata table À» »ý¼º ¹× ÃʱâÈ­ÇÑ´Ù. ÀÌ¹Ì »ý¼ºµÇ¾î ÀÖ´Ù¸é ¿¡·¯¸¦ ¹ß»ý½ÃŲ´Ù, 
migrateMigrates the database 
validateValidates the applied migrations against the ones on the classpath 
infoPrints the information about applied, current and pending migrations 
repairRepairs the metadata table after a failed migration 

ÁÖ¿ä options

config ¿¡ ÀÖ´Â ¼³Á¤ Ç׸ñµéÀ» runtime ¿¡ options À¸·Î ó¸® °¡´É

-X : µð¹ö±ë¿ë ÀÚ¼¼ÇÑ Ãâ·Â

 

ù¹ø° migration »ý¼º

À§ sql µð·ºÅ丮 ¹Ø¿¡ V1__Create_person_table.sql »ý¼º

V1__Create_person_table.sql
create table PERSON (
    ID int not null,
    NAME varchar(100) not null
);

ù¹ø° migration ½ÇÇà

flyway migrate
 /usr/bin/tput
Flyway (Command-line Tool) v.2.3
Creating Metadata table: "PUBLIC"."schema_version"
Current version of schema "PUBLIC": << Empty Schema >>
Migrating schema "PUBLIC" to version 1
Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.087s).

µÎ¹ø° migration ½ÇÇà

sql µð·ºÅ丮 ¹Ø¿¡ V2__Add_people.sql »ý¼º

V2__Add_people.sql
insert into PERSON (ID, NAME) values (1'Axel');
insert into PERSON (ID, NAME) values (2'Mr. Foo');
insert into PERSON (ID, NAME) values (3'Ms. Bar');
flyway migrate
/usr/bin/tput
Flyway (Command-line Tool) v.2.3
 
Current version of schema "PUBLIC"1
Migrating schema "PUBLIC" to version 2
Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.056s).

migration Á¤º¸ È®ÀÎ

flyway info
+----------------+-----------------------------------------------------------------------------------------+---------------------+---------+
| Version        | Description                                                                             | Installed on        | State   |
+----------------+-----------------------------------------------------------------------------------------+---------------------+---------+
1              | Create person table                                                                     | 2014-01-15 17:34:52 | Success |
2              | Add people                                                                              | 2014-01-15 17:34:52 | Success |
+----------------+-----------------------------------------------------------------------------------------+---------------------+---------+

 

command line arguent overide

driver³ª sql Æú´õµîÀÌ config ¿¡ ÁöÁ¤µÇ¾î ÀÖÀ¸¹Ç·Î ¿©·¯ °³ÀÇ DB °¡ Àְųª ¿©·¯ ÇÁ·ÎÁ§Æ®¸¦ ±¸µ¿ÇÒ °æ¿ì flywayÀÇ config ¿É¼ÇÀ» ¼öÁ¤ÇØ¾ß ÇÏ´Â ºÒÆíÇÔÀÌ ÀÖ´Ù. À̸¦ À§ÇØ ±¸µ¿½Ã ¼³Á¤°ªÀ» ¹Ù²Ü¼ö ÀÖ´Â ±â´ÉÀ» Á¦°øÇÑ´Ù.

Çü½ÄÀº -key=value À̸ç key °ªÀº config ¿¡ ÁöÁ¤µÈ °ª¿¡¼­ flyway ¶ó´Â prefix ¸¦ Á¦°ÅÇÏ¸é µÈ´Ù.

migration ¿ë SQL  ÆÄÀÏÀº locations property ÀÌ°í JDBC driver µîÀ» ã´Â property´Â jarDir ÀÌ´Ù.

flyway -url="jdbc:mysql://localhost:3306/lesstif?useUnicode=true&characterEncoding=utf8" -user=dbusers -password=dbpwd -locations=/home/lesstif/proj1/sql -jarDir=/home/lesstif/prj1/flyway-jars

 

¼Ò°¨

  • flyway ´Â open source À̳ª »ó¿ë ±â¼ú Áö¿øÀ¸·Î µ·À» ¹ö´Â »ç¾÷ ¸ðµ¨ äÅÃÇÔ. ±×·¡¼­ ÀϹÝÀûÀÎ ¿ÀǼҽºº¸´Ù ¹®¼­ Ç°ÁúÀÌ ¸Å¿ì ÈǸ¢ÇÔ
  • maven, command-tool, API, Gradle, Ant µî°ú ¿¬°èÇؼ­ »ç¿ë °¡´ÉÇϳª °³ÀÎÀûÀ¸·Î´Â command-tool ÀÌ ½ÇÁ¦ ¿î¿µ ȯ°æ¿¡ migrate Çϱ⿡ ÀûÇÕÇÑ°Í °°À½
    • ¿î¿µ ȯ°æ¿¡¼­ web app µðÇ÷ÎÀ̸¦ maven À¸·Î ¹Ù·Î ÇÏÁö´Â ¾ÊÀ¸¹Ç·Î db migration ÀÛ¾÷¿¡ ¸ÞÀ̺ìÀ» »ç¿ëÇÏ´Â °Í º¸´Ù´Â Ä¿¸Çµå ¶óÀÎ ¹æ½ÄÀÌ ÀûÀý
  • ruby on rails ¿Í ´Þ¸® rollback ÀÌ ¾øÀ¸³ª ½ÇÁ¦·Î db ÀÛ¾÷½Ã rollback Àº ¸®½ºÅ©°¡ Å©¹Ç·Î Àû¿ëÇÏÁö ¾Ê´Â´Ù°í ÇÏ´Ï ºüÁ®µµ »ó°üÀº ¾øÀ»µí
  • schema ÀÇ ¹öÀüÀº ÆÄÀϸíÀ¸·Î ÆÇ´ÜÇϹǷΠ°³¹ßÀÚ°¡ ¹öÀüÀ» Á÷Á¢ ÁöÁ¤ÇØ ÁÖ¾î¾ß ÇÔ. ÆÄÀÏÀÇ ¹öÀüÀÌ °°À¸¸é ¿¡·¯°¡ ¹ß»ýÇϹǷΠ½Ç¼ö ¿©Áö´Â ÀûÀ» °Í °°À½
  • flyway clean ¸í·É¾î´Â Àû¿ëµÈ migration À» ¸ðµÎ »èÁ¦ÇϹǷΠ¸Å¿ì À§ÇèÇϳª º°µµÀÇ ¾ÈÀüÀåÄ¡°¡ ¾øÀ½
    • ÃÖÃÊ¿¡ ½ºÅ°¸¶ »ý¼ºÀ» flyway ·Î ÇÏ°í ¿î¿µÁß¿¡ clean ¸í·ÉÀ» ½ÇÇàÇÏ¸é ¸ðµç table ÀÌ drop µÇ¾î ¹ö¸®¹Ç·Î ½É°¢ÇÑ »óȲÀÌ ¹ß»ýÇÔ
  • ¿î¿µÈ¯°æ¿¡¼­ clean ¸í·ÉÀº »ç¿ëÇÏÁö ¸øÇÏ°Ô ÇÏ´Â ¾ÈÀü ÀåÄ¡°¡ ÇÊ¿äÇÔ
    • ¼Ò½º¸¦ ¼öÁ¤Çؼ­ ¿î¿µ¿ë(clean »ç¿ë ºÒ°¡)°ú °³¹ß/Å×½ºÆ®¿ë flyway(clean »ç¿ë °¡´É) ·Î ³ª´©¾î »ç¿ëÇÏ´Â°Ç ¸Å¿ì ¹ø°Å·Ó°í flyway »õ ¹öÀüÀÌ ³ª¿À¸é ¼Ò½º ¼öÁ¤À» ÇØ¾ß ÇϹǷΠÁÁÀº ¹æ¹ýÀº ¾Æ´Ñ°Í °°À½

 

Ref

http://flywaydb.org/