Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Rahul
Rahul

Posted on

     

Recent Favorite Rails Tip!

Rails is a powerful framework known for boosting productivity and being developer-friendly. There are many traits that help you in being productive. Let's talk about the one that I found very recently and how difficult was it to get the job done without it.

Rails migrations. If you haven't heard this term, in short, migrations help in changing the schema of your database. Read morehere in case you're not familiar with it.

Rails provides scaffolding to create migration files. You need not create a migration file from scratch nor copy from the existing files. This is pretty standard.

rails generate migration add_email_to_users email:string
Enter fullscreen modeExit fullscreen mode

generates the following file

class AddEmailToUsers < ActiveRecord::Migration[5.0] def change   add_column :users, :email, :string endend
Enter fullscreen modeExit fullscreen mode

After that, you just run

rake db:migrate
Enter fullscreen modeExit fullscreen mode

Making a change to the database hardly takes a minute!

But consider this scenario where you created a table and started working on it.

source code 2

Now for some reasons, you want to rename the column names and also add another column to it.

source code

Since the migration already ran, these are the steps you're required to do to run the migration again.

  • Get the migration version from the file name20200827072540
  • Remove the entry from theschema_migrations table
  • Drop the tabletransaction_dumps
  • Run the migrationrake db:migrate again

This set of things gets really frustrating if you want to play around in your development or you're not really sure of the final schema.

A better way to do this is,

rake db:rollback #rolls back the latest migration changerake db:migrate #runs all the pending migrations again
Enter fullscreen modeExit fullscreen mode

Neat right.

Hang on. This post ain't about this.

If you want to rollback multiple migration files and run them again, the headache is back. You have to get the migration versions and do,

rake db:migrate:down VERSION=20200827072540rake db:migrate:down VERSION=20200825121103rake db:migrate
Enter fullscreen modeExit fullscreen mode

There is aRails way to do this and this is my recent favourite go-to option when I am playing with the migrations.

rake db:migrate:redo
Enter fullscreen modeExit fullscreen mode

This rolls back the latest migration and runs the migration again. Here's the elegant part, if you want to redo the lastn migration files, you could just feed that as a variableSTEP.

rake db:migrate:redo STEP=2
Enter fullscreen modeExit fullscreen mode

output

Easy and painless!

Always remember this, if you're doing something that takes too much time and makes you feel that Rails isn't fun to work with, you might be doing it the wrong way 😉

Thanks@Prathamesh for this tip.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Bangalore, India
  • Work
    Software Engineer
  • Joined

More fromRahul

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp