123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::SchemaMigration

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: activerecord/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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(pool) ⇒ SchemaMigration

[ GitHub ]

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

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

Instance Attribute Details

#arel_table (readonly)

[ GitHub ]

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

attr_reader :arel_table

#table_exists?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 115

def table_exists?
  @pool.with_connection do |connection|
    connection.data_source_exists?(table_name)
  end
end

Instance Method Details

#count

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 106

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

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 68

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)

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 23

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

#create_versions(versions)

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 31

def create_versions(versions) # :nodoc:
  return if versions.empty?

  im = Arel::InsertManager.new(arel_table)
  im.columns << arel_table[primary_key]
  im.values = im.create_values_list(versions.map { |version| [version] })
  @pool.with_connection do |connection|
    connection.execute(connection.to_sql(im), "#{self.class} Create")
  end
end

#delete_all_versions

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 51

def delete_all_versions
  # Eagerly check in connection to avoid checking in/out many times in the called method.
  @pool.with_connection do
    versions.each do |version|
      delete_version(version)
    end
  end
end

#delete_version(version)

[ GitHub ]

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

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

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 78

def drop_table
  @pool.with_connection do |connection|
    connection.drop_table table_name, if_exists: true
  end
end

#integer_versions

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 102

def integer_versions
  versions.map(&:to_i)
end

#normalize_migration_number(number)

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 84

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

#normalized_versions

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 88

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

#primary_key

[ GitHub ]

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

def primary_key
  "version"
end

#table_name

[ GitHub ]

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

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

#versions

[ GitHub ]

  
# File 'activerecord/lib/active_record/schema_migration.rb', line 92

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