Class: Lexicon::Common::Production::DatasourceLoader

Inherits:
Object
  • Object
show all
Includes:
Mixin::SchemaNamer
Defined in:
lib/lexicon/common/production/datasource_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell:, database_factory:, file_loader:, database_url:) ⇒ DatasourceLoader

Returns a new instance of DatasourceLoader.

Parameters:



12
13
14
15
16
17
# File 'lib/lexicon/common/production/datasource_loader.rb', line 12

def initialize(shell:, database_factory:, file_loader:, database_url:)
  @shell = shell
  @database_factory = database_factory
  @file_loader = file_loader
  @database_url = database_url
end

Instance Method Details

#load_package(package, only: nil, without: []) ⇒ Object

Parameters:

  • package (Package::Package)
  • only (Array<String>, nil) (defaults to: nil)
  • without (Array<String>) (defaults to: [])

    If nil, all datasets are loaded. If present, only listed datasets are loaded. Structures are ALWAYS loaded



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lexicon/common/production/datasource_loader.rb', line 25

def load_package(package, only: nil, without: [])
  file_sets = if only.nil?
                package.file_sets.select(&:data_path)
              else
                sets_by_name = package.file_sets.map { |fs| [fs.name, fs] }.to_h

                missing, present = only.map { |name| [name, sets_by_name.fetch(name, nil)] }
                                       .partition { |(_name, value)| value.nil? }

                if missing.any?
                  puts "[ NOK ] Datasources #{missing.map(&:first).join(', ')} don't exist!"
                  return
                end

                present.map(&:second)
                       .select(&:data_path)
              end

  file_sets = file_sets.reject { |fs| without.include?(fs.name) }

  load_structure_files(package.structure_files, schema: version_to_schema(package.version))

  file_sets.map do |fs|
    Thread.new do
      puts "Loading #{fs.name}"
      file_loader.load_file(package.data_path(fs))
      puts '[  OK ] '.green + fs.name.yellow
    end
  end.each(&:join)

  lock_tables(package)
end