Class: ActiveRecord::SchemaMigration
- Inherits:
-
Object
- Object
- ActiveRecord::SchemaMigration
show all
- Defined in:
- lib/active_record/schema_migration.rb
Overview
This class is used to create a table that keeps track of which migrations have been applied to a given database. When a migration is run, its schema number is inserted in to the schema migrations table so it doesn’t need to be executed the next time.
Defined Under Namespace
Classes: NullSchemaMigration
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SchemaMigration.
14
15
16
17
|
# File 'lib/active_record/schema_migration.rb', line 14
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.
12
13
14
|
# File 'lib/active_record/schema_migration.rb', line 12
def arel_table
@arel_table
end
|
Instance Method Details
#count ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/active_record/schema_migration.rb', line 90
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
52
53
54
55
56
57
58
59
60
|
# File 'lib/active_record/schema_migration.rb', line 52
def create_table
@pool.with_connection do |connection|
unless connection.table_exists?(table_name)
connection.create_table(table_name, id: false) do |t|
t.string :version, **connection.internal_string_options_for_primary_key
end
end
end
end
|
#create_version(version) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/active_record/schema_migration.rb', line 19
def create_version(version)
im = Arel::InsertManager.new(arel_table)
im.insert(arel_table[primary_key] => version)
@pool.with_connection do |connection|
connection.insert(im, "#{self.class} Create", primary_key, version)
end
end
|
#delete_all_versions ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/active_record/schema_migration.rb', line 36
def delete_all_versions
@pool.with_connection do |connection|
versions.each do |version|
delete_version(version)
end
end
end
|
#delete_version(version) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/active_record/schema_migration.rb', line 27
def delete_version(version)
dm = Arel::DeleteManager.new(arel_table)
dm.wheres = [arel_table[primary_key].eq(version)]
@pool.with_connection do |connection|
connection.delete(dm, "#{self.class} Destroy")
end
end
|
#drop_table ⇒ Object
62
63
64
65
66
|
# File 'lib/active_record/schema_migration.rb', line 62
def drop_table
@pool.with_connection do |connection|
connection.drop_table table_name, if_exists: true
end
end
|
#integer_versions ⇒ Object
86
87
88
|
# File 'lib/active_record/schema_migration.rb', line 86
def integer_versions
versions.map(&:to_i)
end
|
#normalize_migration_number(number) ⇒ Object
68
69
70
|
# File 'lib/active_record/schema_migration.rb', line 68
def normalize_migration_number(number)
"%.3d" % number.to_i
end
|
#normalized_versions ⇒ Object
72
73
74
|
# File 'lib/active_record/schema_migration.rb', line 72
def normalized_versions
versions.map { |v| normalize_migration_number v }
end
|
#primary_key ⇒ Object
44
45
46
|
# File 'lib/active_record/schema_migration.rb', line 44
def primary_key
"version"
end
|
#table_exists? ⇒ Boolean
99
100
101
102
103
|
# File 'lib/active_record/schema_migration.rb', line 99
def table_exists?
@pool.with_connection do |connection|
connection.data_source_exists?(table_name)
end
end
|
#versions ⇒ Object
76
77
78
79
80
81
82
83
84
|
# File 'lib/active_record/schema_migration.rb', line 76
def versions
sm = Arel::SelectManager.new(arel_table)
sm.project(arel_table[primary_key])
sm.order(arel_table[primary_key].asc)
@pool.with_connection do |connection|
connection.select_values(sm, "#{self.class} Load")
end
end
|