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)

940
941
942
943
944
945
946
947
948
949
950
951
# File 'activerecord/lib/active_record/migration.rb', line 940

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)

  Base.connection.initialize_schema_migrations_table
end

Class Attribute Details

.migrations_pathsObject


873
874
875
876
877
# File 'activerecord/lib/active_record/migration.rb', line 873

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

.any_migrations?Boolean


861
862
863
# File 'activerecord/lib/active_record/migration.rb', line 861

def any_migrations?
  migrations(migrations_paths).any?
end

.current_version(connection = Base.connection) ⇒ Object


853
854
855
# File 'activerecord/lib/active_record/migration.rb', line 853

def current_version(connection = Base.connection)
  get_all_versions(connection).max || 0
end

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


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

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

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

.forward(migrations_paths, steps = 1) ⇒ Object


815
816
817
# File 'activerecord/lib/active_record/migration.rb', line 815

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

.get_all_versions(connection = Base.connection) ⇒ Object


845
846
847
848
849
850
851
# File 'activerecord/lib/active_record/migration.rb', line 845

def get_all_versions(connection = Base.connection)
  if connection.table_exists?(schema_migrations_table_name)
    SchemaMigration.all.map { |x| x.version.to_i }.sort
  else
    []
  end
end

.last_migrationObject

:nodoc:


869
870
871
# File 'activerecord/lib/active_record/migration.rb', line 869

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

.last_versionObject


865
866
867
# File 'activerecord/lib/active_record/migration.rb', line 865

def last_version
  last_migration.version
end

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


798
799
800
801
802
803
804
805
806
807
808
809
# File 'activerecord/lib/active_record/migration.rb', line 798

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

.migration_files(paths) ⇒ Object


922
923
924
# File 'activerecord/lib/active_record/migration.rb', line 922

def migration_files(paths)
  Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end

.migrations(paths) ⇒ Object


887
888
889
890
891
892
893
894
895
896
897
898
899
900
# File 'activerecord/lib/active_record/migration.rb', line 887

def migrations(paths)
  paths = Array(paths)

  migrations = migration_files(paths).map do |file|
    version, name, scope = parse_migration_filename(file)
    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


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

def migrations_path
  migrations_paths.first
end

.migrations_status(paths) ⇒ Object


902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'activerecord/lib/active_record/migration.rb', line 902

def migrations_status(paths)
  paths = Array(paths)

  db_list = ActiveRecord::SchemaMigration.normalized_versions

  file_list = migration_files(paths).map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
    status = db_list.delete(version) ? "up" : "down"
    [status, version, (name + scope).humanize]
  end.compact

  db_list.map! do |version|
    ["up", version, "********** NO FILE **********"]
  end

  (db_list + file_list).sort_by { |_, version, _| version }
end

.needs_migration?(connection = Base.connection) ⇒ Boolean


857
858
859
# File 'activerecord/lib/active_record/migration.rb', line 857

def needs_migration?(connection = Base.connection)
  (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0
end

.open(migrations_paths) ⇒ Object


837
838
839
# File 'activerecord/lib/active_record/migration.rb', line 837

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

.parse_migration_filename(filename) ⇒ Object

:nodoc:


883
884
885
# File 'activerecord/lib/active_record/migration.rb', line 883

def parse_migration_filename(filename) # :nodoc:
  File.basename(filename).scan(/\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first
end

.rollback(migrations_paths, steps = 1) ⇒ Object


811
812
813
# File 'activerecord/lib/active_record/migration.rb', line 811

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

.run(direction, migrations_paths, target_version) ⇒ Object


833
834
835
# File 'activerecord/lib/active_record/migration.rb', line 833

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

.schema_migrations_table_nameObject


841
842
843
# File 'activerecord/lib/active_record/migration.rb', line 841

def schema_migrations_table_name
  SchemaMigration.table_name
end

.up(migrations_paths, target_version = nil) ⇒ Object


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

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

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

Instance Method Details

#current_migrationObject Also known as: current


957
958
959
# File 'activerecord/lib/active_record/migration.rb', line 957

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

#current_versionObject


953
954
955
# File 'activerecord/lib/active_record/migration.rb', line 953

def current_version
  migrated.max || 0
end

#migrateObject


975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'activerecord/lib/active_record/migration.rb', line 975

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


1012
1013
1014
# File 'activerecord/lib/active_record/migration.rb', line 1012

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

#migrationsObject


1003
1004
1005
# File 'activerecord/lib/active_record/migration.rb', line 1003

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

#pending_migrationsObject


1007
1008
1009
1010
# File 'activerecord/lib/active_record/migration.rb', line 1007

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

#runObject


962
963
964
965
966
967
968
969
970
971
972
973
# File 'activerecord/lib/active_record/migration.rb', line 962

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


992
993
994
995
996
997
998
999
1000
1001
# File 'activerecord/lib/active_record/migration.rb', line 992

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