Sha256: f802f3336e4c07ed935ed5f2695b9a705c32e3e562ea2cc53df41a50db736e66

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

if defined?(ActiveRecord)
  namespace :db do
    desc 'Prints out the database statement timeout'
    task statement_timeout: :environment do
      result = ActiveRecord::Base.connection.execute('SHOW statement_timeout').first
      puts result['statement_timeout']
    end

    namespace :migrate do
      task extend_statement_timeout: :environment do
        rails_version = Gem::Version.new(Rails.version)

        if rails_version < Gem::Version.new('6.1')
          config = ActiveRecord::Base.configurations[Rails.env]
          config['variables'] ||= {}
          config['variables']['statement_timeout'] = ENV.fetch('MIGRATION_STATEMENT_TIMEOUT', 10_000)
        else
          configs = ActiveRecord::Base.configurations.configurations
          old_url_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'primary')
          new_config_hash = old_url_config.configuration_hash.deep_dup
          new_config_hash[:variables] ||= {}
          new_config_hash[:variables][:statement_timeout] = ENV.fetch('MIGRATION_STATEMENT_TIMEOUT', 10_000)
          if old_url_config.respond_to?(:url)
            new_url_config = ActiveRecord::DatabaseConfigurations::UrlConfig.new(
              old_url_config.env_name,
              old_url_config.name,
              old_url_config.url,
              new_config_hash
            )
          else
            new_url_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
              old_url_config.env_name,
              old_url_config.name,
              new_config_hash
            )
          end
          configs.delete(old_url_config)
          configs << new_url_config
        end

        ActiveRecord::Base.establish_connection
      end
    end
  end

  %i(
    db:create
    db:drop
    db:migrate
    db:migrate:down
    db:rollback
  ).each do |task|
    Rake::Task[task].enhance(%i[db:migrate:extend_statement_timeout])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roo_on_rails-2.2.0 lib/roo_on_rails/tasks/db.rake