Sha256: 9ef21ea26197d1842ec1c23ee884d74906dba64db54bc3a33729f2dc0c2fda59

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

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

module Lhm
  module Cleanup
    class 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, @retry_config)
        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

1 entries across 1 versions & 1 rubygems

Version Path
lhm-shopify-3.5.0 lib/lhm/cleanup/current.rb