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 Alembic's check model parity utility to the linting #199

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chouinar
Copy link
Contributor

Ticket

#198

Changes

Added alembic check to the linter which verifies the SQLAlchemy models are in sync with what is in the DB

Context for reviewers

It's very easy to forget to generate your migrations, this will make the linting step tell you.

Testing

If you're already up-to-date, you should get an output like:
Screenshot 2023-09-21 at 10 28 56 AM

If you're out of date (easily done with make db-migrate-down db-check-model-parity), you'll get:
Screenshot 2023-09-21 at 10 29 44 AM

@chouinar
Copy link
Contributor Author

Need to look at this a bit more - looks like normal DB migrations don't happen in the CI like the code I'm porting this from.

@chouinar
Copy link
Contributor Author

Need to look at this a bit more - looks like normal DB migrations don't happen in the CI like the code I'm porting this from.

The place I ported this from did the CI a bit different. It ran make init and then make lint, but we don't do make init here. Here, the DB does get started, but only because we're running docker commands.

@lorenyu - thoughts on whether we should adjust something here?

One approach we could do is to add another check that does make init-db db-check-model-parity to verify the DB is up-to-date. That, or we just add init-db to the linting step.

@lorenyu
Copy link
Contributor

lorenyu commented Sep 21, 2023

@chouinar if i understand correctly, this command checks if the local database has any pending migrations, which doesn't quite strike me as being in the category of "linting", which is more about programming style and conventions and should should be able to be done purely by analyzing source code without any sort of building or initialization.

we could potentially make this a separate job in ci-app rather than as part of the lint job

could you help me understand better what the check is solving? i see the description says "It's very easy to forget to generate your migrations". So is that a scenario where a developer makes a change to the models/schema but forgets to generate migrations?

@lorenyu
Copy link
Contributor

lorenyu commented Sep 21, 2023

yeah after thinking about it a bit more, my initial reaction is to make this a separate job that:

  • checks out the repo
  • initializes the local db and runs migrations locally
  • runs alembic check

thoughts?

@chouinar
Copy link
Contributor Author

yeah after thinking about it a bit more, my initial reaction is to make this a separate job that:

  • checks out the repo
  • initializes the local db and runs migrations locally
  • runs alembic check

thoughts?

So, would that be roughly a new step like this?

  test:
    name: DB Model Parity
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Start tests
        run: |
          make init-db
          make db-check-model-parity

@lorenyu
Copy link
Contributor

lorenyu commented Sep 22, 2023

yep something like that. i would love call it something other than "model parity", since i'm not quite sure what that phrase means, but i also can't come up with anything off the top of my head.

on a related note, are there other common database-related errors that could be worth checking? i vaguely remember either you or someone else suggesting the idea of checking to make sure that no new database migrations were merged to main that aren't in this branch, to prevent the alembic multiple heads / merge issue

@chouinar
Copy link
Contributor Author

yep something like that. i would love call it something other than "model parity", since i'm not quite sure what that phrase means, but i also can't come up with anything off the top of my head.

on a related note, are there other common database-related errors that could be worth checking? i vaguely remember either you or someone else suggesting the idea of checking to make sure that no new database migrations were merged to main that aren't in this branch, to prevent the alembic multiple heads / merge issue

There's a unit test that checks if you'd end up in the multi-head state in the test_migrations file. Since GitHub requires merges from main to merge a PR, that should catch it now.

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.

2 participants