Class: ActiveRecord::InternalMetadata

Inherits:
Object
  • 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.

Defined Under Namespace

Classes: NullInternalMetadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ InternalMetadata

Returns a new instance of InternalMetadata.



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

def initialize(connection)
  @connection = connection
  @arel_table = Arel::Table.new(table_name)
end

Instance Attribute Details

#arel_tableObject (readonly)

Returns the value of attribute arel_table.



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

def arel_table
  @arel_table
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  return unless enabled?

  if entry = select_entry(key)
    entry[value_key]
  end
end

#[]=(key, value) ⇒ Object



39
40
41
42
43
# File 'lib/active_record/internal_metadata.rb', line 39

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

  update_or_create_entry(key, value)
end

#countObject



59
60
61
62
63
64
# File 'lib/active_record/internal_metadata.rb', line 59

def count
  sm = Arel::SelectManager.new(arel_table)
  sm.project(*Arel::Nodes::Count.new([Arel.star]))

  connection.select_values(sm, "#{self.class} Count").first
end

#create_tableObject

Creates an internal metadata table with columns key and value



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record/internal_metadata.rb', line 75

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

#create_table_and_set_flags(environment, schema_sha1 = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/active_record/internal_metadata.rb', line 66

def create_table_and_set_flags(environment, schema_sha1 = nil)
  return unless enabled?

  create_table
  update_or_create_entry(:environment, environment)
  update_or_create_entry(:schema_sha1, schema_sha1) if schema_sha1
end

#delete_all_entriesObject



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

def delete_all_entries
  dm = Arel::DeleteManager.new(arel_table)

  connection.delete(dm, "#{self.class} Destroy")
end

#drop_tableObject



87
88
89
90
91
# File 'lib/active_record/internal_metadata.rb', line 87

def drop_table
  return unless enabled?

  connection.drop_table table_name, if_exists: true
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  connection.
end

#primary_keyObject



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

def primary_key
  "key"
end

#table_exists?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_record/internal_metadata.rb', line 93

def table_exists?
  connection.schema_cache.data_source_exists?(table_name)
end

#table_nameObject



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

def table_name
  "#{ActiveRecord::Base.table_name_prefix}#{ActiveRecord::Base.}#{ActiveRecord::Base.table_name_suffix}"
end

#value_keyObject



31
32
33
# File 'lib/active_record/internal_metadata.rb', line 31

def value_key
  "value"
end