Sha256: 31e6c3d20a3362cae5f06c5763825ee16daf6fc630ca3a64e9fc70d9a38e99e3

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 Bytes

Contents

require 'yaml'
require 'shellwords'

module SlonikMigration
  class Command
    def initialize
      config_file = ENV['CONFIG_FILE'] || 'config/slonik.yml'
      env = ENV['RAILS_ENV'] || 'development'
      @config = YAML.load(ERB.new(IO.read(config_file)).result)[env].deep_symbolize_keys
    end

    def execute(sql, options = {})
      command = build(sql, options)
      puts command if ENV['VERBOSE']
      system command
    end

    private

    def build(sql, target: nil, name: nil, owner: nil)
      sql << %Q|; ALTER #{target} "#{name}" OWNER TO #{@config[:owner]}| if target && name && @config[:owner]
      @config[:command].gsub(%r{\$SQL}, Shellwords.escape(sql))
                       .gsub(%r{\$(\w+)}) { replace($1.to_sym) }
    end

    def replace(key)
      if (var = @config.dig(:variables, key))
        Shellwords.escape(var.to_s)
      else
        "$#{key}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slonik_migration-1.0.0 lib/slonik_migration/command.rb