Sha256: 0e75b25e7b5a02e01b53050de00592dad40923d45bc624aa74d6d83a1e79d323

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'pg_export/lib/pg_export/transactions/evaluator'
require 'pg_export/lib/pg_export/value_objects/dump_file'
require 'pg_export/lib/pg_export/value_objects/result'

class PgExport
  module Transactions
    class ExportDump
      def initialize(dump_factory:, bash_adapter:, encrypt_dump:, open_connection:, remove_old_dumps:, listeners:)
        @dump_factory = dump_factory
        @bash_adapter = bash_adapter

        @evaluator = Evaluator.new(listeners)
        @evaluator << method(:prepare_params)
        @evaluator << method(:build_dump)
        @evaluator << encrypt_dump
        @evaluator << open_connection
        @evaluator << method(:upload_dump)
        @evaluator << remove_old_dumps
        @evaluator << method(:close_connection)
      end

      def call(input)
        evaluator.call(input)
      end

      private

      attr_reader :evaluator, :dump_factory, :bash_adapter


      def prepare_params(database_name:)
        database_name = database_name.to_s

        return ValueObjects::Failure.new(message: 'Invalid database name') if database_name.empty?

        ValueObjects::Success.new(database_name: database_name)
      end

      def build_dump(database_name:)
        dump = dump_factory.plain(
          database: database_name,
          file: bash_adapter.pg_dump(ValueObjects::DumpFile.new, database_name)
        )
        ValueObjects::Success.new(dump: dump)
      rescue bash_adapter.class::PgDumpError => e
        ValueObjects::Failure.new(message: 'Unable to dump database: ' + e.to_s)
      end

      def upload_dump(dump:, gateway:)
        gateway.persist(dump.file, dump.name)
        ValueObjects::Success.new(dump: dump, gateway: gateway)
      end

      def close_connection(removed_dumps:, gateway:)
        gateway.close
        ValueObjects::Success.new(gateway: gateway)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_export-1.0.0.rc6 lib/pg_export/lib/pg_export/transactions/export_dump.rb