-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database schema update and database migration logic (#520)
* add db migration logic and a test for it * make Job and JobDefinition records extendable * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make updated_job_model a fixture * add return types to test_orm fixtures * refactor update_db_schema logic into a separate function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make initial_db return a tuple * improve naming clarity * remove a level of intendation in update_db_schema * Ignore nullability and default values during the db migration, document the fact via comments * improve update_db_schema accordingly to comments --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d690ac8
commit 5b55901
Showing
3 changed files
with
113 additions
and
4 deletions.
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" |