Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming original migration file in Rails after doing the rename_table_migration

I happened to create a Query model in Rails and recently found out that this is one of the reserved words now..

I renamed the table using a new migration file and renamed all the files that were created (name of new model - Plot)

Question: is it OK to rename the original migration file (20111228212521_create_queries.rb) to 20111228212521_create_plots.rb)

and everything inside the old file:

class CreateQueries < ActiveRecord::Migration
 def change
   create_table :queries do |t|
    t.string :name
    t.text :content

    t.timestamps
    end
   end
 end

to

class CreatePlots < ActiveRecord::Migration
   def change
    create_table :plots do |t|
    t.string :name
    t.text :content

    t.timestamps
    end
   end
 end

??

I just don't want too many migration files and also worried that there may be some errors when I switch to production..

like image 519
Stpn Avatar asked Dec 30 '11 05:12

Stpn


People also ask

Can I rename migration file rails?

You can change the migration file name, but you have to perform a few steps: rake db:rollback to the point that queries table is rolled back. Now change the name of migration file, also the contents. Change the name of any Model that may use the table.

How do I rename a migration file?

Also you can change a migrations name after the fact (you've already run the migration). You just need to change the filename in the migrations database table (so it can find it to roll it back) in addition to changing the actual filename and classname. I'd use a 3 digit number for the prefix.

How do I change migration in rails?

5 Changing Existing Migrations You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

Can I delete a migration file rails?

【Ruby on Rails】 When you want to delete a migration file you made, you can choose version and delete it. You don't need to execute rails db:rollback many times!


3 Answers

You can change the migration file name, but you have to perform a few steps:

  1. rake db:rollback to the point that queries table is rolled back.

  2. Now change the name of migration file, also the contents.

  3. Change the name of any Model that may use the table.

  4. rake db:migrate

like image 113
abhijeetmisra Avatar answered Oct 06 '22 02:10

abhijeetmisra


The short answer is to just make another migration file.

Migration files are meant to keep track of each and every change to the database. So, you're encouraged to make small one-off changes in a separate file. I can't speak for your situation, but in my situation, when I make a mistake like this, I simply create a new migration file and don't check the old migration file into source control. This way the errant changes are only on my local db and don't get into prod/dev/staging.

like image 27
Ramy Avatar answered Oct 06 '22 00:10

Ramy


Aside from rolling back, and especially useful for when you need to rename a migration from early on in production you can now in Rails 4 create a new migration to rename it.

  1. $rails generate migration RenamesFooBarr

and then in the method of the new migration add

rename_table :old_migration_name, :new_migration name

like this:

class RenamesFooBar < ActiveRecord::Migration
   def change
       rename_table :old_foo_bar_name, :new_foo_bar_name
   end
end

This will effectively take care of all the indexes as well in the up and down since ActiveRecord recognizes the rename_table

source: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

like image 33
jrichocean Avatar answered Oct 06 '22 01:10

jrichocean