Sha256: 56bb7c3862f5552be39b710d18e0d8fbc56f369d539400d7bb8364c8ed7662e8

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module ActiveRecordViews
  class ChecksumCache
    def initialize(connection)
      @connection = connection
      init_state_table!
    end

    def init_state_table!
      table_exists = @connection.table_exists?('active_record_views')

      if table_exists && !@connection.column_exists?('active_record_views', 'class_name')
        @connection.transaction :requires_new => true do
          @connection.select_values('SELECT name FROM active_record_views;').each do |view_name|
            @connection.execute "DROP VIEW IF EXISTS #{view_name} CASCADE;"
          end
          @connection.execute 'DROP TABLE active_record_views;'
        end
        table_exists = false
      end

      unless table_exists
        @connection.execute 'CREATE TABLE active_record_views(name text PRIMARY KEY, class_name text NOT NULL UNIQUE, checksum text NOT NULL);'
      end
    end

    def get(name)
      @connection.select_one("SELECT class_name, checksum FROM active_record_views WHERE name = #{@connection.quote name}").try(:symbolize_keys)
    end

    def set(name, data)
      if data
        data.assert_valid_keys :class_name, :checksum
        if @connection.update("UPDATE active_record_views SET class_name = #{@connection.quote data[:class_name]}, checksum = #{@connection.quote data[:checksum]} WHERE name = #{@connection.quote name}") == 0
          @connection.insert "INSERT INTO active_record_views (name, class_name, checksum) VALUES (#{@connection.quote name}, #{@connection.quote data[:class_name]}, #{@connection.quote data[:checksum]})"
        end
      else
        @connection.delete "DELETE FROM active_record_views WHERE name = #{@connection.quote name}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord_views-0.0.6 lib/active_record_views/checksum_cache.rb
activerecord_views-0.0.5 lib/active_record_views/checksum_cache.rb