master_data_tool 0.9.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c1a8cf7753997cee7c6508d745c9eb9152261ea433dcd0ea436bd9684d2b57c
4
- data.tar.gz: ae108bb9da67df2a46e15335cc7930a6568a0e359d599323e24683bc3db761c4
3
+ metadata.gz: 69795950505c4ab8feeb32605e77d588da3922793d21e2eeb59995c6a3ac2238
4
+ data.tar.gz: dd153019b32a9808bc5c8da0f6dba8add7ffcb753f85940882efce22a2aac3b5
5
5
  SHA512:
6
- metadata.gz: be594e338ccd11ec1bb9621452c6ff1b0834c253326b74b89fd0f4e7285b3f040f5948f69cde7eae6028581503bf37f26ba8ce0950e7543184b529f44ec51e55
7
- data.tar.gz: a3ceed39c0d391fb2ec5774371b54f66e25a7d3dc52fff323fd8235edb6e3d750022909782e482fa347ec950b55b2e75722440f3ce613569e7fd134a4b012069
6
+ metadata.gz: 2107f256a35f5a8ad8e9a9cfb8f6b668e9518b185eeb91a42b4aac7b56695841abf1f627a1c182e0afc6f06a441c2b5569c5f9e62985d76f76d89f30c5a37d2d
7
+ data.tar.gz: 587736de3714b372e702e9338a6dfa6e45c44251fcf584b1d0c117a7bca8120cb12a9c97a9635ccaad34e0dd6693e5149253ac20a7201802f9b84f572bc474bd
data/README.md CHANGED
@@ -45,6 +45,7 @@ Or install it yourself as:
45
45
  | --except-verify-tables | [] | 指定したテーブルのバリデーションチェックを行わない |
46
46
  | --skip-no-change | true | CSVファイルに更新がないテーブルをスキップする |
47
47
  | --silent | false | 結果の出力をやめる |
48
+ | --delete-all-ignore-foreign-key | false | 外部キー制約を無視してレコードを消すかどうか |
48
49
 
49
50
  ```bash
50
51
  bundle exec master_data_tool import
@@ -61,7 +62,8 @@ bundle exec thor master_data_tool import \
61
62
  --only-verify-tables="" \
62
63
  --except-verify-tables="" \
63
64
  --skip-no-change=true \
64
- --silent=false
65
+ --silent=false \
66
+ --delete-all-ignore-foreign-key=false
65
67
  ```
66
68
 
67
69
  ### ダンプ
data/exe/master_data_tool CHANGED
@@ -9,35 +9,19 @@ require environment_path
9
9
 
10
10
  module MasterDataTool
11
11
  class CLI < Thor
12
- option :dry_run, default: true, type: :boolean
13
- option :verify, default: true, type: :boolean
14
- option :only_import_tables, default: [], type: :array
15
- option :except_import_tables, default: [], type: :array
16
- option :only_verify_tables, default: [], type: :array
17
- option :except_verify_tables, default: [], type: :array
18
- option :skip_no_change, default: true, type: :boolean
19
- option :silent, default: false, type: :boolean
12
+ option :dry_run, default: nil, type: :boolean
13
+ option :verify, default: nil, type: :boolean
14
+ option :only_import_tables, default: nil, type: :array
15
+ option :except_import_tables, default: nil, type: :array
16
+ option :only_verify_tables, default: nil, type: :array
17
+ option :except_verify_tables, default: nil, type: :array
18
+ option :skip_no_change, default: nil, type: :boolean
19
+ option :silent, default: nil, type: :boolean
20
+ option :delete_all_ignore_foreign_key, default: nil, type: :boolean
20
21
  desc 'import', 'import'
21
22
  def import
22
- dry_run = options[:dry_run]
23
- verify = options[:verify]
24
- only_import_tables = options[:only_import_tables]
25
- except_import_tables = options[:except_import_tables]
26
- only_verify_tables = options[:only_verify_tables]
27
- except_verify_tables = options[:except_verify_tables]
28
- skip_no_change = options[:skip_no_change]
29
- silent = options[:silent]
30
-
31
- executor = MasterDataTool::Import::Executor.new(
32
- dry_run: dry_run,
33
- verify: verify,
34
- only_import_tables: only_import_tables,
35
- except_import_tables: except_import_tables,
36
- only_verify_tables: only_verify_tables,
37
- except_verify_tables: except_verify_tables,
38
- skip_no_change: skip_no_change,
39
- silent: silent
40
- )
23
+ new_options = MasterDataTool.config.default_import_options.with_indifferent_access.merge(options)
24
+ executor = MasterDataTool::Import::Executor.new(**new_options.symbolize_keys)
41
25
  executor.execute
42
26
  end
43
27
 
@@ -7,15 +7,15 @@ module MasterDataTool
7
7
  config_accessor :master_data_dir
8
8
  config_accessor :dump_ignore_tables
9
9
  config_accessor :dump_ignore_columns
10
+ config_accessor :default_import_options
10
11
  config_accessor :logger
11
12
 
12
- def self.default_config
13
- new.tap do |config|
14
- config.master_data_dir = nil # Rails.root.join('db/fixtures')
15
- config.dump_ignore_tables = %w[]
16
- config.dump_ignore_columns = %w[]
17
- config.logger = Logger.new(nil)
18
- end
13
+ def initialize
14
+ self.master_data_dir = nil
15
+ self.dump_ignore_tables = %w[]
16
+ self.dump_ignore_columns = %w[]
17
+ self.default_import_options = {}
18
+ self.logger = Logger.new(nil)
19
19
  end
20
20
  end
21
21
  end
@@ -11,6 +11,7 @@ module MasterDataTool
11
11
  except_verify_tables: [],
12
12
  skip_no_change: true,
13
13
  silent: false,
14
+ delete_all_ignore_foreign_key: false,
14
15
  report_printer: MasterDataTool::Report::DefaultPrinter.new)
15
16
 
16
17
  @dry_run = dry_run
@@ -21,12 +22,15 @@ module MasterDataTool
21
22
  @except_verify_tables = Array(except_verify_tables)
22
23
  @skip_no_change = skip_no_change
23
24
  @silent = silent
25
+ @delete_all_ignore_foreign_key = delete_all_ignore_foreign_key
24
26
  @report_printer = report_printer
25
27
  @report_printer.silent = silent
26
28
  end
27
29
 
28
30
  def execute
29
31
  ApplicationRecord.transaction do
32
+ print_execute_options
33
+
30
34
  master_data_list = build_master_data_list
31
35
 
32
36
  import_all!(master_data_list)
@@ -45,6 +49,14 @@ module MasterDataTool
45
49
 
46
50
  private
47
51
 
52
+ def print_execute_options
53
+ puts "==== execute ===="
54
+ instance_variables.each do |k|
55
+ puts "#{k}: #{instance_variable_get(k)}"
56
+ end
57
+ puts "================="
58
+ end
59
+
48
60
  def build_master_data_list
49
61
  [].tap do |master_data_list|
50
62
  extract_master_data_csv_paths.each do |path|
@@ -57,7 +69,7 @@ module MasterDataTool
57
69
 
58
70
  master_data_list << master_data
59
71
  end
60
- end
72
+ end.sort_by { |m| m.csv_path } # 外部キー制約などがある場合には先に入れておかないといけないデータなどがある。なので、プレフィックスを付けて順序を指定して貰う
61
73
  end
62
74
 
63
75
  def import_all!(master_data_list)
@@ -65,7 +77,7 @@ module MasterDataTool
65
77
  next unless master_data.loaded?
66
78
  next if import_skip_table?(master_data.table_name)
67
79
 
68
- report = master_data.import!(dry_run: @dry_run)
80
+ report = master_data.import!(dry_run: @dry_run, delete_all_ignore_foreign_key: @delete_all_ignore_foreign_key)
69
81
  report.print(@report_printer)
70
82
  end
71
83
  end
@@ -103,14 +103,16 @@ module MasterDataTool
103
103
  @model_klass.table_name
104
104
  end
105
105
 
106
- def import!(dry_run: true)
106
+ def import!(dry_run: true, delete_all_ignore_foreign_key: false)
107
107
  raise MasterDataTool::NotLoadedError unless @loaded
108
108
 
109
109
  MasterDataTool::Report::ImportReport.new(self).tap do |report|
110
110
  return report if dry_run
111
111
  return report unless affected?
112
112
 
113
+ disable_foreign_key_checks if delete_all_ignore_foreign_key
113
114
  @model_klass.delete_all
115
+ enable_foreign_key_checks if delete_all_ignore_foreign_key
114
116
 
115
117
  # マスターデータ間の依存がある場合に投入順制御するのは大変なのでこのタイミングでのバリデーションはしない
116
118
  @model_klass.import(import_records, validate: false, on_duplicate_key_update: @columns, timestamps: true)
@@ -119,7 +121,7 @@ module MasterDataTool
119
121
 
120
122
  def verify!(ignore_fail: false)
121
123
  MasterDataTool::Report::VerifyReport.new(self).tap do |report|
122
- @model_klass.order(id: :asc).all.find_each do |record|
124
+ @model_klass.all.find_each do |record|
123
125
  valid = record.valid?
124
126
  report.append(MasterDataTool::Report::VerifyReport.build_verify_record_report(self, record, valid))
125
127
  next if ignore_fail
@@ -152,5 +154,13 @@ module MasterDataTool
152
154
  end
153
155
  end
154
156
  end
157
+
158
+ def enable_foreign_key_checks
159
+ ApplicationRecord.connection.execute('SET FOREIGN_KEY_CHECKS = 1')
160
+ end
161
+
162
+ def disable_foreign_key_checks
163
+ ApplicationRecord.connection.execute('SET FOREIGN_KEY_CHECKS = 0')
164
+ end
155
165
  end
156
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MasterDataTool
4
- VERSION = "0.9.0"
4
+ VERSION = "0.12.0"
5
5
  end
@@ -17,7 +17,7 @@ module MasterDataTool
17
17
 
18
18
  class << self
19
19
  def config
20
- @config ||= Config.default_config
20
+ @config ||= Config.new
21
21
  end
22
22
 
23
23
  def configure
@@ -25,7 +25,8 @@ module MasterDataTool
25
25
  end
26
26
 
27
27
  def resolve_table_name(csv_path)
28
- csv_path.relative_path_from(config.master_data_dir).to_s.delete_suffix('.csv')
28
+ # 0001_table_nameのように投入順序を制御可能にする
29
+ csv_path.relative_path_from(config.master_data_dir).to_s.gsub(/^\d+_/, '').delete_suffix('.csv')
29
30
  end
30
31
  end
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: master_data_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-19 00:00:00.000000000 Z
11
+ date: 2022-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -205,7 +205,6 @@ files:
205
205
  - lib/master_data_tool/report/verify_report.rb
206
206
  - lib/master_data_tool/version.rb
207
207
  - log/test.log
208
- - master_data_tool.gemspec
209
208
  - scripts/setup.sh
210
209
  - sig/master_data_tool.rbs
211
210
  homepage: https://github.com/taka0125/master_data_tool
@@ -228,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
227
  - !ruby/object:Gem::Version
229
228
  version: '0'
230
229
  requirements: []
231
- rubygems_version: 3.2.22
230
+ rubygems_version: 3.0.3
232
231
  signing_key:
233
232
  specification_version: 4
234
233
  summary: マスタデータの管理ツール
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/master_data_tool/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "master_data_tool"
7
- spec.version = MasterDataTool::VERSION
8
- spec.authors = ["Takahiro Ooishi"]
9
- spec.email = ["taka0125@gmail.com"]
10
-
11
- spec.summary = "マスタデータの管理ツール"
12
- spec.description = "システムが稼働する上で最初から必要なデータ(マスタデータ)を管理するツールです。"
13
- spec.homepage = "https://github.com/taka0125/master_data_tool"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = spec.homepage
18
-
19
- # Specify which files should be added to the gem when it is released.
20
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
- `git ls-files -z`.split("\x0").reject do |f|
23
- (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
- end
25
- end
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
29
-
30
- spec.add_development_dependency 'rspec'
31
- spec.add_development_dependency 'mysql2'
32
- spec.add_development_dependency 'psych', '~> 3.1'
33
- spec.add_development_dependency 'appraisal'
34
- spec.add_development_dependency 'ridgepole'
35
- spec.add_development_dependency 'database_cleaner-active_record'
36
- spec.add_development_dependency 'standalone_activerecord_boot_loader'
37
-
38
- spec.add_dependency 'activerecord', '>= 5.1.7'
39
- spec.add_dependency 'activesupport'
40
- spec.add_dependency 'thor'
41
- spec.add_dependency 'activerecord-import'
42
- end