user2809703
user2809703

Reputation: 33

rails "rake db:migrate" migration partially done and need to be redone from scratch

I'm new to ruby-on-rails and having hard time.

I tried to run "rake db:migrate" but it was aborted because there was a typo. I typed "users " instead of "users". So I fixed the rb file in db/migrate directory and tried to run it again only to find migration is already partially proceeded and I cannot redo it over again. It seems like it's a common error by beginner like me and the instructor presented two solution for this: first DROP the table in SQL or second COMMENT OUT the already-run part and run the leftover.

But before I found the trouble-shooting advice, I thought I can just use different file name(SIGH) and I changed the 'users' to 'admin_user' and 'admin_user' to 'admin_user2'. I didn't work, and I dropped the table and did

$ rails generate migration AlterUsers and $ rake db:migrate

But it aborted again because it cannot find this file:users.frm. (I have no idea where .frm files are..)

Thanks a lot.

---------------------------Error Message----------------------------------------

$ rake db:migrate
==  AlterUsers: migrating =====================================================
-- rename_table("users", "admin_users")
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Can't find file: './simple_cms_development/users.frm' (errno: 2 - No such file or directory): RENAME TABLE `users` TO `admin_users`/Users/gymmilo/Sites/simple_cms/db/migrate/20130927112013_alter_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

---------------------------migration file----------------------------------------

class AlterUsers < ActiveRecord::Migration
  def change
    rename_table("users", "admin_users")
    add_column("admin_users", "username", :string, :limit => 25)
    change_column("admin_users", "email", :string, :limit => 100)
    rename_column("admin_users", "password", "hashed_password")
    add_column("admin_users", "salt", :string, :limit => 40)
    puts "*** About to add an index ***"
    add_index("admin_users", "username")
  end
end

------------------------------(I already dropped the table once and it turned out like this! Thanks for help. --------

mysql> SHOW TABLES;
+----------------------------------+
| Tables_in_simple_cms_development |
+----------------------------------+
| admin_users1                     |
| schema_migrations                |
+----------------------------------+
2 rows in set (0.00 sec)

mysql> DROP TABLE admin_users1;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
$ rake db:migrate
==  AlterUsers: migrating =====================================================
-- rename_table("users", "admin_users")
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Can't find file: './simple_cms_development/users.frm' (errno: 2 - No such file or directory): RENAME TABLE `users` TO `admin_users`/Users/gymmilo/Sites/simple_cms/db/migrate/20130927112013_alter_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
$ mysql -u simple_cms -p simple_cms_development
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.6.13 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW TABLES;
+----------------------------------+
| Tables_in_simple_cms_development |
+----------------------------------+
| schema_migrations                |
+----------------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye
$ pwd
/Users/gymmilo/Sites/simple_cms
$ rake db:migrate
==  AlterUsers: migrating =====================================================
-- rename_table("users", "admin_users")
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Can't find file: './simple_cms_development/users.frm' (errno: 2 - No such file or directory): RENAME TABLE `users` TO `admin_users`/Users/gymmilo/Sites/simple_cms/db/migrate/20130927112013_alter_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Upvotes: 3

Views: 1538

Answers (2)

Max Wong
Max Wong

Reputation: 326

If your database comes from the production database, you can dump it to local computer and run rake db:migrate.

If it is just a test database, run "rake db:reset", and the whole database would be refresh and all migration task would be executed one by one.

Upvotes: 0

royalghost
royalghost

Reputation: 2808

You can reset database back to the original state using the following command and run the migration again.

rake db:reset

Upvotes: 2

Related Questions