Ruby Database Migration

Database Migration

Greetings!!!

This blog page will help you to play with the schema. First of all, make sure that you have everything setup. Let's see what is required
  • MySQL path variable  
Make sure that your environment variable is set for the MySQL bin folder. Following are the steps to setup environment variable:
  1. On the Windows desktop, right-click the My Computer icon, and select Properties.
  2. Next, select the Advanced tab from the System Properties menu that appears, and click the Environment Variables button.
  3. Under System Variables, select Path, and then click the Edit button. The Edit System Variable dialogue should appear.
  4. Place your cursor at the end of the text appearing in the space marked Variable Value. (Use the End key to ensure that your cursor is positioned at the very end of the text in this space.) Then enter the complete path name of your MySQL bin directory (for example, C:\Program Files\MySQL\MySQL Server 5.7\bin)
  • MySQL user setup
Make sure that you have created a user with full permission on the database. That user should be configured in your project. Following is the command to create a user and grant all access.
grant all privileges on <Project_Name>.* to '<user_name>'@'localhost' identified by '<password>';

Example:
grant all privileges on demo_project_development.* to 'rails_user'@'localhost' identified by 'Test@1234';
  • database.yml
Database.yml is the critical part which acts as a connector between your application and database. Make sure that you have provided proper hostname, username, password and database name.
  • schema.rb
Schema.rb file holds the ActiveRecords as a database schema. In following two sections, this file would be used to read/write database schema.

Database to ActiveRecords

First of all getting into this topic make sure that you have created the database and tables and configured database.yml file properly. Open the command prompt and execute the following command:

rails db:schema:dump

This command will dump ActiveRecord from the database into your schema.rb file. If you don't have the schema.rb file under db folder it will create one for you. 

Or you could use the following command:

rake db:schema:dump

Before executing the command make sure that you are under the project folder.

ActiveRecrods to Database

This is the reverse procedure to populate database tables from the schema.rb file. Sometimes we ran into a condition where we have the code but we don't have database schema. At this time this procedure would help a lot. 

Before proceeding make sure that you have setup an empty database and database.yml is configured properly. If everything is set, you are good to go with following command:

rake db:schema:load

This command will load schema to your database from your schema.rb file. 

EXTRA NOTES:

RAKE is the best command to work with database related stuff. Following are some of the commands which would be useful:

  • db:create Creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
  • db:create:all Creates the database for all environments.
  • db:drop Drops the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
  • db:drop:all Drops the database for all environments.
  • db:migrate Runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.
  • db:migrate:redo Runs db:migrate:down and db:migrate:up or db:migrate:rollback and db:migrate:migrate depending on the specified migration. I usually run this after creating and running a new migration to ensure the migration is reversible.
  • db:migrate:up Runs the up for the given migration VERSION.
  • db:migrate:down Runs the down for the given migration VERSION.
  • db:migrate:status Displays the current migration status.
  • db:migrate:rollback Rolls back the last migration.
  • db:version Prints the current schema version.
  • db:forward Pushes the schema to the next version.
  • db:seed Runs the db/seeds.rb file.
  • db:schema:load Loads the schema into the current environment’s database.
  • db:schema:dump Dumps the current environment’s schema to db/schema.rb.
  • db:setup Runs db:schema:load and db:seed.
  • db:reset Runs db:drop and db:setup.
  • db:migrate:reset Runs db:dropdb:create and db:migrate.
  • db:test:prepare Check for pending migrations and load the test schema. (If you run rake without any arguments it will do this by default.)
  • db:test:clone Recreate the test database from the current environment’s database schema.
  • db:test:clone_structure Similar to db:test:clone, but it will ensure that your test database has the same structure, including charsets and collations, as your current environment’s database.

Comments

Popular posts from this blog

How to solve migration issues with rails

ActiveRecords and ActiveRelation

CRUD Basics