Class: Lexicon::Common::Production::FileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/lexicon/common/production/file_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell:, database_url:) ⇒ FileLoader

Returns a new instance of FileLoader.

Parameters:



9
10
11
12
# File 'lib/lexicon/common/production/file_loader.rb', line 9

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

Instance Method Details

#load_archive(archive) ⇒ Boolean

Parameters:

  • archive (Pathname)

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/lexicon/common/production/file_loader.rb', line 28

def load_archive(archive)
  shell.execute <<~BASH
    cat '#{archive}' | gzip -d | psql '#{database_url}'
  BASH

  true
end

#load_file(data_file) ⇒ Boolean

Parameters:

  • data_file (Pathname)

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/lexicon/common/production/file_loader.rb', line 16

def load_file(data_file)
  if data_file.basename.to_s =~ /\.sql\z/
    load_sql(data_file)
  elsif data_file.basename.to_s =~ /\.sql\.gz\z/
    load_archive(data_file)
  else
    raise StandardError.new("Unknown file type: #{data_file.basename}")
  end
end

#load_sql(file) ⇒ Boolean

Parameters:

  • file (Pathname)

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/lexicon/common/production/file_loader.rb', line 38

def load_sql(file)
  shell.execute <<~BASH
    echo psql '#{database_url}' < '#{file}'
  BASH

  true
end