Class: ActiveRecord::InternalMetadata

Inherits:
Base show all
Defined in:
activerecord/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.

Constant Summary

Constants included from Querying

Querying::QUERYING_METHODS

Constants included from ConnectionHandling

ConnectionHandling::DEFAULT_ENV, ConnectionHandling::ER_BAD_DB_ERROR, ConnectionHandling::RAILS_ENV

Constants included from SecureToken

SecureToken::MINIMUM_TOKEN_LENGTH

Constants included from Transactions

Transactions::ACTIONS

Constants included from ActiveModel::SecurePassword

ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED

Constants included from Callbacks

Callbacks::CALLBACKS

Constants included from AttributeMethods

AttributeMethods::RESTRICTED_CLASS_METHODS

Constants included from ActiveModel::AttributeMethods

ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP, ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP

Instance Attribute Summary

Attributes included from ConnectionHandling

#connection_specification_name

Class Method Summary collapse

Methods included from Aggregations::ClassMethods

#composed_of

Methods included from Delegation::DelegateCache

#generate_relation_method, #inherited, #initialize_relation_delegate_cache, #relation_delegate_class

Methods included from Enum

#enum, extended, #inherited

Methods included from Explain

#collecting_queries_for_explain, #exec_explain

Methods included from Translation

#i18n_scope, #lookup_ancestors

Methods included from ActiveModel::Translation

#human_attribute_name, #i18n_scope, #lookup_ancestors

Methods included from ActiveModel::Naming

extended, #model_name, param_key, plural, route_key, singular, singular_route_key, uncountable?

Methods included from Querying

#count_by_sql, #find_by_sql

Methods included from QueryCache::ClassMethods

#cache, #uncached

Methods included from ConnectionHandling

#clear_cache!, #clear_query_caches_for_current_thread, #connected?, #connected_to, #connected_to?, #connection, #connection_config, #connection_db_config, #connection_pool, #connects_to, #current_role, #establish_connection, #lookup_connection_handler, #mysql2_connection, #postgresql_connection, #primary_class?, #remove_connection, #retrieve_connection, #sqlite3_connection, #with_handler

Methods included from ActiveSupport::DescendantsTracker

clear, descendants, #descendants, #direct_descendants, direct_descendants, #inherited, store_inherited

Methods included from ActiveSupport::Benchmarkable

#benchmark

Methods included from Suppressor

#save, #save!

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included

Methods included from Serialization

#serializable_hash

Methods included from ActiveModel::Serializers::JSON

#as_json, #from_json

Methods included from ActiveModel::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

#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, #reload

Methods included from ActiveSupport::Autoload

#autoload, #autoload_at, #autoload_under, #autoloads, #eager_autoload, #eager_load!, extended

Methods included from Timestamp

#initialize_dup

Methods included from Callbacks

#destroy, #increment!, #touch

Methods included from AttributeMethods

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

Methods included from ActiveModel::AttributeMethods

#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?

Methods included from Locking::Pessimistic

#lock!, #with_lock

Methods included from Locking::Optimistic

#locking_enabled?

Methods included from Validations

#save, #save!, #valid?

Methods included from ActiveModel::Validations

#errors, #initialize_dup, #invalid?, #valid?, #validate!, #validates_with

Methods included from Integration

#cache_key, #cache_key_with_version, #cache_version, #to_param

Methods included from ActiveModel::Conversion

#to_key, #to_model, #to_param, #to_partial_path

Methods included from ActiveModel::AttributeAssignment

#assign_attributes

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?, #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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods

Class Method Details

.[](key) ⇒ Object



27
28
29
# File 'activerecord/lib/active_record/internal_metadata.rb', line 27

def [](key)
  where(key: key).pluck(:value).first
end

.[]=(key, value) ⇒ Object



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

def []=(key, value)
  find_or_initialize_by(key: key).update!(value: value)
end

._internal?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'activerecord/lib/active_record/internal_metadata.rb', line 11

def _internal?
  true
end

.create_tableObject

Creates an internal metadata table with columns key and value



32
33
34
35
36
37
38
39
40
41
42
# File 'activerecord/lib/active_record/internal_metadata.rb', line 32

def create_table
  unless table_exists?
    key_options = connection.internal_string_options_for_primary_key

    connection.create_table(table_name, id: false) do |t|
      t.string :key, **key_options
      t.string :value
      t.timestamps
    end
  end
end

.drop_tableObject



44
45
46
# File 'activerecord/lib/active_record/internal_metadata.rb', line 44

def drop_table
  connection.drop_table table_name, if_exists: true
end

.primary_keyObject



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

def primary_key
  "key"
end

.table_nameObject



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

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