概述
我将介绍如何使用Docker在本地环境中构建数据库迁移环境。
说到使用Flyway进行迁移,使用gradle和sbt等插件是否普遍?我认为,但是还有一种使用命令行执行它的方法。
https://flywaydb.org/getstarted/
并且由于包含该工具的官方Docker Image,我将尝试使用此工具在本地构建迁移环境。
https://hub.docker.com/r/boxfuse/flyway/
你想做的事
- 在Docker容器中启动PostgreSQL
- 执行从Flyway的Docker容器到PostgreSQL的迁移
1.创建所需的文件
docker-compose
的定义
在管理多个相关的Docker容器时,使用docker-compose进行操作很方便,因此我将编写yaml。
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | version: '3.4' x-template: &flyway-template image: boxfuse/flyway:latest volumes: - ./sql:/flyway/sql # マイグレーション用SQLファイルの格納先 - ./conf:/flyway/conf # 設定ファイルの格納先 depends_on: - db services: flyway-clean: <<: *flyway-template command: clean flyway-migrate: <<: *flyway-template command: migrate flyway-info: <<: *flyway-template command: info db: image: postgres:latest environment: - POSTGRES_USER=user - POSTGRES_PASSWORD=pass ports: - 5432:5432 volumes: - ./init:/docker-entrypoint-initdb.d # Create DataBase するinit用のSQLの格納先 container_name: db |
flyway.conf文件定义
指定在docker-compose中定义的数据库连接目标信息。
https://flywaydb.org/documentation/configfiles
flyway.conf
1 2 3 | flyway.url=jdbc:postgresql://db:5432/test_db flyway.user=user flyway.password=pass |
* Compose为应用程序设置一个网络。
每个容器上的服务加入默认网络后,便可以从同一网络上的其他容器进行连接,因此它可以使用容器名称
创建用于初始化的SQL文件
SQL
创建要迁移的表
create_database.sql
1 | CREATE DATABASE test_db; |
创建用于迁移的SQL文件
暂时适当准备两个
V0.001__create_table.sql
1 | CREATE TABLE IF NOT EXISTS test_table(test_id int, test_name varchar(255)); |
V0.002__insert_table.sql
1 | INSERT INTO test_table(test_id, test_name) VALUES (1, 'test'); |
目录结构
到目前为止,已创建的文件最终被放置在这样的配置中。
1 2 3 4 5 6 7 8 9 | flyway-migration ├── docker-compose.yml ├── conf │ └── flyway.conf ├── init │ └── create_database.sql └── sql ├── V0.001__create_table.sql └── V0.002__insert_table.sql |
2.启动PostgreSQL容器
启动容器
1 | $ docker-compose up -d db |
确认以名称
启动
1 2 3 4 | $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 52cbb658ae0a postgres:latest "docker-entrypoint.s…" 46 hours ago Up 46 hours 0.0.0.0:5432->5432/tcp db |
3.飞路迁移执行
准备好的迁移SQL按
的顺序执行
1 2 3 4 5 6 7 8 9 | $ docker-compose run --rm flyway-migrate Starting db ... done Flyway Community Edition 5.1.4 by Boxfuse Database: jdbc:postgresql://db:5432/test_db (PostgreSQL 10.5) Successfully validated 2 migrations (execution time 00:00.040s) Current version of schema "public": 0.002 Schema "public" is up to date. No migration necessary. |
您可以查看迁移执行历史记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ docker-compose run --rm flyway-info Starting db ... done Flyway Community Edition 5.1.4 by Boxfuse Database: jdbc:postgresql://db:5432/test_db (PostgreSQL 10.5) Schema version: 0.002 +-----------+---------+--------------+------+---------------------+---------+ | Category | Version | Description | Type | Installed On | State | +-----------+---------+--------------+------+---------------------+---------+ | Versioned | 0.001 | create table | SQL | 2018-10-01 17:05:42 | Success | | Versioned | 0.002 | insert table | SQL | 2018-10-01 17:05:42 | Success | +-----------+---------+--------------+------+---------------------+---------+ |
确保正确创建了表。
1 2 3 4 5 6 7 8 9 10 11 12 | $ pgcli -h localhost -U user -p 5432 user@localhost:user> \connect test_db You are now connected to database "test_db" as user "user" Time: 0.018s user@localhost:test_db> select * from test_table; +-----------+-------------+ | test_id | test_name | |-----------+-------------| | 1 | test | +-----------+-------------+ |
您可以做到!
如果要删除迁移
创建的表,请执行干净
1 2 3 4 5 6 | $ docker-compose run --rm flyway-clean Starting db ... done Flyway Community Edition 5.1.4 by Boxfuse Database: jdbc:postgresql://db:5432/test_db (PostgreSQL 10.5) Successfully cleaned schema "public" (execution time 00:00.042s) |
源代码
源代码发布在↓,所以如果您不介意的话。
https://github.com/supimen/flyway-docker-migration
这就是
的全部。