diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-06-10 02:33:14 -0700 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-06-10 02:33:14 -0700 |
commit | 4845c0685aa3983b4685d4afe141ac5d398521bc (patch) | |
tree | 76ad4b6c69694928f8d7aa920e388f19cbbbdf0f /activerecord/lib/active_record | |
parent | fdcc252e81629eb2d142da32d47037fc5cf4030d (diff) | |
parent | d741a4c6f863778c5ebf04b21f6c3292091c13a7 (diff) | |
download | rails-4845c0685aa3983b4685d4afe141ac5d398521bc.tar.gz rails-4845c0685aa3983b4685d4afe141ac5d398521bc.tar.bz2 rails-4845c0685aa3983b4685d4afe141ac5d398521bc.zip |
Merge pull request #6665 from schneems/schneems/raise-migration-error
Notify A User they Have Pending Migrations
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 32 | ||||
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 7 |
2 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index ac4f53c774..a0169203b0 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -32,6 +32,12 @@ module ActiveRecord end end + class PendingMigrationError < ActiveRecordError#:nodoc: + def initialize + super("Migrations are pending run 'bundle exec rake db:migrate RAILS_ENV=#{ENV['RAILS_ENV']}' to resolve the issue") + end + end + # = Active Record Migrations # # Migrations can manage the evolution of a schema used by several physical @@ -326,10 +332,28 @@ module ActiveRecord class Migration autoload :CommandRecorder, 'active_record/migration/command_recorder' + + # This class is used to verify that all migrations have been run before + # loading a web page if config.active_record.migration_error is set to :page_load + class CheckPending + def initialize(app) + @app = app + end + + def call(env) + ActiveRecord::Migration.check_pending! + status, headers, body = @app.call(env) + end + end + class << self attr_accessor :delegate # :nodoc: end + def self.check_pending! + raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator::needs_migrations? + end + def self.method_missing(name, *args, &block) # :nodoc: (delegate || superclass.delegate).send(name, *args, &block) end @@ -605,6 +629,14 @@ module ActiveRecord end end + def needs_migrations? + current_version < last_version + end + + def last_version + migrations(migrations_paths).last.try(:version)||0 + end + 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 name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}" diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 1e497b2a79..319516413b 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -59,6 +59,13 @@ module ActiveRecord ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger } end + initializer "active_record.migration_error" do |app| + if config.active_record.delete(:migration_error) == :page_load + config.app_middleware.insert_after "::ActionDispatch::Callbacks", + "ActiveRecord::Migration::CheckPending" + end + end + initializer "active_record.set_configs" do |app| ActiveSupport.on_load(:active_record) do if app.config.active_record.delete(:whitelist_attributes) |