multi_db_migration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d5cf93d64500c13a30417619c04b4dd63719f9cae1c5709ea3f1c59dc500b427
4
+ data.tar.gz: 4d6441944ee98083f0ae0be1015c779b09bb8755e49bf96e5b96e3f56595d0ed
5
+ SHA512:
6
+ metadata.gz: 7c6153f530af30f3615ea50779d6eb41f8eec39028999ca871188ee37844c722a40dde8e489b4a4d6b8d32773bf2532be3c01475d8fa27d1f18c0f2275f4f9e4
7
+ data.tar.gz: 512fb3716f3f9205f7a0235a518ca7fb3051ea5578bbd7c63ac178b15024f2c880e15a0992e38b49529b8b1c53503e92a6bf33a429cde24f2169a865dba7c1ed
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ vendor/
14
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in multi_db_migration.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Takahiro Ooishi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # MultiDbMigration
2
+
3
+ Multiple database migration use ridgepole
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'multi_db_migration'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install multi_db_migration
20
+
21
+ ## Configuration
22
+
23
+ - `config/multi_db_migration.yml`
24
+
25
+ ```yaml
26
+ parallel_num: 1
27
+ db:
28
+ config: config/database.yml
29
+ shards:
30
+ primary:
31
+ options:
32
+ - --spec-name=primary
33
+ - --ignore-tables=^need_ignore_tables$
34
+ - --skip-drop-table
35
+ - --file=db/schemas/primary/Schemafile
36
+ animals:
37
+ options:
38
+ - --spec-name=animals
39
+ - --ignore-tables=^need_ignore_tables$
40
+ - --skip-drop-table
41
+ - --file=db/schemas/animals/Schemafile
42
+ ```
43
+
44
+ ## ENV
45
+
46
+ | Name | Default | Description |
47
+ | --- | --- | --- |
48
+ | MULTI_DB_MIGRATION_CONFIG | config/multi_db_migration.yml | path for multi_db_migration.yml |
49
+
50
+ ## Usage
51
+
52
+ - dry run
53
+
54
+ ```
55
+ bundle rake multi_db_migration:dry_run
56
+ ```
57
+
58
+ - migrate
59
+
60
+ ```
61
+ bundle rake multi_db_migration:migrate
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taka0125/multi_db_migration.
73
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multi_db_migration"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
4
+ require 'multi_db_migration/version'
5
+ require 'multi_db_migration/ridgepole_client'
6
+ require 'multi_db_migration/config'
7
+ require 'multi_db_migration/railtie' if defined?(::Rails::Railtie)
8
+
9
+ module MultiDbMigration
10
+ class Error < StandardError; end
11
+ end
@@ -0,0 +1,47 @@
1
+ require 'yaml'
2
+
3
+ module MultiDbMigration
4
+ class Config
5
+ DEFAULT_PATH = 'config/multi_db_migration.yml'
6
+ DEFAULT_PARALLEL_NUM = 1
7
+ DEFAULT_DB_CONFIG_PATH = 'config/database.yml'
8
+
9
+ def initialize(path = nil)
10
+ @path = path.presence || DEFAULT_PATH
11
+ end
12
+
13
+ def parallel_num
14
+ config.dig(:parallel_num)&.to_i || DEFAULT_PARALLEL_NUM
15
+ end
16
+
17
+ def db_config
18
+ config.dig(:db, :config) || DEFAULT_DB_CONFIG_PATH
19
+ end
20
+
21
+ def shards
22
+ config.dig(:shards)&.keys || []
23
+ end
24
+
25
+ def shard_config(shard)
26
+ config.dig(:shards, shard)
27
+ end
28
+
29
+ private
30
+
31
+ def config
32
+ @config ||= load_yaml
33
+ end
34
+
35
+ def load_yaml
36
+ raise "#{@path} not found." unless ::File.exist?(@path)
37
+
38
+ yaml = ::ERB.new(File.read(@path)).result
39
+ ::YAML.safe_load(
40
+ yaml,
41
+ permitted_classes: [Symbol],
42
+ permitted_symbols: [],
43
+ aliases: true
44
+ )&.with_indifferent_access
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module MultiDbMigration
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/multi_db_migration.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ module MultiDbMigration
2
+ class RidgepoleClient
3
+ def initialize(shard_config: nil, env: nil, config: nil, dry_run: true)
4
+ @shard_config = shard_config.presence || {}
5
+ @env = env
6
+ @config = config.presence || MultiDbMigration::Config::DEFAULT_DB_CONFIG_PATH
7
+ @dry_run = dry_run
8
+ end
9
+
10
+ def generate_command
11
+ "bundle exec ridgepole #{command_options.join(' ')}"
12
+ end
13
+
14
+ private
15
+
16
+ def command_options
17
+ options = @shard_config.dig(:options) || []
18
+ options << env_option
19
+ options << config_option
20
+ options << dry_run_option
21
+ options << '--apply'
22
+ options.compact
23
+ end
24
+
25
+ def env_option
26
+ return if @env.blank?
27
+
28
+ "--env #{@env}"
29
+ end
30
+
31
+ def config_option
32
+ return if @config.blank?
33
+
34
+ "--config #{@config}"
35
+ end
36
+
37
+ def dry_run_option
38
+ return if @dry_run.blank?
39
+
40
+ "--dry-run"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module MultiDbMigration
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,67 @@
1
+ require 'open3'
2
+ require 'parallel'
3
+
4
+ namespace :multi_db_migration do
5
+ desc 'migrate'
6
+ task :migrate do
7
+ ENV['RAILS_ENV'] ||= "development"
8
+ config = MultiDbMigration::Config.new(config_path)
9
+ parallel_migrate(config, false)
10
+ end
11
+
12
+ desc 'dry run'
13
+ task :dry_run do
14
+ ENV['RAILS_ENV'] ||= "development"
15
+ config = MultiDbMigration::Config.new(config_path)
16
+ parallel_migrate(config, true)
17
+ end
18
+
19
+ private
20
+
21
+ def config_path
22
+ @config_path ||= ENV.fetch('MULTI_DB_MIGRATION_CONFIG', MultiDbMigration::Config::DEFAULT_PATH)
23
+ end
24
+
25
+ def parallel_migrate(config, dry_run)
26
+ shards = config.shards
27
+ parallel_num = config.parallel_num
28
+
29
+ ::Parallel.each(shards, in_threads: parallel_num) do |shard|
30
+ migrate(config, shard, dry_run)
31
+ end
32
+ end
33
+
34
+ def migrate(config, shard, dry_run)
35
+ shard_config = config.shard_config(shard)
36
+
37
+ client = MultiDbMigration::RidgepoleClient.new(
38
+ env: ENV['RAILS_ENV'],
39
+ config: config.db_config,
40
+ file: shard_config[:file],
41
+ spec_name: shard_config[:spec_name],
42
+ dry_run: dry_run,
43
+ ignore_tables: shard_config[:ignore_tables],
44
+ skip_drop_table: shard_config[:skip_drop_table],
45
+ mysql_change_table_comment: shard_config[:mysql_change_table_comment],
46
+ )
47
+
48
+ command = client.generate_command
49
+ execute_command(shard, command)
50
+ end
51
+
52
+ def execute_command(shard, command)
53
+ puts "[#{shard}] #{command}"
54
+
55
+ is_success = Open3.popen2e(command) do |stdin, stdout_and_stderr, wait_thr|
56
+ stdin.close
57
+
58
+ stdout_and_stderr.each_line do |line|
59
+ puts "[#{shard}] #{line}"
60
+ end
61
+
62
+ wait_thr.value.success?
63
+ end
64
+
65
+ exit(1) unless is_success
66
+ end
67
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/multi_db_migration/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "multi_db_migration"
5
+ spec.version = MultiDbMigration::VERSION
6
+ spec.authors = ["Takahiro Ooishi"]
7
+ spec.email = ["taka0125@gmil.com"]
8
+
9
+ spec.summary = %q{Multiple database migration use ridgepole}
10
+ spec.description = %q{Multiple database migration use ridgepole}
11
+ spec.homepage = "https://github.com/taka0125/multi_db_migration"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.3")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/taka0125/multi_db_migration"
16
+ spec.metadata["changelog_uri"] = "https://github.com/taka0125/multi_db_migration/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "ridgepole", ">= 0.8"
28
+ spec.add_dependency "activesupport", ">= 5.0"
29
+ spec.add_dependency "parallel"
30
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_db_migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Ooishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ridgepole
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: parallel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Multiple database migration use ridgepole
56
+ email:
57
+ - taka0125@gmil.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/multi_db_migration.rb
71
+ - lib/multi_db_migration/config.rb
72
+ - lib/multi_db_migration/railtie.rb
73
+ - lib/multi_db_migration/ridgepole_client.rb
74
+ - lib/multi_db_migration/version.rb
75
+ - lib/tasks/multi_db_migration.rake
76
+ - multi_db_migration.gemspec
77
+ homepage: https://github.com/taka0125/multi_db_migration
78
+ licenses: []
79
+ metadata:
80
+ homepage_uri: https://github.com/taka0125/multi_db_migration
81
+ source_code_uri: https://github.com/taka0125/multi_db_migration
82
+ changelog_uri: https://github.com/taka0125/multi_db_migration/CHANGELOG.md
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.6.3
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.1.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Multiple database migration use ridgepole
102
+ test_files: []