Class: ActiveRecord::SchemaMigration

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

#initialize(connection) ⇒ SchemaMigration

Returns a new instance of SchemaMigration.



14
15
16
17
# File 'lib/active_record/schema_migration.rb', line 14

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.



12
13
14
# File 'lib/active_record/schema_migration.rb', line 12

def arel_table
  @arel_table
end

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/active_record/schema_migration.rb', line 12

def connection
  @connection
end

Instance Method Details

#countObject



78
79
80
81
82
83
# File 'lib/active_record/schema_migration.rb', line 78

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



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

def create_table
  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

#create_version(version) ⇒ Object



19
20
21
22
23
# 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)
  connection.insert(im, "#{self.class} Create", primary_key, version)
end

#delete_all_versionsObject



32
33
34
35
36
# File 'lib/active_record/schema_migration.rb', line 32

def delete_all_versions
  versions.each do |version|
    delete_version(version)
  end
end

#delete_version(version) ⇒ Object



25
26
27
28
29
30
# File 'lib/active_record/schema_migration.rb', line 25

def delete_version(version)
  dm = Arel::DeleteManager.new(arel_table)
  dm.wheres = [arel_table[primary_key].eq(version)]

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

#drop_tableObject



54
55
56
# File 'lib/active_record/schema_migration.rb', line 54

def drop_table
  connection.drop_table table_name, if_exists: true
end

#integer_versionsObject



74
75
76
# File 'lib/active_record/schema_migration.rb', line 74

def integer_versions
  versions.map(&:to_i)
end

#normalize_migration_number(number) ⇒ Object



58
59
60
# File 'lib/active_record/schema_migration.rb', line 58

def normalize_migration_number(number)
  "%.3d" % number.to_i
end

#normalized_versionsObject



62
63
64
# File 'lib/active_record/schema_migration.rb', line 62

def normalized_versions
  versions.map { |v| normalize_migration_number v }
end

#primary_keyObject



38
39
40
# File 'lib/active_record/schema_migration.rb', line 38

def primary_key
  "version"
end

#table_exists?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/active_record/schema_migration.rb', line 85

def table_exists?
  connection.data_source_exists?(table_name)
end

#table_nameObject



42
43
44
# File 'lib/active_record/schema_migration.rb', line 42

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

#versionsObject



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

def versions
  sm = Arel::SelectManager.new(arel_table)
  sm.project(arel_table[primary_key])
  sm.order(arel_table[primary_key].asc)

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