Sha256: 1c53c22fbe6c0c7d2ee172ce175b7a5a75e490ec2c52dad20e0423999f0fc8ea

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'pg_export/transactions/evaluator'
require 'pg_export/value_objects/dump_file'
require 'pg_export/adapters/shell_adapter'
require 'pg_export/value_objects/result'

class PgExport
  module Transactions
    class ExportDump
      def initialize(dump_factory:, shell_adapter:, encrypt_dump:, open_connection:, remove_old_dumps:, listeners:)
        @dump_factory = dump_factory
        @shell_adapter = shell_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, :shell_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: shell_adapter.pg_dump(ValueObjects::DumpFile.new, database_name)
        )
        ValueObjects::Success.new(dump: dump)
      rescue Adapters::ShellAdapter::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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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