Class: ActiveRecord::Materialized::RelationCacheWriter Private
- Inherits:
-
Object
- Object
- ActiveRecord::Materialized::RelationCacheWriter
- Extended by:
- T::Sig
- Defined in:
- lib/activerecord/materialized/relation_cache_writer.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Materializes a relation into a cache table via ‘INSERT … SELECT`, with an atomic swap on full refresh.
Instance Method Summary collapse
- #atomic_swap!(relation) ⇒ Object private
- #bootstrap!(relation) ⇒ Object private
-
#initialize(view_class) ⇒ RelationCacheWriter
constructor
private
A new instance of RelationCacheWriter.
- #populate_temp_table!(temp_table, relation) ⇒ Object private
- #refresh_old_table_name ⇒ Object private
- #refresh_temp_table_name ⇒ Object private
- #replace_all!(relation) ⇒ Object private
- #replace_partitions!(relation, key_tuples:, full_partition:) ⇒ Object private
- #swap_tables!(connection, temp_table, old_table) ⇒ Object private
Constructor Details
#initialize(view_class) ⇒ RelationCacheWriter
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of RelationCacheWriter.
13 14 15 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 13 def initialize(view_class) @view_class = view_class end |
Instance Method Details
#atomic_swap!(relation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 52 def atomic_swap!(relation) connection = view_class.connection temp_table = refresh_temp_table_name old_table = refresh_old_table_name populate_temp_table!(temp_table, relation) swap_tables!(connection, temp_table, old_table) view_class.reset_column_information cache_row_count end |
#bootstrap!(relation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 21 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 18 def bootstrap!(relation) CacheTableSchema.ensure_table!(view_class, relation) replace_all!(relation) end |
#populate_temp_table!(temp_table, relation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
65 66 67 68 69 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 65 def populate_temp_table!(temp_table, relation) CacheTableSchema.create_table!(T.cast(view_class, ViewClass), temp_table, relation) temp_model = temporary_model(temp_table) self.class.new(temp_model).replace_all!(relation) end |
#refresh_old_table_name ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
86 87 88 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 86 def refresh_old_table_name "#{view_class.table_name}_old_#{SecureRandom.hex(4)}" end |
#refresh_temp_table_name ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
81 82 83 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 81 def refresh_temp_table_name "#{view_class.table_name}_refresh_#{SecureRandom.hex(4)}" end |
#replace_all!(relation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 27 28 29 30 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 24 def replace_all!(relation) view_class.transaction do view_class.delete_all insert_rows!(relation) end cache_row_count end |
#replace_partitions!(relation, key_tuples:, full_partition:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 39 def replace_partitions!(relation, key_tuples:, full_partition:) view_class.transaction do if full_partition view_class.delete_all else delete_partitions!(key_tuples) end insert_rows!(relation) end cache_row_count end |
#swap_tables!(connection, temp_table, old_table) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
72 73 74 75 76 77 78 |
# File 'lib/activerecord/materialized/relation_cache_writer.rb', line 72 def swap_tables!(connection, temp_table, old_table) connection.transaction do connection.rename_table(view_class.table_name, old_table) if view_class.table_exists? connection.rename_table(temp_table, view_class.table_name) connection.drop_table(old_table, if_exists: true) end end |