My experience with Rails Active Record migrations

WOCinTechChat.com

Overview

Why

Preliminary questions

  • How long will a migration like this take?
  • Would there be an impact on users during the migration?
  • Should the migration be done outside of business hours?

How

  • Create a new branch to begin my work on.
  • Use the Active Record generator to create a standalone migrationrails generate migration ChangeCadenceTeamIdToBigInt.
  • Define up and down methods in my Migration class. The up method describes the column changes we’re going to make. The down method describes what to do if we ever need to undo the change. Alternatively, you can use the def change method to accomplish the same thing. See mock example below.
  • Execute the migration file locally with rake db:migrate. This command will also make the changes to our database and update our schema.
  • Push changes on my branch to GitHub and create a pull request.
  • Test changes in a QA environment to be sure that nothing is broken.
  • Open an interactive shell in one of the cadence analytics pods.
  • Connect to the cadence analytics database. Now that we’re in production, write the schema modifications in pure SQL instead.
  • The migration I created in phase 1 has a version number, for example 20190829141124_change_cadence_team_id_to_big_int.rb. I use that version number to specify an addition to an internal Rails database table called schema_migrations like so insert into schema_migrations (version) values ('20190829121124');.
  • Finally, I merge my pull request into the master branch. The migration won’t run again since I’ve added it manually to the schema_migrations table.

Conclusion

Photo by Val Vesa on Unsplash
  • A task like this requires teamwork.
  • You should chat with your team about the best way execute the migration (manually or automatically).
  • It’s very important to collaborate with database engineering and tech operations teams to learn about the impact executing your migration will have (i.e. locking tables).

--

--

--

Software engineer | My mom makes the best empanadas you'll ever taste instagram.com/gailscaribb

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

BoseAR: Developing a Spatial BoseAR Experience

Getting Started with the TomTom Geofencing Service

Integrating AI into Clinical Workflow with Orthanc and OHIF Viewer

By author

Permissions in your Laravel API

Android Automation: A super simple guide to create your first Espresso test

Progressive Web Applications & Internet of Things (part 1 of 4)

Becoming a Software Engineer — 2— Prep and Planning

Compiler Design — Phases of Compiler

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alicia

Alicia

Software engineer | My mom makes the best empanadas you'll ever taste instagram.com/gailscaribb

More from Medium

All the Code I Didn’t Write

“Hello, World” in Ruby

How to use Active Record in Sinatra (RUBY)

Association in Ruby on Rails