Class: ActiveRecordViews::ChecksumCache
- Inherits:
-
Object
- Object
- ActiveRecordViews::ChecksumCache
- Defined in:
- lib/active_record_views/checksum_cache.rb
Instance Method Summary collapse
- #get(name) ⇒ Object
- #init_state_table! ⇒ Object
-
#initialize(connection) ⇒ ChecksumCache
constructor
A new instance of ChecksumCache.
- #set(name, data) ⇒ Object
Constructor Details
#initialize(connection) ⇒ ChecksumCache
Returns a new instance of ChecksumCache.
3 4 5 6 |
# File 'lib/active_record_views/checksum_cache.rb', line 3 def initialize(connection) @connection = connection init_state_table! end |
Instance Method Details
#get(name) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/active_record_views/checksum_cache.rb', line 38 def get(name) if data = @connection.select_one("SELECT class_name, checksum, options FROM active_record_views WHERE name = #{@connection.quote name}") data.symbolize_keys! data[:options] = JSON.load(data[:options]).symbolize_keys data end end |
#init_state_table! ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_record_views/checksum_cache.rb', line 8 def init_state_table! table_exists = @connection.data_source_exists?('active_record_views') if table_exists && !@connection.column_exists?('active_record_views', 'class_name') @connection.begin_transaction in_transaction = true @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;' table_exists = false end if table_exists && !@connection.column_exists?('active_record_views', 'options') @connection.execute "ALTER TABLE active_record_views ADD COLUMN options json NOT NULL DEFAULT '{}';" end if table_exists && !@connection.column_exists?('active_record_views', 'refreshed_at') @connection.execute "ALTER TABLE active_record_views ADD COLUMN refreshed_at timestamp;" 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, options json NOT NULL DEFAULT '{}', refreshed_at timestamp);" end ensure @connection.commit_transaction if in_transaction end |
#set(name, data) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_record_views/checksum_cache.rb', line 46 def set(name, data) if data data.assert_valid_keys :class_name, :checksum, :options = data[:options] || {} unless .keys.all? { |key| key.is_a?(Symbol) } raise ArgumentError, 'option keys must be symbols' end = JSON.dump() rows_updated = @connection.update(<<-SQL.squish) UPDATE active_record_views SET class_name = #{@connection.quote data[:class_name]}, checksum = #{@connection.quote data[:checksum]}, options = #{@connection.quote } WHERE name = #{@connection.quote name} ; SQL if rows_updated == 0 @connection.insert <<-SQL.squish INSERT INTO active_record_views ( name, class_name, checksum, options ) VALUES ( #{@connection.quote name}, #{@connection.quote data[:class_name]}, #{@connection.quote data[:checksum]}, #{@connection.quote } ) SQL end else @connection.delete "DELETE FROM active_record_views WHERE name = #{@connection.quote name}" end end |