ä»æ¥ã¯ãliquibaseã§ã®äœæ¥ãããå°ã䟿å©ã«ããæ¹æ³ããäŒãããããšæããŸãã æåã§ç§»è¡ãèšè¿°ããã¿ãŒã²ããããŒã¿ããŒã¹ãPostgreSQLã§ããå Žåããã®èšäºãèªãã§ãã ãããèšäºãèªãã®ã«è²»ãããæéã¯ãå¿ ãå©çãããããã§ãããã
Liquibaseã®åäœã«æ¢ã«ç²ŸéããŠããããšãæãã§ããã®ã§ãã·ã¹ãã ã®ä»çµã¿ãç°¡åã«èª¬æããŸãã ããŒã¿ããŒã¹ã«å€æŽãå ããã«ã¯ã移è¡ãã¡ã€ã«ïŒchangesetïŒãäœæããå¿ èŠããããŸãããã®ãã¡ã€ã«ãžã®ãªã³ã¯ã¯changelogãã¡ã€ã«ã§æå®ããå¿ èŠããããŸãããã®åŸã移è¡ãã¿ãŒã²ããããŒã¿ããŒã¹ã«æ£åžžã«é©çšã§ããŸãã ãã®ã¢ãããŒãã®åŠå®ã§ããªãæçœãªå©ç¹ã¯ãè¡ãããå€æŽãããŒã«ããã¯ã§ããããšã§ãã
ç§ã®æèŠã§ã¯ãliquibaseã«ã¯é倧ãªæ¬ ç¹ã1ã€ãããããŸãã-å€æŽã®èªåããŒã«ããã¯ã¯ã移è¡ãXMLãã¡ã€ã«ãšããŠèšè¿°ãããŠããå Žåã«ã®ã¿æ©èœããŸãã ãã®ç¬éã¯ãããŒã¿ããŒã¹ã«å€æŽãå ããéçºè ãšå€æŽãæ€æ»ããããŒã¿ããŒã¹ç®¡çè ã®äž¡æ¹ã®é ãèŠãé ããŸãã XMLããŒã¯ã¢ããã¯ãå€ããŠããç¥ãããŠããSQLãšã¯ç°ãªããèªããã«ã¯ã»ã©é ãã§ãã 幞ããªããšã«ãLiquibaseã䜿çšãããšã移è¡ãSQLââã«æžã蟌ãããšãã§ããŸãããåä»ãªæ¬ ç¹ã¯1ã€ã ãã§ããèªåããŒã«ããã¯ãæ©èœããªããªããããŒã«ããã¯ã»ã¯ã·ã§ã³ãæåã§èšè¿°ããå¿ èŠããããŸãã
ä»æ¥ã¯ãã®èª€è§£ãä¿®æ£ããliquibaseã®ç§»è¡ãã¡ã€ã«ãäœæããæ¹æ³ãåŠã³ãŸãããŸãSQLã§ã次ã«ããŒã«ããã¯ã»ã¯ã·ã§ã³ã§ã次ã«ãããã®ãã¡ã€ã«ãèªåçã«çæãããŸãã ä¿¡ããããªãïŒ æ¬¡ã«ããã¹ãŠã詳现ã«èª¬æããŸãã
次ã®ããã«ç§»è¡ãäœæããŸãã2ã€ã®ããŒã¿ããŒã¹ãããã1ã€ã¯éçºã®æ¡ä»¶ä»ãã§ããã1ã€ã¯æŠéïŒã¹ããŒãžããã¹ã-奜ã¿ã®éžæïŒã§ãã ãæ°ã«å ¥ãã®ããŒã«ã䜿çšããŠéçºããŒã¿ããŒã¹ã«å€æŽãå ããåŸãããŒã¿ããŒã¹ãæ¯èŒããŠç§»è¡ãè¡ãããããã¿ãŒã²ããããŒã¿ããŒã¹ã«å±éããŸãïŒããã«ãšèŠãªããŸãïŒã
以äžã®ãã¹ãŠã®ã³ãã³ãã¯ãLinuxã·ã§ã«ã§ã®äœæ¥ãèæ ®ããŠè¡šç€ºãããŸãã 移è¡ãã¡ã€ã«ã»ã¯ã·ã§ã³ãçæããã«ã¯pgCodeKeeperãå¿ èŠã§ãããããŒã«ããã¯ã»ã¯ã·ã§ã³ãäœæããã«ã¯ã·ã¹ãã ã«sedãå¿ èŠã§ãã
2ã€ã®ããŒã¿ããŒã¹dbdevãšdbprodãããããšã«åæããŸããããdbdevã§ã¯æåã§å€æŽãè¡ããdbprodã§ã¯Liquibaseã䜿çšããŠå€æŽãååŸããŸãã
/ tmpã«ç§»è¡ãã£ã¬ã¯ããªãäœæããŸããããã§ãã¹ãŠã®äœæ¥ãè¡ããŸãããã¡ãããåžæãããã£ã¬ã¯ããªã䜿çšã§ããŸãã æ¢ã«ããŒã«ã«ã³ã³ãã¥ãŒã¿ãŒã«PostgreSQLãã€ã³ã¹ããŒã«ãããŠããã®ã§ãããã䜿çšããŸãã
ä»äºçšã®ããŒã¿ããŒã¹ãäœæããŸãã
$ mkdir /tmp/migration $ cd /tmp/migration/ $ createdb dbdev $ createdb dbprod
PostgreSQLãLiquibaseãããã³pgCodeKeeperãã£ã¹ããªãã¥ãŒã·ã§ã³çšã®JDBCãã©ã€ããŒã®ããŠã³ããŒã
$ wget https://jdbc.postgresql.org/download/postgresql-42.1.3.jar $ wget https://github.com/liquibase/liquibase/releases/download/liquibase-parent-3.4.2/liquibase-3.4.2-bin.tar.gz $ wget http://pgcodekeeper.ru/cli/release/pgCodeKeeper-cli-3.11.4.201707170702.zip
liquibaseãšpgcodekeeperãçŸåšã®ãã£ã¬ã¯ããªã«è§£åããŸãããã¡ãããéžæãããã£ã¬ã¯ããªã«è§£åããŸãã
泚æïŒ ãã®èšäºã®å·ç以éãããã€ãã®å€æŽããããŸããã pgCodeKeeperã®CLIããŒãžã§ã³ã¯ãgithub.com / pgcodekeeper / pgcodekeeper / releasesããããŠã³ããŒãã§ããŸãã
$ tar xzvf liquibase-3.4.2-bin.tar.gz $ unzip pgCodeKeeper-cli-3.11.4.201707170702.zip
çŸåšã®ãã£ã¬ã¯ããªã§ã次ã®å 容ã®liquibase-liquibase.propertiesã®èšå®ãã¡ã€ã«ãäœæããŸãã
driver: org.postgresql.Driver classpath: ./postgresql-42.1.3.jar url: jdbc:postgresql:dbprod username: user password: topsecret changeLogFile: db.changelog.xml
ãããã¯Liquibaseã®èšå®ã§ããããšã«æ³šæããŠãã ããã 移è¡ãå±éããdbprodããŒã¿ããŒã¹ãžã®æ¥ç¶ãèšè¿°ããŸãã ãŠãŒã¶ãŒåãšãã¹ã¯ãŒããèªåã®å€ã«èšå®ããŸãããŠãŒã¶ãŒåagsããããã³ãã³ãã®åºåã®åŸç¶ã®ã¡ãã»ãŒãžã§ãã®ååãæ€åºãããŸãã çŸåšã®ãã£ã¬ã¯ããªã§ã次ã®å 容ã®db.changelog.xmlãã¡ã€ã«ãäœæããŸãã
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> </databaseChangeLog>
liquibaseã®äœ¿çšæºåãã§ããŠãããã©ããã確èªããŸãã
$ ./liquibase status ags@jdbc:postgresql:dbprod is up to date Liquibase 'status' Successful
ãããã£ããååã®ä»äºã¯çµãã£ããã»ãã®å°ãæ®ã£ãŠããã pgCodeKeeperãæ©èœãããã©ããã確èªããŸãã
$ ./pgcodekeeper-cli.sh jdbc:postgresql:dbdev jdbc:postgresql:dbprod
ããŒã ãã¡ãã»ãŒãžãåºããªãã£ãå Žå-ãã¹ãŠãé 調ã§ãã postgresã€ã³ã¹ã¿ã³ã¹ãå¥ã®ãã¹ããŸãã¯ããŒãã§æ§æãããŠããå ŽåããŸãã¯ãã¹ã¯ãŒãã¢ã¯ã»ã¹ã䜿çšãããŠããå Žåã¯ãããŒã¿ããŒã¹ã«é©åãªJDBC URLãäœæããå¿ èŠãããããšã«æ³šæããŠãã ããã ããã圢æããæ¹æ³ã¯ã ãããèªãã§ãã ãã ã
æºåãã§ãŒãºãå®äºããããŒã¹ãäœæãããããŒã«ãã»ããã¢ãããããŸãã-ã¡ã€ã³éšåã«é²ãããšãã§ããŸãã
dbdevããŒã¿ããŒã¹ã«ããŒãã«ãäœæããŸãã
[ags@saushkin-ag:/tmp/migration] $ psql dbdev psql (9.6.3, 9.5.7) "help", . (ags@[local]:5432) 16:08:43 [dbdev] =# create table users (id serial primary key, name text); CREATE TABLE : 20,708 (ags@[local]:5432) 16:09:16 [dbdev] * =# commit; COMMIT : 6,913
pgCodeKeeperãéããèŠã€ããããšãã§ãããã©ããã確èªããŸãã
$ ./pgcodekeeper-cli.sh jdbc:postgresql:dbdev jdbc:postgresql:dbprod CREATE SEQUENCE users_id_seq START WITH 1 INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1; ALTER SEQUENCE users_id_seq OWNER TO ags; CREATE TABLE users ( id integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL, name text ); ALTER TABLE users OWNER TO ags; ALTER TABLE users ADD CONSTRAINT users_pkey PRIMARY KEY (id); ALTER SEQUENCE users_id_seq OWNED BY users.id;
ã¯ãããã¹ãŠãæåŸ ã©ããã«æ©èœããŸãã 移è¡ãäœæããããã®ã¹ã¯ãªããã®æºåmigrate.sh
#!/bin/bash FILENAME=${1:-changeset.sql} # echo "--liquibase formatted sql" > $FILENAME echo "--changeset $USER:$FILENAME" >> $FILENAME echo "" >> $FILENAME # ./pgcodekeeper-cli.sh jdbc:postgresql:dbdev jdbc:postgresql:dbprod >> $FILENAME echo "" >> $FILENAME # ( ) ./pgcodekeeper-cli.sh jdbc:postgresql:dbprod jdbc:postgresql:dbdev | sed -e 's/^/--rollback /' >> $FILENAME
ã¹ã¯ãªããã¯1ã€ã®ãã©ã¡ãŒã¿ãŒãåããŸããåºåãã¡ã€ã«ã®ååã§ãããã¡ã€ã«åãæå®ãããŠããªãå Žåãchangeset.sqlã¯ããã©ã«ãã§åãå ¥ããããŸãã 次ã«ãSQLãã¡ã€ã«ã®ããããŒã圢æãããŸããLiquibaseã¯ãã¿ãŒã²ããããŒã¿ããŒã¹ã«å ããããå€æŽã«é¢ããæ å ±ãä¿åããå¿ èŠããããŸãã 次ã¯ãå®éã®ç§»è¡ãã¡ã€ã«ã®åœ¢æã§ãã
ããŒã ïŒ
./pgcodekeeper-cli.sh jdbc:postgresql:dbdev jdbc:postgresql:dbprod >> $FILENAME
ããŒã¿ããŒã¹éã®éããèŠã€ããããŒã¿ããŒã¹ãªããžã§ã¯ãã®æ§é ãdbdevããdbprodã«å€æããããã®ãã¡ã€ã«ãäœæããåºåãã¡ã€ã«ã«ä¿åããŸãã
ããŒã ïŒ
./pgcodekeeper-cli.sh jdbc:postgresql:dbprod jdbc:postgresql:dbdev | sed -e 's/^/--rollback /' >> $FILENAME
åãããŒã¿ããŒã¹éã®çžéç¹ãç°ãªãæ¹åã§ã®ã¿æ€çŽ¢ãããããã¿ãŒã²ããããŒã¿ããŒã¹ã¯éçºè ã«ãªãã€ã€ãããããå€æŽãèªåçã«ããŒã«ããã¯ã§ããŸãã Liquibaseã®ããŒã«ããã¯ã»ã¯ã·ã§ã³ã«ã¯--rollbackã³ã¡ã³ããä»ããŠããŸãã
移è¡ã®åœ¢æã確èªããŸãã ãã¹ãã§ã¯ãåºåãã¡ã€ã«ãšããŠ/ dev / stdoutã䜿çšããŸãã
$ chmod +x ./migrate.sh $ ./migrate.sh /dev/stdout --liquibase formatted sql --changeset ags:/dev/stdout CREATE SEQUENCE users_id_seq START WITH 1 INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1; ALTER SEQUENCE users_id_seq OWNER TO ags; CREATE TABLE users ( id integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL, name text ); ALTER TABLE users OWNER TO ags; ALTER TABLE users ADD CONSTRAINT users_pkey PRIMARY KEY (id); ALTER SEQUENCE users_id_seq OWNED BY users.id; --rollback ALTER TABLE users --rollback DROP CONSTRAINT users_pkey; --rollback --rollback DROP TABLE users; --rollback
ãŸããããŒã«ã¢ããã»ã¯ã·ã§ã³ãšããŒã«ããã¯ã»ã¯ã·ã§ã³ã®äž¡æ¹ã圢æãããŸãã ãã®ããã1è¡ã®SQLè¡ãæåã§èšè¿°ããå¿ èŠã¯ãããŸããã§ããïŒ ç§»è¡ãã¡ã€ã«ã®äœæã«é¢ãããã¹ãŠã®äœæ¥ã¯èªåçã«è¡ãããŸããã
å®éã®ç§»è¡ãã¡ã€ã«ãäœæããŸã
$ ./migrate.sh 001_users.sql
db.changelog.xmlãã¡ã€ã«ãç·šéãã001_users.sqlãã¡ã€ã«ã«includeãã£ã¬ã¯ãã£ããè¿œå ããŠã移è¡ãã°ã«æ¥ç¶ããŸãã
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> <include file="001_users.sql" /> </databaseChangeLog>
ãããŠãå€æŽãdbprodã«ããŒã«ããŸããå€æŽãããŒã«ããåã«ãã¿ãŒã²ããããŒã¿ããŒã¹ã«ã¿ã°ãé 眮ããŠãå°æ¥ããŒã«ããã¯ã§ããããã«ããŸãã
$ ./liquibase tag 001_before_users Successfully tagged ags@jdbc:postgresql:dbprod Liquibase 'tag' Successful $ ./liquibase migrate Liquibase Update Successful
ã¹ããŒã¿ã¹ã確èªãã
$ ./liquibase status ags@jdbc:postgresql:dbprod is up to date Liquibase 'status' Successful
ããŒãã«ãããŒã«ããåã®ç¶æ ã«ããŒã«ããã¯ãã
$ ./liquibase rollback 001_before_users Liquibase Rollback Successful
ã¹ããŒã¿ã¹ãããäžåºŠç¢ºèªãã
$ ./liquibase status 1 change sets have not been applied to ags@jdbc:postgresql:dbprod Liquibase 'status' Successful
ãã¬ããŒã«ãè¿ã
$ ./liquibase migrate Liquibase Update Successful
ããŒã¿ããŒã¹ã®æ§é ãç°ãªã£ãŠãããã©ããã確èªããŸãããã ããã§ãããŒã«ããã¯ãšããŒã«ããã¯ããè©ŠãããåŸãdbdevããŒã¿ããŒã¹ãšdbprodããŒã¿ããŒã¹ã®ç¶æ ãåãã«ãªããŸãã ããã§ããïŒ
$ ./pgcodekeeper-cli.sh jdbc:postgresql:dbdev jdbc:postgresql:dbprod ALTER TABLE databasechangeloglock DROP CONSTRAINT pk_databasechangeloglock; DROP TABLE databasechangeloglock; DROP TABLE databasechangelog;
pgCodeKeeperã¯ãdbdevã«ã¯databasechangeloglockãšdatabasechangelogã®2ã€ã®ããŒãã«ã¯ååšããªããšèšã£ãŠããŸãã ãããã®ããŒãã«ã¯liquibaseã«ãã£ãŠèªåçã«äœæãããå®è¡ãããããŒã«ã«é¢ããæ å ±ãä¿åãããŸãã ãããã®ããŒãã«ããªããã°ãliquibaseã¯äžå¯èœã§ãã ãããã®ããŒãã«ã移è¡ã®åœ¢æäžã«å¹²æžããªãããã«ããããã®ããŒãã«ã®æ§é ãéçºããŒã¿ããŒã¹ã«ã³ããŒããããpgCodeKeeperã®ãªã¹ãç¡èŠæ©èœã䜿çšã§ããŸãã
ãªããžã§ã¯ããç¡èŠããæ©èœã䜿çšããã«ã¯ãçŸåšã®ãã£ã¬ã¯ããªã«æ¬¡ã®å 容ã®.pgcodekeeperignoreãã¡ã€ã«ãäœæããŸãã
$ cat .pgcodekeeperignore SHOW ALL HIDE REGEX "databasechangelog.*"
ä»ãããŒã
$ ./pgcodekeeper-cli.sh -I .pgcodekeeperignore jdbc:postgresql:dbdev jdbc:postgresql:dbprod
å€æŽã衚瀺ããªãã§ãã ããã
ããã§ãææ¡ãããã¹ããŒã ã«åŸã£ãŠå€æŽãå ããããã«äœãããå¿ èŠããããïŒ
- éçºããŒã¿ããŒã¹ã«å€æŽãå ããŸã
- ã¹ã¯ãªããã䜿çšããŠã移è¡ã圢æããŸã
- 移è¡ãæ€æ»ãã移è¡ãã°ã«æ¥ç¶ããŸã
- æŠéïŒãã¹ããã¹ããŒãžïŒããŒã¿ããŒã¹ãžã®å€æŽãå±éãã
- å¿ èŠã«å¿ããŠãããŒã«ããã¯ãå®è¡ããŸã
Liquibase移è¡ãã¡ã€ã«ã®äœæãèªåçã«å®è¡ã§ããããã«ãªããŸããã 移è¡ã¯SQLã§èšè¿°ãããŠãããéèŠãªããšã«ãããŒã«ããã¯ã»ã¯ã·ã§ã³ã人éã®ä»å ¥ãªãã«äœæãããŸãã äžçªé£ããã®ã¯ã移è¡ãã¡ã€ã«åãèãåºãããšã§ãã
PSïŒLiquibaseã«ã¯2ã€ã®ããŒã¿ããŒã¹éã§å·®ç°ã»ãããçæããããã®ã¡ã«ããºã ãçµã¿èŸŒãŸããŠããŸãããæ®å¿µãªãããåžžã«ããŸãæ©èœãããšã¯éããŸããã
PPSïŒèšäºã®å·ç以æ¥ãCLIãªãªãŒã¹ã®é 眮ãå€æŽãããŸãããçŸåšã¯æ¬¡ã®ãšããã§ãïŒ github.com/pgcodekeeper/pgcodekeeper/releases
Eclipseã®æŽæ°ãµã€ãã¯ãã¡ãïŒ pgcodekeeper.org/update