Class: ActiveRecord::InternalMetadata
- Inherits:
-
Object
- Object
- ActiveRecord::InternalMetadata
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
Returns a new instance of InternalMetadata.
18
19
20
21
|
# File 'lib/active_record/internal_metadata.rb', line 18
def initialize(pool)
@pool = pool
@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 'lib/active_record/internal_metadata.rb', line 16
def arel_table
@arel_table
end
|
Instance Method Details
#[](key) ⇒ Object
47
48
49
50
51
52
53
54
55
|
# File 'lib/active_record/internal_metadata.rb', line 47
def [](key)
return unless enabled?
@pool.with_connection do |connection|
if entry = select_entry(connection, key)
entry[value_key]
end
end
end
|
#[]=(key, value) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/active_record/internal_metadata.rb', line 39
def []=(key, value)
return unless enabled?
@pool.with_connection do |connection|
update_or_create_entry(connection, key, value)
end
end
|
#count ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/active_record/internal_metadata.rb', line 65
def count
sm = Arel::SelectManager.new(arel_table)
sm.project(*Arel::Nodes::Count.new([Arel.star]))
@pool.with_connection do |connection|
connection.select_values(sm, "#{self.class} Count").first
end
end
|
#create_table ⇒ Object
Creates an internal metadata table with columns key
and value
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/active_record/internal_metadata.rb', line 85
def create_table
return unless enabled?
@pool.with_connection do |connection|
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
end
|
#create_table_and_set_flags(environment, schema_sha1 = nil) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/active_record/internal_metadata.rb', line 74
def create_table_and_set_flags(environment, schema_sha1 = nil)
return unless enabled?
@pool.with_connection do |connection|
create_table
update_or_create_entry(connection, :environment, environment)
update_or_create_entry(connection, :schema_sha1, schema_sha1) if schema_sha1
end
end
|
#delete_all_entries ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/active_record/internal_metadata.rb', line 57
def delete_all_entries
dm = Arel::DeleteManager.new(arel_table)
@pool.with_connection do |connection|
connection.delete(dm, "#{self.class} Destroy")
end
end
|
#drop_table ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/active_record/internal_metadata.rb', line 99
def drop_table
return unless enabled?
@pool.with_connection do |connection|
connection.drop_table table_name, if_exists: true
end
end
|
#enabled? ⇒ Boolean
35
36
37
|
# File 'lib/active_record/internal_metadata.rb', line 35
def enabled?
@pool.db_config.use_metadata_table?
end
|
#primary_key ⇒ Object
23
24
25
|
# File 'lib/active_record/internal_metadata.rb', line 23
def primary_key
"key"
end
|
#table_exists? ⇒ Boolean
107
108
109
|
# File 'lib/active_record/internal_metadata.rb', line 107
def table_exists?
@pool.schema_cache.data_source_exists?(table_name)
end
|
#value_key ⇒ Object
27
28
29
|
# File 'lib/active_record/internal_metadata.rb', line 27
def value_key
"value"
end
|