Sha256: d62932b9103e0d5cf6c06ed0d89b0a399c53bf64a2dee57b1eb7bb5f87ce971f

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require 'database_flusher/active_record/adapters/abstract_adapter'
require 'database_flusher/active_record/adapters/mysql2_adapter'
require 'database_flusher/active_record/adapters/postgresql_adapter'
require 'database_flusher/active_record/adapters/sqlite_adapter'

module DatabaseFlusher
  module ActiveRecord
    class DeletionStrategy
      attr_reader :tables, :adapter

      class Subscriber
        def initialize(strategy)
          @strategy = strategy
        end

        def call(_, _, _, _, payload)
          sql = payload[:sql]
          match = sql.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
          return unless match
          table = match[1]
          if table
            @strategy.tables << table
          end
        end
      end

      def initialize
        @tables = Set.new
      end

      def start
        @subscriber ||= ActiveSupport::Notifications.subscribe(
          'sql.active_record',
          Subscriber.new(self)
        )
        connection = ::ActiveRecord::Base.connection
        @adapter = DatabaseFlusher::ActiveRecord.
          const_get("#{connection.adapter_name}Adapter").
          new(connection)
      end

      def stop
        if @subscriber
          ActiveSupport::Notifications.unsubscribe(@subscriber)
          @subscriber = nil
        end
      end

      def clean
        return if tables.empty?

        # puts "Cleaning #{tables.inspect}"
        adapter.delete(*tables)

        tables.clear
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
database_flusher-0.2.3 lib/database_flusher/active_record/deletion_strategy.rb
database_flusher-0.2.2 lib/database_flusher/active_record/deletion_strategy.rb
database_flusher-0.2.1 lib/database_flusher/active_record/deletion_strategy.rb