-
Notifications
You must be signed in to change notification settings - Fork 22
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 database schema update and database migration logic #520
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
73613cb
add db migration logic and a test for it
andrii-i 72d89b7
make Job and JobDefinition records extendable
andrii-i 85ce20a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a6734fe
make updated_job_model a fixture
andrii-i 0e003d1
add return types to test_orm fixtures
andrii-i 1cf8f4b
refactor update_db_schema logic into a separate function
andrii-i 6266c3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a88336e
make initial_db return a tuple
andrii-i 9fa650c
improve naming clarity
andrii-i 45bb2de
remove a level of intendation in update_db_schema
andrii-i 992d733
Ignore nullability and default values during the db migration, docume…
andrii-i ec2bddf
improve update_db_schema accordingly to comments
andrii-i 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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Type | ||
|
||
import pytest | ||
from sqlalchemy import Column, Integer, String, inspect | ||
from sqlalchemy.orm import DeclarativeMeta, sessionmaker | ||
|
||
from jupyter_scheduler.orm import ( | ||
create_session, | ||
create_tables, | ||
declarative_base, | ||
generate_uuid, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def initial_db(jp_scheduler_db_url) -> tuple[Type[DeclarativeMeta], sessionmaker, str]: | ||
TestBase = declarative_base() | ||
|
||
class MockInitialJob(TestBase): | ||
__tablename__ = "jobs" | ||
job_id = Column(String(36), primary_key=True, default=generate_uuid) | ||
runtime_environment_name = Column(String(256), nullable=False) | ||
input_filename = Column(String(256), nullable=False) | ||
|
||
initial_job = MockInitialJob(runtime_environment_name="abc", input_filename="input.ipynb") | ||
|
||
create_tables(db_url=jp_scheduler_db_url, Base=TestBase) | ||
|
||
Session = create_session(jp_scheduler_db_url) | ||
session = Session() | ||
|
||
session.add(initial_job) | ||
session.commit() | ||
job_id = initial_job.job_id | ||
session.close() | ||
|
||
return (TestBase, Session, job_id) | ||
|
||
|
||
@pytest.fixture | ||
def updated_job_model(initial_db) -> Type[DeclarativeMeta]: | ||
TestBase = initial_db[0] | ||
|
||
class MockUpdatedJob(TestBase): | ||
__tablename__ = "jobs" | ||
__table_args__ = {"extend_existing": True} | ||
job_id = Column(String(36), primary_key=True, default=generate_uuid) | ||
runtime_environment_name = Column(String(256), nullable=False) | ||
input_filename = Column(String(256), nullable=False) | ||
new_column = Column("new_column", Integer) | ||
|
||
return MockUpdatedJob | ||
|
||
|
||
def test_create_tables_with_new_column(jp_scheduler_db_url, initial_db, updated_job_model): | ||
TestBase, Session, initial_job_id = initial_db | ||
|
||
session = Session() | ||
initial_columns = {col["name"] for col in inspect(session.bind).get_columns("jobs")} | ||
assert "new_column" not in initial_columns | ||
session.close() | ||
|
||
JobModel = updated_job_model | ||
create_tables(db_url=jp_scheduler_db_url, Base=TestBase) | ||
|
||
session = Session() | ||
updated_columns = {col["name"] for col in inspect(session.bind).get_columns("jobs")} | ||
assert "new_column" in updated_columns | ||
|
||
updated_job = session.query(JobModel).filter(JobModel.job_id == initial_job_id).one() | ||
assert hasattr(updated_job, "new_column") | ||
assert updated_job.runtime_environment_name == "abc" | ||
assert updated_job.input_filename == "input.ipynb" |
Oops, something went wrong.
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.
I had to do some testing to understand why this is necessary if we are defining
update_db_schema()
. Essentially, this argument is needed to extend the table metadata (e.g. column names & types) associated with the table. If this line is omitted intest_orm.py
, the test fails with the error:So both the table migration (defined in
update_db_schema()
) and table metadata migration (defined here by setting__table_args__ = {"extend_existing": True}
) must be performed when a new column is added. 😵No action needed. I just felt it was necessary to call this out to other readers in this review.