Sha256: d7ab7cbc86e776f37a3470f75cc238829a2629f5362aebd143e19cb5f6d1ea37

Contents?: true

Size: 814 Bytes

Versions: 2

Compression:

Stored size: 814 Bytes

Contents

# frozen_string_literal: true

require 'yaml'
require 'shellwords'

module SlonikMigration
  class Command
    def initialize
      @config = SlonikMigration::Config.load
    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
      command = @config.command.dup
      command.gsub!(%r{\$SQL}) { Shellwords.escape(sql) }
      command.gsub!(%r{\$(\w+)}) { replace($1) }
      command
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slonik_migration-1.2.2 lib/slonik_migration/command.rb
slonik_migration-1.2.1 lib/slonik_migration/command.rb