class DataKeeper::Loader

Public Class Methods

new(dump, file) click to toggle source
# File lib/data_keeper/loader.rb, line 7
def initialize(dump, file)
  @dump = dump
  @file = file
  @psql_version = Terrapin::CommandLine.new('psql', '--version').run
                                       .match(/[0-9]{1,}\.[0-9]{1,}/)
                                       .to_s.to_f
end

Public Instance Methods

load!() click to toggle source
# File lib/data_keeper/loader.rb, line 15
def load!
  if @dump.type == :full
    load_full_database!
  else
    load_partial_database!
  end

  if @dump.on_after_load_block
    @dump.on_after_load_block.call
  end
end

Private Instance Methods

ensure_schema_compatibility!() click to toggle source
# File lib/data_keeper/loader.rb, line 106
def ensure_schema_compatibility!
  cmd = Terrapin::CommandLine.new(
    'psql',
    "#{connection_args} -d :database -c :command",
    environment: psql_env
  )

  if @psql_version >= 11.0
    cmd.run(database: database, host: host, port: port, command: "drop schema if exists public")
  else
    cmd.run(database: database, host: host, port: port, command: "create schema if not exists public")
  end
end
inflate(path) { |schema_path, tables_path, sql_dumps| ... } click to toggle source
# File lib/data_keeper/loader.rb, line 165
def inflate(path)
  Dir.mktmpdir do |dir|
    File.open(path, "rb") do |f|
      Gem::Package.new("").extract_tar_gz(f, dir)

      inflated_files = InflatedFiles.new(@dump, Dir.glob(File.join(dir, "*")))
      raise inflated_files.errors.join(", ") unless inflated_files.valid?

      yield(
        inflated_files.schema_path,
        inflated_files.tables_path,
        inflated_files.sql_dumps
      )
    end
  end
end
load_full_database!() click to toggle source
# File lib/data_keeper/loader.rb, line 29
def load_full_database!
  ensure_schema_compatibility!

  pg_restore = Terrapin::CommandLine.new(
    'pg_restore',
    "#{connection_args} -j 4 --no-owner --dbname #{database} #{@file.path} 2>/dev/null",
    environment: psql_env
  )

  pg_restore.run(
    database: database,
    host: host,
    port: port
  )

  cmd = Terrapin::CommandLine.new(
    'psql',
    "#{connection_args} -d :database -c :sql",
    environment: psql_env
  )

  cmd.run(
    database: database,
    host: host,
    port: port,
    sql: "UPDATE ar_internal_metadata SET value = 'development'"
  )
end
load_partial_database!() click to toggle source
# File lib/data_keeper/loader.rb, line 58
def load_partial_database!
  inflate(@file.path) do |schema_path, tables_path, sql_files|
    ensure_schema_compatibility!

    pg_restore = Terrapin::CommandLine.new(
      'pg_restore',
      "#{connection_args} -j 4 --no-owner --dbname :database #{schema_path} 2>/dev/null",
      environment: psql_env
    )

    pg_restore.run(
      database: database,
      host: host,
      port: port
    )

    pg_restore = Terrapin::CommandLine.new(
      'pg_restore',
      "#{connection_args} --data-only -j 4 --no-owner --disable-triggers --dbname :database #{tables_path} 2>/dev/null",
      environment: psql_env
    )

    pg_restore.run(
      database: database,
      host: host,
      port: port
    )

    sql_files.each do |table, csv_path|
      cmd = Terrapin::CommandLine.new(
        'psql',
        "#{connection_args} -d :database -c :command < :csv_path",
        environment: psql_env
      )

      cmd.run(
        database: database,
        host: host,
        port: port,
        csv_path: csv_path,
        command: "ALTER TABLE #{table} DISABLE TRIGGER all; COPY #{table} FROM stdin DELIMITER ',' CSV HEADER"
      )
    end

    Rake::Task['db:environment:set'].invoke
  end
end