Ruby rails rake not rolling back far enough

I try "rake db:rollback STEP=6" or more, but when I run "rake db:migrate" it only shows the last migration file being executed:

== 20150508040222 CreateStructure: migrating ================================== == 20150508040222 CreateStructure: migrated (0.0000s) =========================

When I try to check if the new fields have been created in the previous step using "rails console" and "puts User.new().inspect" it shows those fields are still missing.

How do I roll back so that it will execute those transaction files that I've edited?

[edit]

When I try rolling back using "rake db:migrate VERSION=20150328013052", it fails at the second last migration, because it can't remove the non-existent fields. I think I modified that migration to add those fields after the migration was already executed, so those fields don't exist. What should I do? I tried modifying the timestamp in schema.rb and running migrate but it looks like that's not what keeps track of the current version.

class AddLoginToUsers < ActiveRecord::Migration
  def change
    add_column :name, :password, :string
  end
end

== 20150508040222 CreateStructure: reverting ================================== == 20150508040222 CreateStructure: reverted (0.0273s) =========================

== 20150506210153 AddLoginToUsers: reverting ================================== -- remove_column(:name, :password, :string) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

Could not find table 'name'/var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:517:in table_structure' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:431:in primary_key' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in copy_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:533:in move_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:526:in block in alter_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:525:in alter_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:467:in alter_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:467:in remove_column' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in block in method_missing' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in block in say_with_time' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in say_with_time' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in method_missing' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:497:in block in revert' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:496:in each' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:496:in revert' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:604:in exec_migration' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in block (2 levels) in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in blo ck in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in with_connection' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:588:in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:765:in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:995:in block in execute_migration_in_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in block in ddl_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in block in transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in within_new_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in transaction' /var/lib/gems/1.9.1/g ems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in ddl_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in execute_migration_in_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in block in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in each' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:827:in down' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:802:in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in block (2 levels) in ' Tasks: TOP => db:migrate (See ful l trace by running task with --trace)

[edit]

Okay, I see, I just had to blank out the migrations to reverse them, and rewrite them going forward. Problem solved.


I would find the current state of the Schema by opening up the schema.rb file (located in db/schema.rb), the first line looks something like:

ActiveRecord::Schema.define(version: 20150507210038) do

Match up that version number with the migrations in your db/migrate folder. Perhaps you are already at the appropriate migration. To go back migrations, I recommend:

rake db:migrate VERSION=20080906120000

Where the version number corresponds to the version on the migration file name.

[UPDATE]

The error is here:

class AddLoginToUsers < ActiveRecord::Migration
  def change
   add_column :name, :password, :string
  end
end

The add_column has the following requirements:

add_column :table, :column_name, :column_type

Make sure the table is pluralized. Check your Schema.rb file to see the tables. Since this is the users table, perhaps you meant something like:

add_column :users, :password, :string

If you are having issues with the schema you can completely dump your database and recreate it. I would edit the appropriate migrations (remove the ones that are causing the issue), then dump and recreate the database by running:

rake db:drop db:create db:migrate

Then you can run another migration in order to add new columns. You can of course add those to the existing migrations before you recreate the database. Hope that helps.

链接地址: http://www.djcxy.com/p/56902.html

上一篇: Ruby on Rails 5.0与迁移用户表冲突升级

下一篇: Ruby rails的耙子不能回滚得足够远