Class: ActiveRecord::InternalMetadata
- Inherits:
-
Object
- Object
- ActiveRecord::InternalMetadata
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.
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
Returns a new instance of InternalMetadata.
18
19
20
21
|
# File 'activerecord/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_table ⇒ Object
Returns the value of attribute arel_table
16
17
18
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 16
def arel_table
@arel_table
end
|
#connection ⇒ Object
Returns the value of attribute connection
16
17
18
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 16
def connection
@connection
end
|
Instance Method Details
45
46
47
48
49
50
51
|
# File 'activerecord/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 'activerecord/lib/active_record/internal_metadata.rb', line 39
def []=(key, value)
return unless enabled?
update_or_create_entry(key, value)
end
|
59
60
61
62
63
64
|
# File 'activerecord/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_table ⇒ Object
Creates an internal metadata table with columns key
and value
73
74
75
76
77
78
79
80
81
82
83
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 73
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
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 66
def create_table_and_set_flags(environment, schema_sha1 = nil)
create_table
update_or_create_entry(:environment, environment)
update_or_create_entry(:schema_sha1, schema_sha1) if schema_sha1
end
|
#delete_all_entries ⇒ Object
53
54
55
56
57
|
# File 'activerecord/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_table ⇒ Object
85
86
87
88
89
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 85
def drop_table
return unless enabled?
connection.drop_table table_name, if_exists: true
end
|
#enabled? ⇒ Boolean
23
24
25
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 23
def enabled?
connection.use_metadata_table?
end
|
#primary_key ⇒ Object
27
28
29
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 27
def primary_key
"key"
end
|
#table_exists? ⇒ Boolean
91
92
93
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 91
def table_exists?
connection.schema_cache.data_source_exists?(table_name)
end
|
#value_key ⇒ Object
31
32
33
|
# File 'activerecord/lib/active_record/internal_metadata.rb', line 31
def value_key
"value"
end
|