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 check command for upgrade diffs #1101

Closed

Conversation

nxlouie
Copy link
Contributor

@nxlouie nxlouie commented Oct 17, 2022

Fixes #724

Description

  • Add check command. If the revision command with autogenerate has pending upgrade operations to run, then raise an error. Otherwise, continue.

Testing

Wrote Tests:

tox tests/test_command.py::CheckTest::test_check_no_changes
tox tests/test_command.py::CheckTest::test_check_changes_detected

I also tested this function in my company Replica's code base:

from alembic.config import Config
from alembic.command import check

def test_migration_autogen_parity():
    alembic_cfg = Config("alembic.ini")
    check(alembic_cfg)
  1. When there is parity, the test passes.
  2. When I spike a model with an extra change and then run the test, the error is thrown:
>           raise util.RevisionOpsNotEmptyError(f"Revision has upgrade ops to run: {diffs}.")
E           alembic.util.exc.RevisionOpsNotEmptyError: Revision has upgrade ops to run: [('add_column', 'schema_name', 'table_name', Column('new_column_name', TEXT(), table=<table_name>))].

.venv/lib/python3.10/site-packages/alembic/command.py:292: RevisionOpsNotEmptyError
...
Results (16.42s):
       1 failed
         - migrations/test_migration_autogen_parity.py:7 test_migration_autogen_parity

Have a nice day!

@zzzeek
Copy link
Member

zzzeek commented Oct 18, 2022

OK two things needed:

  1. a test in test_command.py
  2. shouldn't this be it's own command? alembic check calling it "alembic revision" is a little unclear because with the --check option, a revision would never be generated.

@nxlouie nxlouie marked this pull request as ready for review October 18, 2022 04:43
@nxlouie
Copy link
Contributor Author

nxlouie commented Oct 18, 2022

Hey @zzzeek,

  1. Added a few tests. Apologies for submitting this early - I put it in draft mode and set out to figure out the testing framework, meaning to set as ready when I had the tests. I understand that generates extra noise for the maintainers.
  2. I set out to follow the proposal in Enable comparing migrations to implementation in tests. #724 by @ziima verbatim:

I'm glad to hear that. Here's my proposal: add --check option to revision command. It would print the differences in migrations (using logging, since alembic has no verbosity) and return non-zero exit code, if it found any. It would be only available when --autogenerate is used. Other command options are applied accordingly.

If I understand correctly, it was to mirror the django makemigrations --check behavior.
Do we still want to follow that? If not, I can leave an update on the issue and implement a separate command.

@zzzeek
Copy link
Member

zzzeek commented Nov 7, 2022

I think "revision --check" makes no sense for us as it means "revision" is not going to ever generate a revision, "check" is better. @CaselIT opinions?

@CaselIT
Copy link
Member

CaselIT commented Nov 7, 2022

I think "revision --check" makes no sense for us as it means "revision" is not going to ever generate a revision, "check" is better. @CaselIT opinions?

I do prefer check since using revision --check without augenerate would be basically a no-op, while we can document that check implies autogenerate mode (or reflection in any case)

@nxlouie
Copy link
Contributor Author

nxlouie commented Nov 7, 2022

Sounds good, I'll give it a shot.

@nxlouie
Copy link
Contributor Author

nxlouie commented Nov 8, 2022

@zzzeek @CaselIT - I updated this PR to be a distinct check command. In addition to the tests written here, I can confirm that a variant of this code has successfully been running in my company Replica's code base for the past month, and that when I plugged in this check command, the test caught the non-empty revision ops.

@nxlouie nxlouie changed the title add check flag and check upgrade diffs add check command for upgrade diffs Nov 9, 2022
@VenturaParade
Copy link

Hello everyone, I am also waiting for this to be merged to use this feature, any idea when it could be?

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision 090b594 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Collaborator

New Gerrit review created for change 090b594: https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295

@zzzeek
Copy link
Member

zzzeek commented Dec 12, 2022

we have very little time right now to work on alembic and this PR was forgotten. will see if I can keep an eye on it in the coming weeks and maybe get it merged

@zzzeek
Copy link
Member

zzzeek commented Dec 13, 2022

the test seems to not be passing.

@nxlouie
Copy link
Contributor Author

nxlouie commented Dec 13, 2022

Hey @zzzeek - thanks for running the tests. I put in fixes for the test_help_text and pep8 failures.

However, I don't know what to do with the openstack_gerrit errors. Let me know if you think it's unrelated or if there's particular action I should take with them.

@CaselIT
Copy link
Member

CaselIT commented Dec 13, 2022

Hey @zzzeek - thanks for running the tests. I put in fixes for the test_help_text and pep8 failures.

However, I don't know what to do with the openstack_gerrit errors. Let me know if you think it's unrelated or if there's particular action I should take with them.

These failures are most likely unrelated

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

OK, this is sqla-tester setting up my work on behalf of CaselIT to try to get revision fbe8f10 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Collaborator

Patchset fbe8f10 added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295

@zzzeek
Copy link
Member

zzzeek commented Dec 13, 2022

yes dont worry about the openstack stuff, those tests frquently stop working due to downstream changes and i usualyl dont have time to keep fixing them

@sqla-tester
Copy link
Collaborator

mike bayer (zzzeek) wrote:

looks like you're down to pep8 stuff. install pre-commit https://pre-commit.com/ in the repo and that will run the code formatters.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295

@nxlouie
Copy link
Contributor Author

nxlouie commented Dec 13, 2022

@zzzeek Thanks for the pointers. I ran pre-commit run --files tests/test_command.py alembic/command.py alembic/util/__init__.py alembic/util/exc.py and put in the fixes from black

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

Hi, this is sqla-tester and I see you've pinged me for review. However, user nxlouie is not authorized to initiate CI jobs. Please wait for a project member to do this!

Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

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

OK, this is sqla-tester setting up my work on behalf of CaselIT to try to get revision 807ed54 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Collaborator

Patchset 807ed54 added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295

@sqla-tester
Copy link
Collaborator

mike bayer (zzzeek) wrote:

@CaselIT can you give this a final review? i made some changes in this patchset. will call this 1.9.0

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295

@zzzeek
Copy link
Member

zzzeek commented Dec 13, 2022

I've made a series of final changes that are visible only on the gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295 . feel free to review!

@nxlouie
Copy link
Contributor Author

nxlouie commented Dec 13, 2022

Awesome, The documentation looks good from my end!

@sqla-tester
Copy link
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4295 has been merged. Congratulations! :)

@VenturaParade
Copy link

Thanks a lot for the quick work :)

@nxlouie nxlouie deleted the add-check-flag-to-revision-command branch December 15, 2022 20:24
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.

Enable comparing migrations to implementation in tests.
5 participants