Module: HeartSeed::DbSeed

Defined in:
lib/heart_seed/db_seed.rb

Constant Summary collapse

BULK =
"bulk"
ACTIVE_RECORD =
"active_record"
UPDATE =
"update"

Class Method Summary collapse

Class Method Details

.bulk_insert(file_path: nil, model_class: nil, validate: true) ⇒ Object

delete all records and bulk insert from seed yaml

Parameters:

  • file_path (String) (defaults to: nil)
  • model_class (Class) (defaults to: nil)

    require. extends ActiveRecord::Base

  • validate (Boolean) (defaults to: true)

    run ActiveRecord’s validation. default: true



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/heart_seed/db_seed.rb', line 12

def self.bulk_insert(file_path: nil, model_class: nil, validate: true)
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
  models = fixtures.each_with_object([]) do |fixture, response|
    response << model_class.new(fixture)
    response
  end

  model_class.transaction do
    model_class.delete_all
    model_class.import(models, validate: validate)
  end
end

.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: , catalogs: , mode: , validate: true) ⇒ Object

import all seed yaml to table

Parameters:

  • seed_dir (String) (defaults to: HeartSeed::Helper.seed_dir)
  • tables (Array<String>, String) (defaults to: )

    table names array or comma separated table names. if empty, import all seed yaml. if not empty, import only these tables.

  • catalogs (Array<String>, String) (defaults to: )

    catalogs names array or comma separated catalog names. if empty, import all seed yaml. if not empty, import only these tables in catalogs.

  • insert_mode (String)

    const ACTIVE_RECORD or UPDATE other string. if ACTIVE_RECORD, import with ActiveRecord. (delete_all and create!) if UPDATE, import with ActiveRecord. (if exists same record, update!) other, using bulk insert. (delete_all and BULK INSERT)

  • validate (Boolean) (defaults to: true)

    run ActiveRecord’s validation. default: true



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/heart_seed/db_seed.rb', line 74

def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], mode: ENV["MODE"], validate: true)
  mode ||= BULK
  target_table_names = parse_target_table_names(tables: tables, catalogs: catalogs)

  raise "require TABLES or CATALOGS if production" if HeartSeed::Helper.production? && target_table_names.empty?

  ActiveRecord::Migration.verbose = true

  if target_table_names.empty?
    # seed all tables
    Dir.glob(File.join(seed_dir, "*.yml")) do |file_path|
      table_name = File.basename(file_path, '.*')

      ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
        insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
        ActiveRecord::Migration.say("[INFO] success", true)
      end
    end

  else
    # seed specified tables (follow the order)
    target_table_names.each do |table_name|
      file_path = File.join(seed_dir, "#{table_name}.yml")

      unless File.exist?(file_path)
        ActiveRecord::Migration.say("[WARN] #{file_path} is not exists")
        next
      end

      ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
        insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
        ActiveRecord::Migration.say("[INFO] success", true)
      end
    end
  end
end

.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: , catalogs: , mode: ENV["MODE"] || BULK, shard_names: [], validate: true) ⇒ Object

import all seed yaml to table with specified shards

Parameters:

  • seed_dir (String) (defaults to: HeartSeed::Helper.seed_dir)
  • tables (Array<String>, String) (defaults to: )

    table names array or comma separated table names. if empty, import all seed yaml. if not empty, import only these tables.

  • catalogs (Array<String>, String) (defaults to: )

    catalogs names array or comma separated catalog names. if empty, import all seed yaml. if not empty, import only these tables in catalogs.

  • insert_mode (String)

    const ACTIVE_RECORD or UPDATE other string. if ACTIVE_RECORD, import with ActiveRecord. (delete_all and create!) if UPDATE, import with ActiveRecord. (if exists same record, update!) other, using bulk insert. (delete_all and BULK INSERT)

  • shard_names (Array<String>) (defaults to: [])
  • validate (Boolean) (defaults to: true)

    run ActiveRecord’s validation. default: true



126
127
128
129
130
131
132
133
134
# File 'lib/heart_seed/db_seed.rb', line 126

def self.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"],
                                mode: ENV["MODE"] || BULK, shard_names: [], validate: true)
  shard_names.each do |shard_name|
    ActiveRecord::Migration.say_with_time("import to shard: #{shard_name}") do
      ActiveRecord::Base.establish_connection(shard_name.to_sym)
      import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs, mode: mode, validate: validate)
    end
  end
end

.insert(file_path: nil, model_class: nil, validate: true) ⇒ Object

delete all records and insert from seed yaml

Parameters:

  • file_path (String) (defaults to: nil)
  • model_class (Class) (defaults to: nil)

    require. extends ActiveRecord::Base

  • validate (Boolean) (defaults to: true)

    run ActiveRecord’s validation. default: true



30
31
32
33
34
35
36
37
38
# File 'lib/heart_seed/db_seed.rb', line 30

def self.insert(file_path: nil, model_class: nil, validate: true)
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
  model_class.transaction do
    model_class.delete_all
    fixtures.each do |fixture|
      model_class.new(fixture).save!(validate: validate)
    end
  end
end

.insert_or_update(file_path: nil, model_class: nil, validate: true) ⇒ Object

insert records. if same record exists, updated

Parameters:

  • file_path (String) (defaults to: nil)
  • model_class (Class) (defaults to: nil)

    require. extends ActiveRecord::Base

  • validate (Boolean) (defaults to: true)

    run ActiveRecord’s validation. default: true



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/heart_seed/db_seed.rb', line 45

def self.insert_or_update(file_path: nil, model_class: nil, validate: true)
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
  model_class.transaction do
    fixtures.each do |fixture|
      model = model_class.find_by(id: fixture["id"])
      if model
        model.attributes = fixture
        model.save!(validate: validate)
      else
        model_class.new(fixture).save!(validate: validate)
      end
    end
  end
end

.parse_arg_catalogs(catalogs) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/heart_seed/db_seed.rb', line 153

def self.parse_arg_catalogs(catalogs)
  array_catalogs = parse_string_or_array_arg(catalogs)
  return [] if array_catalogs.empty?

  tables = []
  array_catalogs.each do |catalog|
    tables += HeartSeed::Helper.catalog_tables(catalog)
  end
  tables.compact
end

.parse_string_or_array_arg(tables) ⇒ Object



146
147
148
149
150
151
# File 'lib/heart_seed/db_seed.rb', line 146

def self.parse_string_or_array_arg(tables)
  return [] unless tables
  return tables if tables.class == Array

  tables.class == String ? tables.split(",") : []
end