4. DBマイグレーションの仕組み
• マイグレーションのためのSQLやコードをバージョン毎に管理
• それらを順次実行していくことでマイグレーションを実現
• 積み重ねた後のものが、現在の状態を表すものになる
CREATE TABLE products
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
description text,
created_at timestamp NOT NULL,
updated_at timestamp NOT NULL
);
class CreateProducts < ActiveRecord::Migration[5.0]
def change
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
11. 3. マイグレーションで実行するSQLファイルを作成
sql/ 配下にSQLファイルを作成。
~/flyway-8.0.2$ vi sql/V1__create_initial_tables.sql
CREATE TABLE customers (
customer_id serial PRIMARY KEY,
name text NOT NULL,
mail_address text NOT NULL
);
sql/V1__create_initial_tables.sql
V1__create_initial_tables.sql
Prefix
Version
Separator Suffix
Description
12. 4. マイグレーション実行
flyway migrate でマイグレーション実行。
~/flyway-8.0.2$ ./flyway migrate
Flyway Teams Edition 8.0.2 by Redgate
Database: jdbc:postgresql://192.168.33.10:5432/testdb (PostgreSQL 13.4)
Successfully validated 1 migration (execution time 00:00.025s)
Creating Schema History table "public"."flyway_schema_history" ...
Current version of schema "public": << Empty Schema >>
Migrating schema "public" to version "1 - create initial tables"
Successfully applied 1 migration to schema "public", now at version v1 (execution time 00:00.061s)
13. 4. マイグレーション実行
テーブルが作成されていることを確認。
~/flyway-8.0.2$ psql -U user1 testdb
psql (13.4)
Type "help" for help.
testdb=> ¥d customers
Table "public.customers"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+------------------------------------------------
customer_id | integer | | not null | nextval('customers_customer_id_seq'::regclass)
name | text | | not null |
mail_address | text | | not null |
Indexes:
"customers_pkey" PRIMARY KEY, btree (customer_id)
testdb=>
15. マイグレーションを追加
flyway migrate で再度マイグレーション実行。
~/flyway-8.0.2$ ./flyway migrate
Flyway Teams Edition 8.0.2 by Redgate
Database: jdbc:postgresql://192.168.33.10:5432/testdb (PostgreSQL 13.4)
Successfully validated 2 migrations (execution time 00:00.054s)
Current version of schema "public": 1
Migrating schema "public" to version "2 - add phone number“
Successfully applied 1 migration to schema "public", now at version v2 (execution time 00:00.083s)
16. マイグレーションを追加
カラム(phone_number)が追加されていることを確認。
~/flyway-8.0.2$ psql -U user1 testdb
psql (13.4)
Type "help" for help.
testdb=> ¥d customers
Table "public.customers"
Column | Type | Collation | Nullable | Default
--------------+-----------------------+-----------+----------+------------------------------------------------
customer_id | integer | | not null | nextval('customers_customer_id_seq'::regclass)
name | text | | not null |
mail_address | text | | not null |
phone_number | character varying(20) | | |
Indexes:
"customers_pkey" PRIMARY KEY, btree (customer_id)
testdb=>
17. マイグレーション実行状況の確認
マイグレーションの実行状況は flyway info で確認できる。
※ flyway_schema_historyというテーブルがFlywayにより作成され、そこで情報が管理されている
~/flyway-8.0.2$ ./flyway info
Flyway Teams Edition 8.0.2 by Redgate
Database: jdbc:postgresql://192.168.33.10:5432/testdb (PostgreSQL 13.4)
Schema version: 2
+-----------+---------+-----------------------+------+---------------------+---------+----------+
| Category | Version | Description | Type | Installed On | State | Undoable |
+-----------+---------+-----------------------+------+---------------------+---------+----------+
| Versioned | 1 | create initial tables | SQL | 2021-10-30 01:26:12 | Success | No |
| Versioned | 2 | add phone number | SQL | 2021-10-30 05:20:02 | Success | No |
+-----------+---------+-----------------------+------+---------------------+---------+----------+
28. SchemaSpy – 設定
設定ファイルを作成。
# type of database. Run with -dbhelp for details
schemaspy.t=pgsql11
# optional path to alternative jdbc drivers.
schemaspy.dp=drivers
# database properties: host, port number, name user, password
schemaspy.host=192.168.33.10
schemaspy.port=5432
schemaspy.db=testdb
schemaspy.u=user1
schemaspy.p=password1
# output dir to save generated files
schemaspy.o=output
# db scheme for which generate diagrams
schemaspy.s=public
schemaspy.properties