rails, migrations,

Swapnil Gourshete Swapnil Gourshete Follow Jun 28, 2019 · 1 mins read
Squasher: Replacing old migrations
Share this

Too many migrations in application makes it painful. Sometimes they are written a long time back, that now you do not even remember. At the point you might want to replace them with something simple and compact.

Rails have a gem for this utility, called squasher. It eventually scans all the migrations in application and converts them into a single migration named InitSchema. Let's see by example.

  • Installation
    1. Include gem squasher in Gemfile and run
      $ bundle install
    2. To work effectively run
      $ bundle binstub squasher
      and you will have runner inside bin folder.

Now we are set to use squasher.

  • Usage
    1. Let’s consider we need to squash all migrations before 2018. Simply run
      $ squasher 2018
      and squasher will create two migrations InitSchema and SquasherClean. InitSchema will be the new migration for all the migrations before 2018. Squasher afterward deletes all those now-redundent migrations.

Yes it is that simple!


Let’s go down some depth

  • Does Rails versioning affect our way?
    –> Yes. By default squasher will not generate any rails versioning for newly generated migration. So there are high chances it may break during migrations.
    To counter it, squasher simply comes with option -
    -m, --migration=VERSION. It define the rails migration version(since Rails 5)

  • Can I squash migrations before a specific date?
    –> Yes. Simply run squash command like
    $ squasher 2019/12/09
    It will squash migrations before 09 December, 2019.

Swapnil Gourshete
Written by Swapnil Gourshete Follow
Hi I am Swapnil, a Software Engineer and computer science enthusiastic