Gaudam Thiyagarajan
Gaudam Thiyagarajan

Reputation: 1052

Best practise to populate somedata in a existing database in rails

Problem statement:

Let's say I created a new column column_1 in a table table_1 through rails migration. And now I want to populate the data into column_1 doing some computation but it's a one time task.

Approaches:

Is it preferred to do it through migration. or should I add it in seeds and then populate it and remove it back again

or is there any other best practise.

Upvotes: 2

Views: 279

Answers (4)

Sravan
Sravan

Reputation: 18647

Even though there are different approaches, it is better to do that in in Rake or Migrations but not seed.

Rake tasks:

Rake tasks are generally preferred to do maintenance or data migration jobs over a collection of data.

Example of rake:

lib/tasks/addData.rake

  desc "TODO"
  task :my_task1 => :environment do
    User.update_all(status: 'active')
  end

Example of doing it in migration:

If you add status field to user:

class AddStatusToUser < ActiveRecord::Migration
  def up
    add_column :users, :status, :string
    User.update_all(status: 'active')
  end

  def down
    remove_column :users, :status
  end
end

Why Not seeds:

Seeds are generally like database Dump, seed files are used to fill the data into Database for the first time when the application is started. So that the application is kickstarted with different data.

Examples are Application settings, Application Configurations, Admin users etc.

Upvotes: 1

Zia Qamar
Zia Qamar

Reputation: 1775

Generally, we use Rails Migrations to migrate our application’s schema, and Rake tasks to migrate our production data.

There have only been a few cases where we have used Rails Migrations to ensure that a data migration took place as a part of the deployment.

In all other cases, using a Rake task provides us with more flexibility, and less maintenance.

See detailed explanation here

Upvotes: 2

Fiqi Fitransyah
Fiqi Fitransyah

Reputation: 1

One time task ? Definitely will go with migration as it will only executed when the migration take place. And won't be executed afterwards

Rake task is considered as too much, since you will need it to run only once. The task script remain there until you decide to remove it. But, it is totally doesn't make sense ( to build something which will be removed in the near future, unless, for testing purposes in some special cases )

If you're asking about best practices, people will tend to have different approach. It will depend on each case that we are trying to solve. But, in common, there are some cases which are shareable.

Upvotes: 0

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

This is actually an opinion based question

Is it preferred to do it through migration. or should I add it in seeds and then populate it and remove it back again

It depends on how long it's gonna take.

1. Migration:

If it's one-time task go with migration but only if the task is going to run for few minutes.

2. Rake Task:

If the task is one-time but it might take a few hours it should be a rake task, not a migration.

Upvotes: 1

Related Questions