Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Add a step to check for migrations and migrate DB if needed #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

valeriecodes
Copy link
Collaborator

Description of changes

Adds a step to the deploy process which would run migrations on the docker container before it is published.

Note: The original issue requested a test, but I'm not sure how to write an automated test for something like a Makefile task. I also am not sure how best to test locally, beyond running the new task in isolation (which I have done)

Issue Resolved

Fixes #429

@@ -95,7 +95,10 @@ bundle:

setup: build db_create db_migrate

publish: build
migrate_if_needed:
bash -c 'if docker-compose run ${RAILS_CONTAINER} rake db:migrate:status | grep "^\s*down"; then docker-compose run ${RAILS_CONTAINER} rake db:migrate; fi'
Copy link
Collaborator

@robbkidd robbkidd Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently discovered the db:abort_if_pending_migrations task which pretty much does what it says: exits the rake execution with an error if there are pending migrations.

> rake db:db:abort_if_pending_migrations
You have 10 pending migrations:
  20180129223616 AddGroupToTeamMembers
  20180204235116 AddMoocToCodeSchools
  20180401034441 CreateRoles
  20180408172532 AddRoleIdToAdminUser
  20180416024105 AddEmailToTeamMember
  20180420192231 AddIsPartnerToCodeSchools
  20180422030623 CreateSlackUser
  20180422031009 DropUserSlackId
  20180430161218 AddRepNameToCodeschool
  20180711212702 AddMilitarystatusToUser
Run `rails db:migrate` to update your database then try again.
> echo $?
1

And now that I know about it, of course I want to use it! What do you think about this?

migrate_if_needed:
	bash -c 'docker-compose run ${RAILS_CONTAINER} rake db:abort_if_pending_migrations || docker-compose run ${RAILS_CONTAINER} rake db:migrate'

migrate_if_needed:
bash -c 'if docker-compose run ${RAILS_CONTAINER} rake db:migrate:status | grep "^\s*down"; then docker-compose run ${RAILS_CONTAINER} rake db:migrate; fi'

publish: build migrate_if_needed
bin/publish
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I'm not super familiar with the deployed production environment. Looking at the bin/publish script has me thinking that this wiring up of a migrate_if_needed action prior to publish would have the migrations occur in an environment that is not production, but rather in a development or CI environment that is doing Docker image tagging and kubernetes service poking.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I thought maybe the docker container that build was run on would then be promoted to production.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's someone who could walk me through/explain, I would love to write up documentation.

@apex-omontgomery
Copy link
Member

https://github.com/OperationCode/operationcode_backend/wiki/Performing-Production-DB-Migration

Here's a wiki article I created now that our former lead has explained our previous steps, you can look for the details but it's as simple as:

  1. connect to kubectl
  2. get rails console
  3. rake db:migrate

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When merging to production perform db migrate
3 participants