Sha256: fddf102ad5314bd1ab7755b54ba60d30fb73881deea0725a50cd1f3d5025e0e7

Contents?: true

Size: 1.74 KB

Versions: 11

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require "shellwords"
require "open3"
require_relative "database"

module Hanami
  module CLI
    module Commands
      module App
        module DB
          module Utils
            # @api private
            class Postgres < Database
              # @api private
              def create_command
                existing_stdout, status = Open3.capture2(cli_env_vars, "psql -t -c '\\l #{escaped_name}'")

                return true if status.success? && existing_stdout.include?(escaped_name)

                system(cli_env_vars, "createdb #{escaped_name}")
              end

              # @api private
              def drop_command
                system(cli_env_vars, "dropdb #{escaped_name}")
              end

              # @api private
              def dump_command
                system(cli_env_vars, "pg_dump --schema-only --no-owner #{escaped_name} > #{dump_file}")
              end

              # @api private
              def load_command
                raise "Not Implemented Yet"
              end

              # @api private
              def escaped_name
                Shellwords.escape(name)
              end

              # @api private
              def cli_env_vars
                @cli_env_vars ||= {}.tap do |vars|
                  vars["PGHOST"] = config.host.to_s
                  vars["PGPORT"] = config.port.to_s if config.port
                  vars["PGUSER"] = config.user.to_s if config.user
                  vars["PGPASSWORD"] = config.pass.to_s if config.pass
                end
              end

              # @api private
              def dump_file
                "#{root_path}/db/structure.sql"
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hanami-cli-2.1.1 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0.rc3 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0.rc2 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0.rc1 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0.beta2 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.1.0.beta1 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.0.3 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.0.2 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.0.1 lib/hanami/cli/commands/app/db/utils/postgres.rb
hanami-cli-2.0.0 lib/hanami/cli/commands/app/db/utils/postgres.rb