-
Notifications
You must be signed in to change notification settings - Fork 4
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
Upgrade to SQLAlchemy 2.0 #197
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c1f4ed2
WIP
chouinar 3386708
Merge branch 'main' into chouinar/sqlalchemy2
chouinar 3599e03
A lot of cleanup and getting everything working
chouinar 73f5909
Removing unrelated change
chouinar 0179b17
Wrap drop schema in begin so it actually runs
chouinar b850c90
Add pyyaml dependency as it's not automatically there anymore
chouinar de0502b
Undoing something that isn't needed
chouinar f2c43ab
No need to specify type for mapped_column, use connection instead of …
chouinar d4d7a90
Use connection for both create and destroy schema
chouinar 5faceb5
alphabetize the map
chouinar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ authors = ["Nava Engineering <[email protected]>"] | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.10" | ||
SQLAlchemy = {extras = ["mypy"], version = "^1.4.40"} | ||
SQLAlchemy = {extras = ["mypy"], version = "2.0"} | ||
alembic = "^1.8.1" | ||
psycopg2-binary = "^2.9.3" | ||
python-dotenv = "^0.20.0" | ||
|
@@ -37,6 +37,7 @@ bandit = "^1.7.4" | |
pytest = "^6.0.0" | ||
pytest-watch = "^4.2.0" | ||
pytest-lazy-fixture = "^0.6.3" | ||
types-pyyaml = "^6.0.12.11" | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
|
@@ -80,8 +81,6 @@ warn_redundant_casts = true | |
warn_unreachable = true | ||
warn_unused_ignores = true | ||
|
||
plugins = ["sqlalchemy.ext.mypy.plugin"] | ||
|
||
[tool.bandit] | ||
# Ignore audit logging test file since test audit logging requires a lot of operations that trigger bandit warnings | ||
exclude_dirs = ["./tests/src/logging/test_audit.py"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
# Convenience script for running alembic migration commands through a pyscript | ||
# rather than the command line. This allows poetry to package and alias it for | ||
# running on the production docker image from any directory. | ||
import itertools | ||
import logging | ||
import os | ||
from typing import Optional | ||
|
||
import alembic.command as command | ||
import alembic.script as script | ||
import sqlalchemy | ||
from alembic.config import Config | ||
from alembic.operations.ops import MigrationScript | ||
from alembic.runtime import migration | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -53,41 +50,3 @@ def have_all_migrations_run(db_engine: sqlalchemy.engine.Engine) -> None: | |
logger.info( | ||
f"The current migration head is up to date, {current_heads} and Alembic is expecting {expected_heads}" | ||
) | ||
|
||
|
||
def check_model_parity() -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't used and was giving me some type errors. There's a better way to do this anyways as Alembic has a utility now for checking the model parity for you which I'll create a ticket and add later. |
||
revisions: list[MigrationScript] = [] | ||
|
||
def process_revision_directives( | ||
context: migration.MigrationContext, | ||
revision: Optional[str], | ||
directives: list[MigrationScript], | ||
) -> None: | ||
nonlocal revisions | ||
revisions = list(directives) | ||
# Prevent actually generating a migration | ||
directives[:] = [] | ||
|
||
command.revision( | ||
config=alembic_cfg, | ||
autogenerate=True, | ||
process_revision_directives=process_revision_directives, | ||
) | ||
diff = list( | ||
itertools.chain.from_iterable( | ||
op.as_diffs() for script in revisions for op in script.upgrade_ops_list | ||
) | ||
) | ||
|
||
message = ( | ||
"The application models are not in sync with the migrations. You should generate " | ||
"a new automigration or update your local migration file. " | ||
"If there are unexpected errors you may need to merge main into your branch." | ||
) | ||
|
||
if diff: | ||
for line in diff: | ||
print("::error title=Missing migration::Missing migration:", line) | ||
|
||
logger.error(message, extra={"issues": str(diff)}) | ||
raise Exception(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Batch mode no longer exists: https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#psycopg2-fast-execution-helpers
There's another way to configure it to do a similar operation, but I wasn't sure if we wanted to keep it