Sha256: c6707f034eba13cdb1d941cd5903b3da379e2394c32db625f8191d307d3b9c6c

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

class PgExport
  class Utils
    include Logging

    def initialize(encryptor, decryptor)
      @encryptor, @decryptor = encryptor, decryptor
    end

    def create_dump(database_name)
      dump = PlainDump.new
      Open3.popen3("pg_dump -Fc --file #{dump.path} #{database_name}") do |_, _, err|
        error = err.read
        raise PgDumpError, error unless error.empty?
      end
      logger.info "Create #{dump}"
      dump
    end

    def restore_dump(dump, database_name)
      Open3.popen3("pg_restore -c -d #{database_name} #{dump.path}") do |_, _, err|
        error = err.read
        raise PgRestoreError, error if /FATAL/ =~ error
      end
      logger.info "Restore #{dump}"
      self
    end

    def encrypt(dump)
      enc_dump = EncryptedDump.new
      copy_using(encryptor, from: dump, to: enc_dump)
      logger.info "Create #{enc_dump}"
      enc_dump
    end

    def decrypt(enc_dump)
      dump = PlainDump.new
      copy_using(decryptor, from: enc_dump, to: dump)
      logger.info "Create #{dump}"
      dump
    end

    private

    attr_reader :encryptor, :decryptor

    def copy_using(aes, from:, to:)
      aes.reset
      to.open(:write) do |f|
        from.each_chunk do |chunk|
          f << aes.update(chunk)
        end
        f << aes.final
      end
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg_export-0.4.1 lib/pg_export/services/utils.rb
pg_export-0.4.0 lib/pg_export/services/utils.rb