lib/hanami/model/migrator/postgres_adapter.rb in hanami-model-1.3.2 vs lib/hanami/model/migrator/postgres_adapter.rb in hanami-model-1.3.3

- old
+ new

@@ -1,106 +1,108 @@ +# frozen_string_literal: true + +require "hanami/utils/blank" + module Hanami module Model class Migrator # PostgreSQL adapter # # @since 0.4.0 # @api private class PostgresAdapter < Adapter # @since 0.4.0 # @api private - HOST = 'PGHOST'.freeze + HOST = "PGHOST" # @since 0.4.0 # @api private - PORT = 'PGPORT'.freeze + PORT = "PGPORT" # @since 0.4.0 # @api private - USER = 'PGUSER'.freeze + USER = "PGUSER" # @since 0.4.0 # @api private - PASSWORD = 'PGPASSWORD'.freeze + PASSWORD = "PGPASSWORD" # @since 1.0.0 # @api private - DB_CREATION_ERROR = 'createdb: database creation failed. If the database exists, ' \ - 'then its console may be open. See this issue for more details: ' \ - 'https://github.com/hanami/model/issues/250'.freeze + DB_CREATION_ERROR = "createdb: database creation failed. If the database exists, " \ + "then its console may be open. See this issue for more details: " \ + "https://github.com/hanami/model/issues/250" # @since 0.4.0 # @api private def create - set_environment_variables - - call_db_command('createdb') + call_db_command("createdb") end # @since 0.4.0 # @api private def drop - set_environment_variables - - call_db_command('dropdb') + call_db_command("dropdb") end # @since 0.4.0 # @api private def dump - set_environment_variables dump_structure dump_migrations_data end # @since 0.4.0 # @api private def load - set_environment_variables load_structure end private - # @since 0.4.0 + # @since 1.3.3 # @api private - def set_environment_variables - ENV[HOST] = host unless host.nil? - ENV[PORT] = port.to_s unless port.nil? - ENV[PASSWORD] = password unless password.nil? - ENV[USER] = username unless username.nil? + def environment_variables + {}.tap do |env| + env[HOST] = host unless host.nil? + env[PORT] = port.to_s unless port.nil? + env[PASSWORD] = password unless password.nil? + env[USER] = username unless username.nil? + end end # @since 0.4.0 # @api private def dump_structure - execute "pg_dump -s -x -O -T #{migrations_table} -f #{escape(schema)} #{database}" + execute "pg_dump -s -x -O -T #{migrations_table} -f #{escape(schema)} #{database}", env: environment_variables end # @since 0.4.0 # @api private def load_structure - execute "psql -X -q -f #{escape(schema)} #{database}" if schema.exist? + return unless schema.exist? + + execute "psql -X -q -f #{escape(schema)} #{database}", env: environment_variables end # @since 0.4.0 # @api private def dump_migrations_data error = ->(err) { raise MigrationError.new(err) unless err =~ /no matching tables/i } - execute "pg_dump -t #{migrations_table} #{database} >> #{escape(schema)}", error: error + execute "pg_dump -t #{migrations_table} #{database} >> #{escape(schema)}", error: error, env: environment_variables end # @since 0.5.1 # @api private def call_db_command(command) - require 'open3' + require "open3" begin - Open3.popen3(command, database) do |_stdin, _stdout, stderr, wait_thr| + Open3.popen3(environment_variables, command, database) do |_stdin, _stdout, stderr, wait_thr| raise MigrationError.new(modified_message(stderr.read)) unless wait_thr.value.success? # wait_thr.value is the exit status end - rescue SystemCallError => e - raise MigrationError.new(modified_message(e.message)) + rescue SystemCallError => exception + raise MigrationError.new(modified_message(exception.message)) end end # @since 1.1.0 # @api private