diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2019-03-09 21:23:36 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2019-03-13 00:30:47 +0100 |
commit | 6e3a7d127ee4c4813b8cd4efee2c48ab4110767b (patch) | |
tree | 4cb94d37c61308ec49dc478bd8b839191dcd5484 /activerecord/lib/active_record | |
parent | 49676e40547b8dcbd38aa927cfd7995957504493 (diff) | |
download | rails-6e3a7d127ee4c4813b8cd4efee2c48ab4110767b.tar.gz rails-6e3a7d127ee4c4813b8cd4efee2c48ab4110767b.tar.bz2 rails-6e3a7d127ee4c4813b8cd4efee2c48ab4110767b.zip |
Schema Cache: cache table indexes
Useful to not query for indexes when an application uses schema cache.
Ref https://github.com/rails/rails/pull/35546
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/schema_cache.rb | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index 69b7a6e690..5b598620d3 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -13,6 +13,7 @@ module ActiveRecord @columns_hash = {} @primary_keys = {} @data_sources = {} + @indexes = {} end def initialize_dup(other) @@ -21,22 +22,25 @@ module ActiveRecord @columns_hash = @columns_hash.dup @primary_keys = @primary_keys.dup @data_sources = @data_sources.dup + @indexes = @indexes.dup end def encode_with(coder) - coder["columns"] = @columns + coder["columns"] = @columns coder["columns_hash"] = @columns_hash coder["primary_keys"] = @primary_keys coder["data_sources"] = @data_sources - coder["version"] = connection.migration_context.current_version + coder["indexes"] = @indexes + coder["version"] = connection.migration_context.current_version end def init_with(coder) - @columns = coder["columns"] + @columns = coder["columns"] @columns_hash = coder["columns_hash"] @primary_keys = coder["primary_keys"] @data_sources = coder["data_sources"] - @version = coder["version"] + @indexes = coder["indexes"] || {} + @version = coder["version"] end def primary_keys(table_name) @@ -57,6 +61,7 @@ module ActiveRecord primary_keys(table_name) columns(table_name) columns_hash(table_name) + indexes(table_name) end end @@ -82,12 +87,17 @@ module ActiveRecord @columns_hash.key?(table_name) end + def indexes(table_name) + @indexes[table_name] ||= connection.indexes(table_name) + end + # Clears out internal caches def clear! @columns.clear @columns_hash.clear @primary_keys.clear @data_sources.clear + @indexes.clear @version = nil end @@ -101,10 +111,10 @@ module ActiveRecord @columns_hash.delete name @primary_keys.delete name @data_sources.delete name + @indexes.delete name end private - def prepare_data_sources connection.data_sources.each { |source| @data_sources[source] = true } end |