-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
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 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): |
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.
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) |
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.
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) |
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.
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
??
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.
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?
I added comments etc. to hopefully address your questions in a more durable way - lmk what you think! |
99e03ad
to
2aaafc4
Compare
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.
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
If the DB schema as defined in
metadata.classes.Package.from_resource_ids()
doesn't match the schema you get from runningalembic upgrade head
, ANDPudlPaths().pudl_db
is a pre-existing file, thepudl_sqlite_io_manager
throws an error at startup.HOWEVER!
pudl_sqlite_io_manager
is initialized,PudlPaths().pudl_db
does not actually point to an existing file, so we usemetadata.create_all()
- which masks any differences between the migrations and the Packagepudl_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.