db_transformer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 586b314366d3225be3d1b043e95690d5771b586f6cd565893812a4fdaeb3164b
4
+ data.tar.gz: 0c567d7b207a927691342db8b23c2822a2feab8837f08a141cb42d2685bcd9b2
5
+ SHA512:
6
+ metadata.gz: 2e2647356f6af94d45ab0938812f81370614cd76f7932a56ba8d8ca9742a73b925f119ef379987b3761982d75f52ea0ec8204d2b19c44cafbd3891699b3c7d4b
7
+ data.tar.gz: 5b3f8e6b66557ab424655c05771b6cb27402bef350d5182855b09228ce16e1ae4842abfaed31cf547199ed9fb56520af9a91df7b52e46f63e28b2954e4412de1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ichikawa@1060ki.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in db_transformer.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ db_transformer (0.1.0)
5
+ faker
6
+ mysql2 (~> 0.5.0)
7
+ sequel (~> 5.56)
8
+ thor (~> 1.2)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ concurrent-ruby (1.1.9)
14
+ diff-lcs (1.5.0)
15
+ faker (2.17.0)
16
+ i18n (>= 1.6, < 2)
17
+ i18n (1.8.11)
18
+ concurrent-ruby (~> 1.0)
19
+ mysql2 (0.5.4)
20
+ rake (13.0.6)
21
+ rspec (3.11.0)
22
+ rspec-core (~> 3.11.0)
23
+ rspec-expectations (~> 3.11.0)
24
+ rspec-mocks (~> 3.11.0)
25
+ rspec-core (3.11.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-expectations (3.11.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-mocks (3.11.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-support (3.11.0)
34
+ sequel (5.56.0)
35
+ thor (1.2.1)
36
+
37
+ PLATFORMS
38
+ x86_64-darwin-19
39
+
40
+ DEPENDENCIES
41
+ db_transformer!
42
+ rake (~> 13.0)
43
+ rspec (~> 3.0)
44
+
45
+ BUNDLED WITH
46
+ 2.2.28
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DbTransformer
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/db_transformer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'db_transformer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install db_transformer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/db_transformer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/db_transformer/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the DbTransformer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/db_transformer/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'db_transformer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/db_transformer/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'db_transformer'
7
+ spec.version = DbTransformer::VERSION
8
+ spec.authors = ['Tomoki Ichikawa']
9
+ spec.email = ['ichikawa@1060ki.com']
10
+
11
+ spec.summary = ''
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/1060ki/db_transformer'
14
+ spec.required_ruby_version = '>= 2.7.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/1060ki/db_transformer'
18
+ # spec.metadata["changelog_uri"] = ""
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency 'faker'
33
+ spec.add_dependency 'mysql2', "~> 0.5.0"
34
+ spec.add_dependency 'sequel', '~> 5.56'
35
+ spec.add_dependency 'thor', '~> 1.2'
36
+
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'db_transformer'
4
+
5
+ DbTransformer::CLI.start
@@ -0,0 +1,14 @@
1
+ require 'thor'
2
+
3
+ module DbTransformer
4
+ class CLI < Thor
5
+ desc 'apply', 'Copy data from source to destination and transform the destination data'
6
+
7
+ option :config
8
+ def apply
9
+ config = YAML.load(File.read(options[:config]))
10
+
11
+ Synchronizer.new(config).execute!
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module DbTransformer
2
+ module Rules
3
+ class Rename
4
+ class << self
5
+ def apply(mode, row, column, options)
6
+ case mode
7
+ when 'template'
8
+ template(row, column, options['template'])
9
+ end
10
+ end
11
+
12
+ # @param [Hash] row
13
+ # @param [String] column
14
+ # @param [String] template
15
+ def template(row, column, template)
16
+ row.tap do |t|
17
+ t[column.to_sym] = template % row
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,79 @@
1
+ module DbTransformer
2
+ class Synchronizer
3
+ LIMIT = 1000
4
+
5
+ def initialize(settings)
6
+ @settings = settings
7
+ end
8
+
9
+ def execute!
10
+ destination_database_client.run('SET foreign_key_checks = 0;')
11
+
12
+ tables.each do |table_name|
13
+ logger.info("Start copying `#{table_name}`")
14
+
15
+ if @settings.dig('destination', 'options', 'force_replace')
16
+ destination_database_client.run("DROP TABLE #{table_name}")
17
+ end
18
+
19
+ create_table_query = source_database_client.fetch('SHOW CREATE TABLE ?', table_name).first[:'Create Table']
20
+ create_table_query = create_table_query.gsub('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')
21
+ destination_database_client.run(create_table_query)
22
+
23
+ offset = 0
24
+ loop do
25
+ data = source_database_client[table_name].limit(LIMIT, offset)
26
+
27
+ table = destination_database_client[table_name]
28
+ data.each { |row| table.insert(Transform.execute!(table_name, row, rules)) }
29
+
30
+ break if data.none?
31
+
32
+ offset += LIMIT
33
+ end
34
+
35
+ logger.info("Copied `#{table_name}`")
36
+ end
37
+
38
+ destination_database_client.run('SET foreign_key_checks = 1;')
39
+ rescue Sequel::DatabaseConnectionError => e
40
+ if e.message.include?('Unknown database')
41
+ destination_database_client_without_db.run("CREATE DATABASE `#{@settings['destination']['database']}`")
42
+
43
+ retry
44
+ end
45
+
46
+ raise
47
+ end
48
+
49
+ private
50
+
51
+ def logger
52
+ @logger ||= Logger.new(STDOUT)
53
+ end
54
+
55
+ def tables
56
+ source_database_client.tables
57
+ end
58
+
59
+ def source_database_client
60
+ @source_database_client ||= Sequel.mysql2(@settings['source'])
61
+ end
62
+
63
+ def destination_database_client
64
+ @destination_database_client ||= Sequel.mysql2(destination_database_settings)
65
+ end
66
+
67
+ def destination_database_client_without_db
68
+ @destination_database_client_without_db ||= Sequel.mysql2(destination_database_settings.reject { |k, _| k == 'database' })
69
+ end
70
+
71
+ def destination_database_settings
72
+ @destination_database_settings ||= @settings['destination'].reject { |k, _| k == 'options' }
73
+ end
74
+
75
+ def rules
76
+ @settings['rules']
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,32 @@
1
+ module DbTransformer
2
+ class Transform
3
+ def self.execute!(*args)
4
+ new(*args).execute!
5
+ end
6
+
7
+ def initialize(table_name, row, rules)
8
+ @table_name = table_name
9
+ @row = row
10
+ @rules = rules
11
+ end
12
+
13
+ def execute!
14
+ return @row unless match_rules?
15
+
16
+ applicable_rules.inject(@row) do |row, rule|
17
+ rule_model = Rules.const_get(rule['type'].split('_').map(&:capitalize).join, false)
18
+ rule_model.apply(rule['mode'], row, rule['column'], rule)
19
+ end
20
+ end
21
+
22
+ def match_rules?
23
+ applicable_rules.any?
24
+ end
25
+
26
+ def applicable_rules
27
+ @applicable_rules ||= @rules.select do |rule|
28
+ @table_name.to_s == rule['table']
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DbTransformer
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sequel'
4
+ require 'yaml'
5
+ require 'logger'
6
+
7
+ require_relative 'db_transformer/version'
8
+
9
+ require_relative 'db_transformer/cli'
10
+ require_relative 'db_transformer/synchronizer'
11
+ require_relative 'db_transformer/transform'
12
+ require_relative 'db_transformer/rules/rename'
13
+
14
+ module DbTransformer
15
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_transformer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomoki Ichikawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faker
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.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: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sequel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.56'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.56'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ description: ''
70
+ email:
71
+ - ichikawa@1060ki.com
72
+ executables:
73
+ - db_transformer
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".rspec"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - db_transformer.gemspec
86
+ - exe/db_transformer
87
+ - lib/db_transformer.rb
88
+ - lib/db_transformer/cli.rb
89
+ - lib/db_transformer/rules/rename.rb
90
+ - lib/db_transformer/synchronizer.rb
91
+ - lib/db_transformer/transform.rb
92
+ - lib/db_transformer/version.rb
93
+ homepage: https://github.com/1060ki/db_transformer
94
+ licenses: []
95
+ metadata:
96
+ homepage_uri: https://github.com/1060ki/db_transformer
97
+ source_code_uri: https://github.com/1060ki/db_transformer
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.7.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.1.4
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: ''
117
+ test_files: []