master_data_tool 0.11.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: c8ebd8a88c9055d0c745c87114670f1ee0d7e6d898883e9472c54ca37ea3a22c
4
- data.tar.gz: 5b3cdba7ee2a9f3690087ed90abf74d3838543ea8d268979ec789f28dd060399
3
+ metadata.gz: 69795950505c4ab8feeb32605e77d588da3922793d21e2eeb59995c6a3ac2238
4
+ data.tar.gz: dd153019b32a9808bc5c8da0f6dba8add7ffcb753f85940882efce22a2aac3b5
5
5
  SHA512:
6
- metadata.gz: 94d4272694cb2fd10c6fec969ca3b8b877d9f40173586f0300bcf97f1ad54d7ef3cd240b0c96e7cfd33627560e841ae1f336acc58fd188ac323b39d44428c7d9
7
- data.tar.gz: bdae562479f4e795e13f3140add0c58046669ad6a69164b8d409954ae095b99574e1ad6d951585d82b491d482862219c6bc30acd7415cec42c7ea8a81b6a1aba
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|
@@ -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)
@@ -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.11.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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: master_data_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi