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

Test that DB schema matches the Alembic migrations #3027

Merged
merged 2 commits into from
Nov 9, 2023
Merged

Conversation

jdangerx
Copy link
Member

@jdangerx jdangerx commented Nov 8, 2023

If the DB schema as defined in metadata.classes.Package.from_resource_ids() doesn't match the schema you get from running alembic upgrade head, AND PudlPaths().pudl_db is a pre-existing file, the pudl_sqlite_io_manager throws an error at startup.

HOWEVER!

  1. in integration tests, when pudl_sqlite_io_manager is initialized, PudlPaths().pudl_db does not actually point to an existing file, so we use metadata.create_all() - which masks any differences between the migrations and the Package
  2. in nightly builds, the pudl_sqlite_io_manager isn't initialized until we actually want to write to the DB, which is pretty late in the DAG execution. This is a super avoidable nightly build failure.

So, to catch any divergence between the alembic migrations and the package metadata, I added a unit test that runs the migrations on a test db and tries to initialize the io manager.

@jdangerx jdangerx changed the base branch from main to dev November 8, 2023 01:11
Copy link

codecov bot commented Nov 8, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1bb33dd) 88.7% compared to head (2aaafc4) 88.7%.

Additional details and impacted files
@@          Coverage Diff          @@
##             dev   #3027   +/-   ##
=====================================
  Coverage   88.7%   88.7%           
=====================================
  Files         90      90           
  Lines      10988   10988           
=====================================
  Hits        9752    9752           
  Misses      1236    1236           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@cmgosnell cmgosnell left a comment

Choose a reason for hiding this comment

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

I left some clarifying questions. (lol maybe i am not the right person to review this! but i learn things so maybe it good)

def pudl_sqlite_io_manager_fixture(tmp_path, test_pkg):
"""Create a SQLiteIOManager fixture with a PUDL database schema."""
db_path = tmp_path / "pudl.sqlite"
def fake_pudl_sqlite_io_manager_fixture(tmp_path, test_pkg, monkeypatch):
Copy link
Member

Choose a reason for hiding this comment

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

does this guy actually need monkeypatch?

def test_error_when_handling_view_without_metadata(pudl_sqlite_io_manager_fixture):
def test_migrations_match_metadata(tmp_path, monkeypatch):
pkg = Package.from_resource_ids()
monkeypatch.chdir(Path(__file__).parent.parent.parent)
Copy link
Member

Choose a reason for hiding this comment

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

why do you need to do this chdir?

monkeypatch.setenv("PUDL_OUTPUT", tmp_path)
alembic.config.main(["upgrade", "head"])

PudlSQLiteIOManager(base_dir=tmp_path, db_name="pudl", package=pkg)
Copy link
Member

Choose a reason for hiding this comment

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

so this guy is running compare_metadata which is what is actually doing the checking?? why wasn't this doing it before in the pudl_sqlite_io_manager_fixture??

Copy link
Member

Choose a reason for hiding this comment

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

or is it alembic.config.main(["upgrade", "head"]) that is doing the checking? or the combo or the upgrade/hear w/ generating the io manager?

@jdangerx
Copy link
Member Author

jdangerx commented Nov 9, 2023

I added comments etc. to hopefully address your questions in a more durable way - lmk what you think!

Copy link
Member

@cmgosnell cmgosnell left a comment

Choose a reason for hiding this comment

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

okay okay so we are doing the second if option in the test_migrations_match_metadata. initialize the db w/ alembic then creat the IO manager. okay that makes sense to me

@jdangerx jdangerx merged commit ed6455c into dev Nov 9, 2023
11 checks passed
@jdangerx jdangerx deleted the test-db-schema branch January 2, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants