Sha256: ca9ab35867d05b7d54ccb7b1cf9dce9a565ddb796ea98a764fad981c58b2e984

Contents?: true

Size: 1.51 KB

Versions: 11

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Ridgepole
  class ExternalSqlExecuter
    def initialize(script, logger)
      @script = script
      @logger = logger
    end

    def execute(sql)
      cmd = Shellwords.join([@script, sql, JSON.dump(connection_configuration_hash)])
      @logger.info("Execute #{@script}")
      script_basename = File.basename(@script)

      Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
        stdin.close_write
        files = [stdout, stderr]

        begin
          until files.empty?
            ready = IO.select(files)

            next unless ready

            readable = ready[0]

            readable.each do |f|
              data = f.read_nonblock(1024)
              next if data.nil?

              data.chomp!

              if f == stderr
                @logger.warn("[WARNING] #{script_basename}: #{data}")
              else
                @logger.info("#{script_basename}: #{data}")
              end
            rescue EOFError
              files.delete f
            end
          end
        rescue EOFError
          # nothing to do
        end

        raise "`#{@script}` execution failed" unless wait_thr.value.success?
      end
    end

    private

    def connection_configuration_hash
      if ActiveRecord.gem_version < Gem::Version.new('6.1.0')
        # NOTE: Remove code when stopping support for versions below 6.1
        ActiveRecord::Base.connection_config
      else
        ActiveRecord::Base.connection_db_config.configuration_hash
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ridgepole-1.2.1 lib/ridgepole/external_sql_executer.rb
ridgepole-1.2.0 lib/ridgepole/external_sql_executer.rb
ridgepole-1.1.0 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.7 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.6 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.5 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.4 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.3 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.2 lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.2.beta lib/ridgepole/external_sql_executer.rb
ridgepole-1.0.1 lib/ridgepole/external_sql_executer.rb