Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db-migrate to container path to meet infra template requirements #176

Merged
merged 7 commits into from
Jun 15, 2023

Conversation

lorenyu
Copy link
Contributor

@lorenyu lorenyu commented Jun 14, 2023

Ticket

Resolves #175

Changes

  • Add db-migrate executable command to application container PATH
  • Use consistent method for creating db connection between migrations and application
  • Remove code for make_connection_uri
  • Remove support for offline migrations

Context for reviewers

The infra template PR https://github.com/navapbc/template-infra/pull/316/files adds application-requirements that require application container to have a "db-migrate" command available in the PATH. This PR updates template-application-flask to do that.

The previous migrations code connects to the database using a different method than what the application uses, which means that the migrations code does not have the functionality of connecting to the DB using IAM auth, which is a requirement for the infra template. In this change, we make the migrations code in env.py use the existing PostgresDBClient class and remove the extraneous make_connection_uri class.

As part of the change, we also remove support for offline migrations, which is not needed in general. This simplifies the code paths in env.py and reduces maintenance burden.

Testing

Locally ran make release-build then ran docker -it <image hash> db-migrate
image

The migration obviously didn't succeed since I didn't pass any necessary env vars, but the db-migrate command was found.

Copy link
Contributor

@chouinar chouinar left a comment

Choose a reason for hiding this comment

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

LGTM! We'd done something similar for using the DB client on NJ instead of the make_connection_uri method

# Load env vars before anything further
from src.util.local import load_local_env_vars # noqa: E402 isort:skip

load_local_env_vars()
Copy link
Contributor

Choose a reason for hiding this comment

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

How do the env vars get loaded locally? I thought that was done when running Flask commands, but this doesn't look to run via Flask.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good question. i have to confess that was prioritizing getting database integration working, so i intentionally let running on host machine break. i was gonna work on that soon with #136

i'm currently thinking of having developers install direnv, and having an app/.envrc file that looks like

source local.env

and then whenever you cd into app direnv will automatically get all the env vars from local.env into your shell, and when you cd out of the directory direnv will automatically unload those env vars.

the benefit of this is that local developers can also override vars in local.env without risking checking those in by adding the overrides to .envrc e.g.

source local.env
export DB_HOST=localhost

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(I actually probably already broke running host machine with PR #172 , so it was probably already broken before this PR)

@lorenyu lorenyu merged commit 2d38953 into main Jun 15, 2023
@lorenyu lorenyu deleted the lorenyu/dbmigrations branch June 15, 2023 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate with infra migrations
2 participants