Check here for more info on Migration Naming Conventions. For more info on this line, check out the section on Writing a Rails Model. If we were writing a constructive migration like adding a column or a table, we'd want to name the method in the next line change. Since instead we're changing a column, we'll need to create two methods: up and down where up is the change we want to make, and down is its inverse--a fallback in case we want to undo our changes.
Check here for more details on Changing Migrations. We specify how it should be rolled back in the down method. Rails also provides a way to revert a previous migration completely using the revert method.
Sometimes, we want to execute complex SQL inside a migration. Rails 6 added support for using multiple databases within a single Rails application. If we want to use multiple databases, we configure them in the database. This configuration tells Rails that we want to use two databases— primary and analytics.
If we are using multiple databases, we are required to provide a path for storing migrations for the second database. If we only run the rails db:migrate , it will execute migrations for all the databases.
Since migrations can change the state of the database, and our code might depend on those changes, it is extremely important that the migrations are run first before the new code is applied. In Heroku based deployments, migrations can be run in the release phase of the Procfile. In docker based deployments, we can run a sidecar container to run the migrations first before the app is restarted.
This is very important as otherwise, the new containers can go into an inconsistent state if they start using new code before applying the database changes for that new code. In this post, we saw various aspects of writing a database migration in Rails.
We also saw what constitutes a migration as well as how to handle failures and roll back the migrations if needed. Rails 6 allows us to use multiple databases and the migrations for each need to be added separately. Finally, we briefly saw how to run the migrations during deployment so that database changes are applied properly before any new code starts using them.
Magicians never share their secrets. But we do. Sign up for our Ruby Magic email series and receive deep insights about garbage collection, memory allocation, concurrency and much more.
Migrations Migrations in Rails allow us to evolve the database over the lifetime of an application. Twenty Thousand Leagues Into a Rails Database Migration We can create tables, add or remove columns and add indexes on columns using the migrations.
SQL end. Go back Subscribe to Ruby Magic Magicians never share their secrets. We'd like to set cookies, read why. I accept. Now we have our database and the required tables available. In the two subsequent chapters, we will explore two important components called Controller ActionController and View ActionView. Ruby on Rails - Migrations Advertisements.
Previous Page. Next Page. Previous Page Print Page.
0コメント