Class: ActiveRecord::Migrator
- Inherits:
-
Object
- Object
- ActiveRecord::Migrator
- Defined in:
- lib/active_record/migration.rb
Overview
:nodoc:
Class Attribute Summary collapse
Class Method Summary collapse
- .current_version(connection = Base.connection) ⇒ Object
- .down(migrations_paths, target_version = nil, &block) ⇒ Object
- .forward(migrations_paths, steps = 1) ⇒ Object
- .get_all_versions ⇒ Object
-
.last_migration ⇒ Object
:nodoc:.
- .last_version ⇒ Object
- .migrate(migrations_paths, target_version = nil, &block) ⇒ Object
- .migrations(paths) ⇒ Object
- .migrations_path ⇒ Object
- .needs_migration?(connection = Base.connection) ⇒ Boolean
- .open(migrations_paths) ⇒ Object
- .proper_table_name(name, options = {}) ⇒ Object
- .rollback(migrations_paths, steps = 1) ⇒ Object
- .run(direction, migrations_paths, target_version) ⇒ Object
- .schema_migrations_table_name ⇒ Object
- .up(migrations_paths, target_version = nil) ⇒ Object
Instance Method Summary collapse
- #current_migration ⇒ Object (also: #current)
- #current_version ⇒ Object
-
#initialize(direction, migrations, target_version = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #migrated ⇒ Object
- #migrations ⇒ Object
- #pending_migrations ⇒ Object
- #run ⇒ Object
- #runnable ⇒ Object
Constructor Details
#initialize(direction, migrations, target_version = nil) ⇒ Migrator
Returns a new instance of Migrator.
909 910 911 912 913 914 915 916 917 918 919 920 |
# File 'lib/active_record/migration.rb', line 909 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_paths ⇒ Object
867 868 869 870 871 |
# File 'lib/active_record/migration.rb', line 867 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_version(connection = Base.connection) ⇒ Object
833 834 835 836 837 838 839 840 |
# File 'lib/active_record/migration.rb', line 833 def current_version(connection = Base.connection) sm_table = schema_migrations_table_name if connection.table_exists?(sm_table) get_all_versions.max || 0 else 0 end end |
.down(migrations_paths, target_version = nil, &block) ⇒ Object
810 811 812 813 814 815 |
# File 'lib/active_record/migration.rb', line 810 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
799 800 801 |
# File 'lib/active_record/migration.rb', line 799 def forward(migrations_paths, steps=1) move(:up, migrations_paths, steps) end |
.get_all_versions ⇒ Object
829 830 831 |
# File 'lib/active_record/migration.rb', line 829 def get_all_versions SchemaMigration.all.map { |x| x.version.to_i }.sort end |
.last_migration ⇒ Object
:nodoc:
850 851 852 |
# File 'lib/active_record/migration.rb', line 850 def last_migration #:nodoc: migrations(migrations_paths).last || NullMigration.new end |
.last_version ⇒ Object
846 847 848 |
# File 'lib/active_record/migration.rb', line 846 def last_version last_migration.version end |
.migrate(migrations_paths, target_version = nil, &block) ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/active_record/migration.rb', line 782 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
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/active_record/migration.rb', line 877 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_path ⇒ Object
873 874 875 |
# File 'lib/active_record/migration.rb', line 873 def migrations_path migrations_paths.first end |
.needs_migration?(connection = Base.connection) ⇒ Boolean
842 843 844 |
# File 'lib/active_record/migration.rb', line 842 def needs_migration?(connection = Base.connection) current_version(connection) < last_version end |
.open(migrations_paths) ⇒ Object
821 822 823 |
# File 'lib/active_record/migration.rb', line 821 def open(migrations_paths) self.new(:up, migrations(migrations_paths), nil) end |
.proper_table_name(name, options = {}) ⇒ Object
854 855 856 857 858 859 860 861 862 863 864 865 |
# File 'lib/active_record/migration.rb', line 854 def proper_table_name(name, = {}) ActiveSupport::Deprecation.warn "ActiveRecord::Migrator.proper_table_name is deprecated and will be removed in Rails 4.2. Use the proper_table_name instance method on ActiveRecord::Migration instead" = { table_name_prefix: ActiveRecord::Base.table_name_prefix, table_name_suffix: ActiveRecord::Base.table_name_suffix }.merge() if name.respond_to? :table_name name.table_name else "#{[:table_name_prefix]}#{name}#{[:table_name_suffix]}" end end |
.rollback(migrations_paths, steps = 1) ⇒ Object
795 796 797 |
# File 'lib/active_record/migration.rb', line 795 def rollback(migrations_paths, steps=1) move(:down, migrations_paths, steps) end |
.run(direction, migrations_paths, target_version) ⇒ Object
817 818 819 |
# File 'lib/active_record/migration.rb', line 817 def run(direction, migrations_paths, target_version) self.new(direction, migrations(migrations_paths), target_version).run end |
.schema_migrations_table_name ⇒ Object
825 826 827 |
# File 'lib/active_record/migration.rb', line 825 def schema_migrations_table_name SchemaMigration.table_name end |
.up(migrations_paths, target_version = nil) ⇒ Object
803 804 805 806 807 808 |
# File 'lib/active_record/migration.rb', line 803 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_migration ⇒ Object Also known as: current
926 927 928 |
# File 'lib/active_record/migration.rb', line 926 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
922 923 924 |
# File 'lib/active_record/migration.rb', line 922 def current_version migrated.max || 0 end |
#migrate ⇒ Object
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'lib/active_record/migration.rb', line 944 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 |
#migrated ⇒ Object
981 982 983 |
# File 'lib/active_record/migration.rb', line 981 def migrated @migrated_versions ||= Set.new(self.class.get_all_versions) end |
#migrations ⇒ Object
972 973 974 |
# File 'lib/active_record/migration.rb', line 972 def migrations down? ? @migrations.reverse : @migrations.sort_by(&:version) end |
#pending_migrations ⇒ Object
976 977 978 979 |
# File 'lib/active_record/migration.rb', line 976 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version) } end |
#run ⇒ Object
931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'lib/active_record/migration.rb', line 931 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 |
#runnable ⇒ Object
961 962 963 964 965 966 967 968 969 970 |
# File 'lib/active_record/migration.rb', line 961 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 |