Class: ActiveRecord::InternalMetadata

Inherits:
Base
  • Object
show all
Defined in:
lib/active_record/internal_metadata.rb

Overview

This class is used to create a table that keeps track of values and keys such as which environment migrations were run in.

This is enabled by default. To disable this functionality set ‘use_metadata_table` to false in your database configuration.

Constant Summary

Constants included from ConnectionHandling

ConnectionHandling::DEFAULT_ENV, ConnectionHandling::RAILS_ENV

Constants included from Querying

Querying::QUERYING_METHODS

Constants included from SecureToken

SecureToken::MINIMUM_TOKEN_LENGTH

Constants included from Transactions

Transactions::ACTIONS

Constants included from Callbacks

Callbacks::CALLBACKS

Constants included from AttributeMethods

AttributeMethods::RESTRICTED_CLASS_METHODS

Instance Attribute Summary

Attributes included from ConnectionHandling

#connection_specification_name

Attributes included from Core

#strict_loading_mode

Class Method Summary collapse

Methods included from ConnectionHandling

#clear_cache!, #clear_query_caches_for_current_thread, #connected?, #connected_to, #connected_to?, #connected_to_many, #connecting_to, #connection, #connection_db_config, #connection_pool, #connects_to, #establish_connection, #lookup_connection_handler, #mysql2_connection, #postgresql_connection, #primary_class?, #prohibit_shard_swapping, #remove_connection, #retrieve_connection, #shard_swapping_prohibited?, #sqlite3_connection, #while_preventing_writes

Methods included from QueryCache::ClassMethods

#cache, #uncached

Methods included from Querying

#_load_from_sql, #_query_by_sql, #count_by_sql, #find_by_sql

Methods included from Translation

#i18n_scope, #lookup_ancestors

Methods included from DelegatedType

#delegated_type

Methods included from Explain

#collecting_queries_for_explain, #exec_explain

Methods included from Enum

#enum, extended, #inherited

Methods included from Delegation::DelegateCache

#generate_relation_method, #inherited, #initialize_relation_delegate_cache, #relation_delegate_class

Methods included from Aggregations::ClassMethods

#composed_of

Methods included from Encryption::EncryptableRecord

#ciphertext_for, #decrypt, #encrypt, #encrypted_attribute?

Methods included from Suppressor

registry, #save, #save!

Methods included from SignedId

#signed_id

Methods included from Serialization

#serializable_hash

Methods included from Reflection

add_aggregate_reflection, add_reflection, create

Methods included from NoTouching

applied_to?, apply_to, #no_touching?, #touch, #touch_later

Methods included from TouchLater

#before_committed!, #touch, #touch_later

Methods included from Transactions

#before_committed!, #committed!, #destroy, #rolledback!, #save, #save!, #touch, #transaction, #trigger_transactional_callbacks?, #with_transaction_returning_status

Methods included from NestedAttributes

#_destroy

Methods included from AutosaveAssociation

#changed_for_autosave?, #destroyed_by_association, #destroyed_by_association=, #mark_for_destruction, #marked_for_destruction?, #reload

Methods included from Associations

#association, #association_cached?, eager_load!, #initialize_dup

Methods included from Timestamp

#initialize_dup

Methods included from Callbacks

#destroy, #increment!, #touch

Methods included from AttributeMethods

#[], #[]=, #_has_attribute?, #accessed_fields, #attribute_for_inspect, #attribute_names, #attribute_present?, #attributes, dangerous_attribute_methods, #has_attribute?, #respond_to?

Methods included from Locking::Pessimistic

#lock!, #with_lock

Methods included from Locking::Optimistic

#increment!, #locking_enabled?

Methods included from Validations

#save, #save!, #valid?

Methods included from Integration

#cache_key, #cache_key_with_version, #cache_version, #to_param

Methods included from Scoping

#initialize_internals_callback, #populate_with_current_scope_attributes

Methods included from Inheritance

#initialize_dup

Methods included from ModelSchema

derive_join_table_name

Methods included from Persistence

#becomes, #becomes!, #decrement, #decrement!, #delete, #destroy, #destroy!, #destroyed?, #increment, #increment!, #new_record?, #persisted?, #previously_new_record?, #previously_persisted?, #reload, #save, #save!, #toggle, #toggle!, #touch, #update, #update!, #update_attribute, #update_column, #update_columns

Methods included from Core

#<=>, #==, #blank?, #connection_handler, #encode_with, #freeze, #frozen?, #hash, #init_with, #init_with_attributes, #initialize, #initialize_dup, #inspect, #present?, #pretty_print, #readonly!, #readonly?, #slice, #strict_loading!, #strict_loading?, #strict_loading_n_plus_one_only?, #values_at

Class Method Details

.[](key) ⇒ Object



34
35
36
37
38
# File 'lib/active_record/internal_metadata.rb', line 34

def [](key)
  return unless enabled?

  where(key: key).pick(:value)
end

.[]=(key, value) ⇒ Object



28
29
30
31
32
# File 'lib/active_record/internal_metadata.rb', line 28

def []=(key, value)
  return unless enabled?

  find_or_initialize_by(key: key).update!(value: value)
end

.create_tableObject

Creates an internal metadata table with columns key and value



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_record/internal_metadata.rb', line 41

def create_table
  return unless enabled?

  unless connection.table_exists?(table_name)
    connection.create_table(table_name, id: false) do |t|
      t.string :key, **connection.internal_string_options_for_primary_key
      t.string :value
      t.timestamps
    end
  end
end

.drop_tableObject



53
54
55
56
57
# File 'lib/active_record/internal_metadata.rb', line 53

def drop_table
  return unless enabled?

  connection.drop_table table_name, if_exists: true
end

.enabled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/active_record/internal_metadata.rb', line 16

def enabled?
  ActiveRecord::Base.connection.
end

.primary_keyObject



20
21
22
# File 'lib/active_record/internal_metadata.rb', line 20

def primary_key
  "key"
end

.table_nameObject



24
25
26
# File 'lib/active_record/internal_metadata.rb', line 24

def table_name
  "#{table_name_prefix}#{}#{table_name_suffix}"
end