Class: ActiveRecord::Migrator

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/migration.rb

Overview

:nodoc:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, migrations, target_version = nil) ⇒ Migrator

Returns a new instance of Migrator

Raises:

  • (StandardError)

863
864
865
866
867
868
869
870
871
872
873
874
# File 'activerecord/lib/active_record/migration.rb', line 863

def initialize(direction, migrations, target_version = nil)
  raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?

  @direction         = direction
  @target_version    = target_version
  @migrated_versions = nil
  @migrations        = migrations

  validate(@migrations)

  ActiveRecord::SchemaMigration.create_table
end

Class Attribute Details

.migrations_pathsObject


821
822
823
824
825
# File 'activerecord/lib/active_record/migration.rb', line 821

def migrations_paths
  @migrations_paths ||= ['db/migrate']
  # just to not break things if someone uses: migration_path = some_string
  Array(@migrations_paths)
end

Class Method Details

.current_versionObject


791
792
793
794
795
796
797
798
# File 'activerecord/lib/active_record/migration.rb', line 791

def current_version
  sm_table = schema_migrations_table_name
  if Base.connection.table_exists?(sm_table)
    get_all_versions.max || 0
  else
    0
  end
end

.down(migrations_paths, target_version = nil, &block) ⇒ Object


768
769
770
771
772
773
# File 'activerecord/lib/active_record/migration.rb', line 768

def down(migrations_paths, target_version = nil, &block)
  migrations = migrations(migrations_paths)
  migrations.select! { |m| yield m } if block_given?

  self.new(:down, migrations, target_version).migrate
end

.forward(migrations_paths, steps = 1) ⇒ Object


757
758
759
# File 'activerecord/lib/active_record/migration.rb', line 757

def forward(migrations_paths, steps=1)
  move(:up, migrations_paths, steps)
end

.get_all_versionsObject


787
788
789
# File 'activerecord/lib/active_record/migration.rb', line 787

def get_all_versions
  SchemaMigration.all.map { |x| x.version.to_i }.sort
end

.last_migrationObject

:nodoc:


808
809
810
# File 'activerecord/lib/active_record/migration.rb', line 808

def last_migration #:nodoc:
  migrations(migrations_paths).last || NullMigration.new
end

.last_versionObject


804
805
806
# File 'activerecord/lib/active_record/migration.rb', line 804

def last_version
  last_migration.version
end

.migrate(migrations_paths, target_version = nil, &block) ⇒ Object


740
741
742
743
744
745
746
747
748
749
750
751
# File 'activerecord/lib/active_record/migration.rb', line 740

def migrate(migrations_paths, target_version = nil, &block)
  case
  when target_version.nil?
    up(migrations_paths, target_version, &block)
  when current_version == 0 && target_version == 0
    []
  when current_version > target_version
    down(migrations_paths, target_version, &block)
  else
    up(migrations_paths, target_version, &block)
  end
end

.migrations(paths) ⇒ Object


831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'activerecord/lib/active_record/migration.rb', line 831

def migrations(paths)
  paths = Array(paths)

  files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]

  migrations = files.map do |file|
    version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first

    raise IllegalMigrationNameError.new(file) unless version
    version = version.to_i
    name = name.camelize

    MigrationProxy.new(name, version, file, scope)
  end

  migrations.sort_by(&:version)
end

.migrations_pathObject


827
828
829
# File 'activerecord/lib/active_record/migration.rb', line 827

def migrations_path
  migrations_paths.first
end

.needs_migration?Boolean

Returns:

  • (Boolean)

800
801
802
# File 'activerecord/lib/active_record/migration.rb', line 800

def needs_migration?
  current_version < last_version
end

.open(migrations_paths) ⇒ Object


779
780
781
# File 'activerecord/lib/active_record/migration.rb', line 779

def open(migrations_paths)
  self.new(:up, migrations(migrations_paths), nil)
end

.proper_table_name(name) ⇒ Object


812
813
814
815
816
817
818
819
# File 'activerecord/lib/active_record/migration.rb', line 812

def proper_table_name(name)
  # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
  if name.respond_to? :table_name
    name.table_name
  else
    "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
  end
end

.rollback(migrations_paths, steps = 1) ⇒ Object


753
754
755
# File 'activerecord/lib/active_record/migration.rb', line 753

def rollback(migrations_paths, steps=1)
  move(:down, migrations_paths, steps)
end

.run(direction, migrations_paths, target_version) ⇒ Object


775
776
777
# File 'activerecord/lib/active_record/migration.rb', line 775

def run(direction, migrations_paths, target_version)
  self.new(direction, migrations(migrations_paths), target_version).run
end

.schema_migrations_table_nameObject


783
784
785
# File 'activerecord/lib/active_record/migration.rb', line 783

def schema_migrations_table_name
  SchemaMigration.table_name
end

.up(migrations_paths, target_version = nil) ⇒ Object


761
762
763
764
765
766
# File 'activerecord/lib/active_record/migration.rb', line 761

def up(migrations_paths, target_version = nil)
  migrations = migrations(migrations_paths)
  migrations.select! { |m| yield m } if block_given?

  self.new(:up, migrations, target_version).migrate
end

Instance Method Details

#current_migrationObject Also known as: current


880
881
882
# File 'activerecord/lib/active_record/migration.rb', line 880

def current_migration
  migrations.detect { |m| m.version == current_version }
end

#current_versionObject


876
877
878
# File 'activerecord/lib/active_record/migration.rb', line 876

def current_version
  migrated.max || 0
end

#migrateObject


898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'activerecord/lib/active_record/migration.rb', line 898

def migrate
  if !target && @target_version && @target_version > 0
    raise UnknownMigrationVersionError.new(@target_version)
  end

  runnable.each do |migration|
    Base.logger.info "Migrating to #{migration.name} (#{migration.version})" if Base.logger

    begin
      execute_migration_in_transaction(migration, @direction)
    rescue => e
      canceled_msg = use_transaction?(migration) ? "this and " : ""
      raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
    end
  end
end

#migratedObject


935
936
937
# File 'activerecord/lib/active_record/migration.rb', line 935

def migrated
  @migrated_versions ||= Set.new(self.class.get_all_versions)
end

#migrationsObject


926
927
928
# File 'activerecord/lib/active_record/migration.rb', line 926

def migrations
  down? ? @migrations.reverse : @migrations.sort_by(&:version)
end

#pending_migrationsObject


930
931
932
933
# File 'activerecord/lib/active_record/migration.rb', line 930

def pending_migrations
  already_migrated = migrated
  migrations.reject { |m| already_migrated.include?(m.version) }
end

#runObject


885
886
887
888
889
890
891
892
893
894
895
896
# File 'activerecord/lib/active_record/migration.rb', line 885

def run
  migration = migrations.detect { |m| m.version == @target_version }
  raise UnknownMigrationVersionError.new(@target_version) if migration.nil?
  unless (up? && migrated.include?(migration.version.to_i)) || (down? && !migrated.include?(migration.version.to_i))
    begin
      execute_migration_in_transaction(migration, @direction)
    rescue => e
      canceled_msg = use_transaction?(migration) ? ", this migration was canceled" : ""
      raise StandardError, "An error has occurred#{canceled_msg}:\n\n#{e}", e.backtrace
    end
  end
end

#runnableObject


915
916
917
918
919
920
921
922
923
924
# File 'activerecord/lib/active_record/migration.rb', line 915

def runnable
  runnable = migrations[start..finish]
  if up?
    runnable.reject { |m| ran?(m) }
  else
    # skip the last migration if we're headed down, but not ALL the way down
    runnable.pop if target
    runnable.find_all { |m| ran?(m) }
  end
end