Class: SchemaSherlock::SchemaCache
- Inherits:
-
Object
- Object
- SchemaSherlock::SchemaCache
- Defined in:
- lib/schema_sherlock/schema_cache.rb
Class Method Summary collapse
-
.cache_stats ⇒ Object
Get cache statistics.
- .clear_cache ⇒ Object
-
.column(table_name, column_name) ⇒ Object
Helper method to get column by name.
- .columns(table_name) ⇒ Object
- .connection ⇒ Object
- .indexes(table_name) ⇒ Object
- .initialize_cache ⇒ Object
- .preload_all_metadata ⇒ Object
- .primary_key(table_name) ⇒ Object
- .table_exists?(table_name) ⇒ Boolean
Class Method Details
.cache_stats ⇒ Object
Get cache statistics
108 109 110 111 112 113 114 115 |
# File 'lib/schema_sherlock/schema_cache.rb', line 108 def cache_stats { table_exists_cache_size: @table_exists_cache&.size || 0, columns_cache_size: @columns_cache&.size || 0, indexes_cache_size: @indexes_cache&.size || 0, primary_keys_cache_size: @primary_keys_cache&.size || 0 } end |
.clear_cache ⇒ Object
11 12 13 14 15 16 |
# File 'lib/schema_sherlock/schema_cache.rb', line 11 def clear_cache @table_exists_cache&.clear @columns_cache&.clear @indexes_cache&.clear @primary_keys_cache&.clear end |
.column(table_name, column_name) ⇒ Object
Helper method to get column by name
100 101 102 103 104 105 |
# File 'lib/schema_sherlock/schema_cache.rb', line 100 def column(table_name, column_name) table_columns = columns(table_name) return nil unless table_columns table_columns.find { |col| col.name == column_name } end |
.columns(table_name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/schema_sherlock/schema_cache.rb', line 57 def columns(table_name) initialize_cache if @columns_cache.nil? # Check cache first return @columns_cache[table_name] if @columns_cache.key?(table_name) # If not in cache, fetch from database and cache result return nil unless table_exists?(table_name) columns = connection.columns(table_name) @columns_cache[table_name] = columns columns end |
.connection ⇒ Object
18 19 20 |
# File 'lib/schema_sherlock/schema_cache.rb', line 18 def connection ActiveRecord::Base.connection end |
.indexes(table_name) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/schema_sherlock/schema_cache.rb', line 71 def indexes(table_name) initialize_cache if @indexes_cache.nil? # Check cache first return @indexes_cache[table_name] if @indexes_cache.key?(table_name) # If not in cache, fetch from database and cache result return [] unless table_exists?(table_name) indexes = connection.indexes(table_name) @indexes_cache[table_name] = indexes indexes end |
.initialize_cache ⇒ Object
4 5 6 7 8 9 |
# File 'lib/schema_sherlock/schema_cache.rb', line 4 def initialize_cache @table_exists_cache = {} @columns_cache = {} @indexes_cache = {} @primary_keys_cache = {} end |
.preload_all_metadata ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/schema_sherlock/schema_cache.rb', line 22 def initialize_cache if @table_exists_cache.nil? # Preload all tables existence all_tables = connection.tables all_tables.each { |table| @table_exists_cache[table] = true } # Preload columns, indexes, and primary keys for all tables all_tables.each do |table| @columns_cache[table] = connection.columns(table) @indexes_cache[table] = connection.indexes(table) @primary_keys_cache[table] = connection.primary_key(table) end # Return stats for debugging { tables_cached: @table_exists_cache.size, columns_cached: @columns_cache.size, indexes_cached: @indexes_cache.size, primary_keys_cached: @primary_keys_cache.size } end |
.primary_key(table_name) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/schema_sherlock/schema_cache.rb', line 85 def primary_key(table_name) initialize_cache if @primary_keys_cache.nil? # Check cache first return @primary_keys_cache[table_name] if @primary_keys_cache.key?(table_name) # If not in cache, fetch from database and cache result return nil unless table_exists?(table_name) primary_key = connection.primary_key(table_name) @primary_keys_cache[table_name] = primary_key primary_key end |
.table_exists?(table_name) ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/schema_sherlock/schema_cache.rb', line 45 def table_exists?(table_name) initialize_cache if @table_exists_cache.nil? # Check cache first return @table_exists_cache[table_name] if @table_exists_cache.key?(table_name) # If not in cache, check database and cache result exists = connection.table_exists?(table_name) @table_exists_cache[table_name] = exists exists end |