In the previous chapter, we reset the counter cache via the console. Sometimes, you may need a larger script to reset your database or handle multiple conditions and update a database value. For example, if you need to update a column for all records that meet specific criteria, a data migration script would be required. Here, we can utilize rake tasks to achieve this. These rake tasks only need to run once, so they are called one_timer
rake tasks. Let's consider a scenario for writing a rake task. Suppose you have a Rails app where users can post articles, with each article's view count stored in the views_count
column. Initially, view counts were not tracked separately, but now you want to set a default count based on specific conditions:
- Articles created over a year ago should start with a view count of 100.
- Articles created within the last year should start with a view count of 10.
To create the one_timer
rake task file, run the following command in your terminal:
Now, you can define all your one-time Rake tasks in this file by adding each task under one_timer
namespace.
You can now run these tasks in the terminal with the following command:
Data migration
One challenge with one-timer Rake tasks is that developers may occasionally forget to run these tasks in the production environment, which can lead to unexpected behavior in the application. To automate this process, we can use the data_migrate gem, allowing us to run data migrations alongside schema migrations.
Once the data_migrate
gem is added, you can create a new data migration by running the following command in your project:
Replace <migration-name>
with a descriptive name for your data migration. This will generate a new file in the db/data/ folder named <timestamp>_<migration-name>.rb
, where <timestamp>
is a unique identifier for each migration.
In the development environment, to apply changes from the new data migration to your database, simply run:
This command will execute all pending data migrations, ensuring your data changes are in sync with the rest of your application.
Data migration best practices
-
Include both the up
and down
methods in the data migration file to facilitate data addition and rollback. In some cases rollback may not be possible, and thus in such cases we can raise an irreversible migration error. You don't have to take care of this by yourself, the default data migration template will already be having the code for the same.
-
When adding the logic for data migration, ensure that you pay attention to the data being changed. It's important to consider the previous data or preferences of a user or an organization that is already present in the database.
-
Try to use bulk modification methods from Rails, like say update_all
etc whenever possible. This will help with the performance of the data migration.
-
If you are using find_each
or modifying items in batch, then ensure they are wrapped in a transaction because if this migration fails in production, it will be very hard to roll out a fix for partially completed changes.
This is a theoretical chapter. There is nothing to commit in here. If you have made some changes to the project, clean them up by executing the
following command.