Hello everyone, I’m new in Rails and have a question about migration.

Hello everyone, I’m new in Rails and have a question about migration. I have a simple rails application, which has a database with tables. 
1. If I need to rename/delete/add column to the table in db should I create 3 migrations or it can be one big migration?
2. My directory db/migrate contains now 10 migrations and I think it is not good, what should I do with them?
Could you, please,
You already invited:

JavierPons

Upvotes from: Emmanuel Tom

1. Go with one migration.
2. This is fine.

There are a lot of resources on the internet that you can find on the topic of rails migrations. I would recommend reading the Rails guides :

allian

Upvotes from: Emmanuel Tom

don’t worry about the number of migrations - over time, you might have hundreds.if you want to eventually flatten, that’s up to you,but if it’s a new rails app, you’re probably going to create from schema anyways, not migrate a new db.re: adding multiple columns in different tables, you just add multiple lines for each column.just don’t put them in a block for that table, which the generator does for a new model
 
class AddNewColumns < ActiveRecord::Migration[5.0]
def change
add_column :products, :part_number, :string
add_column :cars, :model, :string
end
end

 
re: renaming - sometimes it’s easier just to delete the column and readd the new column
 
however, if you’re not worried about current data (this is a new app) often times I’ll completely delete an old migration and replace it with a new one if I’ve changed my mind on how the data is structured
 
so, let’s say you already created a migration for the product table - if you’ve decided you actually needed other columns in it, if you haven’t already deployed the app, go ahead and just 1) modify the migration to add the other columns or 2) delete it and create a new one that is structured the way you want
 
migrations are handled in the sort order - so if you create a new migration and it has the same “serial” then it will run in the same order as before. This is mainly important if you have modifying migrations or have foreign keys where order is important

Practice

Upvotes from: Emmanuel

if you’re new then guides.rubyonrails.org is your friend.

Emmanuel

Upvotes from:

Normally you would create a scaffold for each Model (which gives you a create table migration) Then you would create any additional migrations as needed to adjust the table columns. The "bigness" of a migration depends on what you are trying to do that are related to that one migration. for example, if you need to create three columns and they are semantically related, then you would use one migration.  If the are not related, then you would use three migrations.

If you wanna answer this question please Login or Register