Sha256: 6b134273d98f118ed23d50087e0e550a94ae5de0528b02312fc4978173531d5c

Contents?: true

Size: 2 KB

Versions: 18

Compression:

Stored size: 2 KB

Contents

require 'lhm/timestamp'
require 'lhm/sql_retry'

module Lhm
  module Cleanup
    class Current

      LOG_PREFIX = "Current"

      def initialize(run, origin_table_name, connection, options={})
        @run = run
        @table_name = TableName.new(origin_table_name)
        @connection = connection
        @ddls = []
        @retry_config = options[:retriable] || {}
      end

      attr_reader :run, :connection, :ddls

      def execute
        build_statements_for_drop_lhm_triggers_for_origin
        build_statements_for_rename_lhmn_tables_for_origin
        if run
          execute_ddls
        else
          report_ddls
        end
      end

      private

      def build_statements_for_drop_lhm_triggers_for_origin
        lhm_triggers_for_origin.each do |trigger|
          @ddls << "drop trigger if exists #{trigger}"
        end
      end

      def lhm_triggers_for_origin
        @lhm_triggers_for_origin ||= all_triggers_for_origin.select { |name| name =~ /^lhmt/ }
      end

      def all_triggers_for_origin
        @all_triggers_for_origin ||= connection.select_values("show triggers like '%#{@table_name.original}'").collect do |trigger|
          trigger.respond_to?(:trigger) ? trigger.trigger : trigger
        end
      end

      def build_statements_for_rename_lhmn_tables_for_origin
        lhmn_tables_for_origin.each do |table|
          @ddls << "rename table #{table} to #{@table_name.failed}"
        end
      end

      def lhmn_tables_for_origin
        @lhmn_tables_for_origin ||= connection.select_values("show tables like '#{@table_name.new}'")
      end

      def execute_ddls
        ddls.each do |ddl|
            @connection.execute(ddl, should_retry: true, log_prefix: LOG_PREFIX)
        end
        Lhm.logger.info("Dropped triggers on #{@lhm_triggers_for_origin.join(', ')}")
        Lhm.logger.info("Dropped tables #{@lhm_triggers_for_origin.join(', ')}")
      end

      def report_ddls
        Lhm.logger.info("The following DDLs would be executed: #{ddls}")
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
lhm-shopify-4.4.2 lib/lhm/cleanup/current.rb
lhm-shopify-4.4.1 lib/lhm/cleanup/current.rb
lhm-shopify-4.4.0 lib/lhm/cleanup/current.rb
lhm-shopify-4.3.0 lib/lhm/cleanup/current.rb
lhm-shopify-4.2.3 lib/lhm/cleanup/current.rb
lhm-shopify-4.2.2 lib/lhm/cleanup/current.rb
lhm-shopify-4.2.1 lib/lhm/cleanup/current.rb
lhm-shopify-4.2.0 lib/lhm/cleanup/current.rb
lhm-shopify-4.1.1 lib/lhm/cleanup/current.rb
lhm-shopify-4.1.0 lib/lhm/cleanup/current.rb
lhm-shopify-4.0.0 lib/lhm/cleanup/current.rb
lhm-teak-3.6.4 lib/lhm/cleanup/current.rb
lhm-teak-3.6.3 lib/lhm/cleanup/current.rb
lhm-teak-3.6.2 lib/lhm/cleanup/current.rb
lhm-teak-3.6.1 lib/lhm/cleanup/current.rb
lhm-teak-3.6.0 lib/lhm/cleanup/current.rb
lhm-shopify-3.5.5 lib/lhm/cleanup/current.rb
lhm-shopify-3.5.4 lib/lhm/cleanup/current.rb