Created
March 11, 2018 10:07
-
-
Save nishio-dens/a1af5753002bc04c43847fc79d16d876 to your computer and use it in GitHub Desktop.
本番環境のデータを別環境に差分コピーするRuby製ツールを作りました ref: https://qiita.com/nishio-dens/items/182d752da57e6b46c023
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem install gamma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `ar_internal_metadata` ( | |
`key` varchar(255) NOT NULL, | |
`value` varchar(255) DEFAULT NULL, | |
`created_at` datetime NOT NULL, | |
`updated_at` datetime NOT NULL, | |
PRIMARY KEY (`key`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
CREATE TABLE `products` ( | |
`id` bigint(20) NOT NULL AUTO_INCREMENT, | |
`name` varchar(255) DEFAULT NULL, | |
`image_path` varchar(512) DEFAULT NULL, | |
`created_at` datetime DEFAULT NULL, | |
`updated_at` datetime DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
CREATE TABLE `users` ( | |
`id` bigint(20) NOT NULL AUTO_INCREMENT, | |
`email` varchar(255) DEFAULT NULL, | |
`encrypted_password` varchar(255) DEFAULT NULL, | |
`created_at` datetime DEFAULT NULL, | |
`updated_at` datetime DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hooks/mask_email.rb | |
# hooks の column 指定をすると、対象行の column名 と value がhookスクリプトに渡されます | |
# apply は dryrunのときには false となります | |
class MaskEmail | |
def execute(apply, column, value) | |
result = value.split("@")[0] | |
result = "#{result}@example.com" | |
unless apply | |
puts "[DRYRUN] #{column} #{value}" | |
end | |
result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gamma dryrun --settings settings.yml --data data.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I, [2018-03-11T18:51:35.611121 #76155] INFO -- : [replace] Sync Start products | |
[DRYRUN] Copy Image: {"id"=>686, "name"=>"production_product_685", "image_path"=>"/test/image/685.png", "created_at"=>2018-03-11 06:13:27 +0900, "updated_at"=>2018-03-11 02:13:27 +0900} | |
I, [2018-03-11T18:51:35.726459 #76155] INFO -- : DRYRUN: UPDATE `products` SET `id` = "686",`name` = "production_product_685",`image_path` = "/test/image/685.png",`created_at` = "2018-03-11 06:13:27",`updated_at` = "2018-03-11 02:13:27" WHERE id = 686 | |
I, [2018-03-11T18:51:36.618468 #76155] INFO -- : [replace] Sync Start users | |
[DRYRUN] email test_email_0@example.com | |
I, [2018-03-11T18:51:36.739384 #76155] INFO -- : DRYRUN: UPDATE `users` SET `id` = "1",`email` = "test_email_0@example.com",`encrypted_password` = "cfcd208495d565ef66e7dff9f98764da",`created_at` = "2018-03-11 06:13:23",`updated_at` = "2018-03-11 13:13:23" WHERE id = 1 | |
[DRYRUN] email test_email_1@example.com | |
I, [2018-03-11T18:51:36.739654 #76155] INFO -- : DRYRUN: UPDATE `users` SET `id` = "2",`email` = "test_email_1@example.com",`encrypted_password` = "c4ca4238a0b923820dcc509a6f75849b",`created_at` = "2018-03-11 06:13:23",`updated_at` = "2018-03-11 13:13:23" WHERE id = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gamma apply --settings settings.yml --data data.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I, [2018-03-11T18:52:32.250442 #76616] INFO -- : [replace] Sync Start products | |
Copy Image. path: /test/image/685.png | |
I, [2018-03-11T18:52:33.268832 #76616] INFO -- : [replace] Sync Start users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# settings.yml | |
in_database_config: | |
adapter: mysql2 | |
encoding: utf8 | |
database: gamma_production | |
pool: 5 | |
host: localhost | |
username: root | |
password: | |
out_database_config: | |
adapter: mysql2 | |
encoding: utf8 | |
database: gamma_development | |
pool: 5 | |
host: localhost | |
username: root | |
password: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# data.yml | |
- data: | |
table: | |
- "*" | |
table_without: | |
- "ar_internal_metadata" | |
mode: "replace" | |
delta_column: "updated_at" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gamma dryrun --settings settings.yml --data data.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I, [2018-03-11T19:02:54.482910 #81194] INFO -- : [replace] Sync Start products | |
I, [2018-03-11T19:02:54.612671 #81194] INFO -- : DRYRUN: INSERT INTO products(`id`,`name`,`image_path`,`created_at`,`updated_at`) VALUES ("9","production_product_8","/test/image/8.png","2018-03-11 06:13:26","2018-03-11 06:13:26") | |
I, [2018-03-11T19:02:55.518998 #81194] INFO -- : [replace] Sync Start users | |
I, [2018-03-11T19:02:55.633015 #81194] INFO -- : DRYRUN: UPDATE `users` SET `id` = "12",`email` = "test_email_11@example.com",`encrypted_password` = "6512bd43d9caa6e02c990b0a82652dca",`created_at` = "2018-03-11 06:13:23",`updated_at` = "2018-03-11 06:13:23" WHERE id = 12 | |
I, [2018-03-11T19:02:55.633129 #81194] INFO -- : DRYRUN: INSERT INTO users(`id`,`email`,`encrypted_password`,`created_at`,`updated_at`) VALUES ("14","test_email_13@example.com","c51ce410c124a10e0db5e4b97fc2af39","2018-03-11 06:13:23","2018-03-11 06:13:23") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gamma apply --settings settings.yml --data data.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I, [2018-03-11T18:42:44.713544 #72090] INFO -- : [replace] Sync Start products | |
I, [2018-03-11T18:42:45.722601 #72090] INFO -- : [replace] Sync Start users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- data: | |
table: | |
- "products" | |
mode: "replace" | |
delta_column: "updated_at" | |
hooks: | |
- row: | |
scripts: | |
- "hooks/copy_image.rb" | |
- data: | |
table: | |
- "users" | |
mode: "replace" | |
delta_column: "updated_at" | |
hooks: | |
- column: | |
name: | |
- "email" | |
scripts: | |
- "hooks/mask_email.rb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hooks/copy_image.rb | |
# hooks の row指定をすると、特定行全てが含まれた値がhookスクリプトに渡されます | |
# apply は dryrunのときには false となります | |
# record には DBのレコードが入っています | |
class CopyImage | |
def execute(apply, record) | |
if apply | |
# ここに画像コピーする処理を記載する | |
puts "Copy Image. path: #{record["image_path"]}" | |
else | |
puts "[DRYRUN] Copy Image: #{record}" | |
end | |
record | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment