Sha256: 59f6dae6f73e88713d85f68bd8cf2a5ba7d18b2652053b6a69e6eb7b2d026bae

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# auto_register: false

require 'dry/transaction'

require 'pg_export/import'
require 'pg_export/container'

class PgExport
  module Transactions
    class ImportDumpInteractively
      include Dry::Transaction(container: PgExport::Container)
      include Import[
        'adapters.bash_adapter',
        'repositories.ftp_dump_repository',
        'repositories.ftp_dump_file_repository',
        'ui_input'
      ]

      step :open_ftp_connection, with: 'operations.open_ftp_connection'
      step :fetch_dumps_from_ftp
      step :select_dump
      step :download_dump_from_ftp
      step :close_ftp_connection
      step :decrypt_dump, with: 'operations.decrypt_dump'
      step :select_database
      step :restore

      private

      def fetch_dumps_from_ftp(database_name:, ftp_adapter:)
        dumps = ftp_dump_repository.all(database_name: database_name, ftp_adapter: ftp_adapter)
        return Failure(message: 'No dumps') if dumps.none?

        Success(ftp_adapter: ftp_adapter, dumps: dumps)
      end

      def select_dump(dumps:, ftp_adapter:)
        dump = ui_input.select_dump(dumps)
        Success(dump: dump, ftp_adapter: ftp_adapter)
      end

      def download_dump_from_ftp(dump:, ftp_adapter:)
        dump.file = ftp_dump_file_repository.by_name(name: dump.name, ftp_adapter: ftp_adapter)
        Success(dump: dump, ftp_adapter: ftp_adapter)
      end

      def close_ftp_connection(dump:, ftp_adapter:)
        Thread.new { ftp_adapter.close_ftp }
        Success(dump: dump)
      end

      def select_database(dump:)
        name = ui_input.enter_database_name(dump.database)
        Success(dump: dump, database: name)
      end

      def restore(dump:, database:)
        bash_adapter.pg_restore(dump.file, database)
        Success({})
      rescue bash_adapter.class::PgRestoreError => e
        Failure(message: e.to_s)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pg_export-0.7.7 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.6 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.5 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.4 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.3 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.2 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.1 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb
pg_export-0.7.0 lib/pg_export/lib/pg_export/transactions/import_dump_interactively.rb