ship Alembic migration scripts as package data
Needs ReviewPublic

Authored by dcallagh on Jun 15 2017, 7:11 AM.

Details

Reviewers
jskladan
Summary

This simplifies the Alembic config loading a bit. It means that the code
can always find its Alembic migrations regardless whether it was
imported from an installed RPM package, or a random git checkout
anywhere on the filesystem.

Specifically, for Greenwave I would like to import the ResultsDB application and run it as a fixture in the Greenwave tests. Right now, that doesn't work because ResultsDB is expecting alembic.ini to be in the current working directory for a git checkout. This seemed like a nice solution to how it can find its Alembic migrations regardless of the cwd.

Test Plan

Check that unit tests still pass
Check that RPM still builds successfully
Ask Josef what else I should test :-)

Diff Detail

Repository
rRSDB resultsdb
Lint
Lint Skipped
Unit
Unit Tests Skipped
dcallagh created this revision.Jun 15 2017, 7:11 AM

Btw it looks like a huge scary diff but that's just from moving the alembic directory into the resultsdb package so that it is picked up as package data.

If the rpm builds/installs and migrations keep working, it's fine with me. Have you tried building/installing the package, and running resultsdb [init_alembic, init_db, upgrade_db] commands? I'm not really sure how this works, but it seems like the script_location is a path that needs to work from the location from which the migration is executed. I can see how this works when you run it from inside the repo, but I'm not sure whether this will be the same for the app installed from the package.

The RPM can indeed be installed, and I tested that init_db, upgrade_db, and init_alembic can all successfully load the migrations.

The reason this works is because script_location accepts either a filesystem path (here alembic.ini specifies script_location=resultsdb/alembic, which is what gets used when you run alembic revision in a git checkout) or it accepts a pkg_resources-style resource directory to load from (this works without any alembic.ini when the resultsdb CLI programmatically constructs its config in get_alembic_config()).

https://stackoverflow.com/questions/25798447/alembic-example-of-using-package-resource-as-script-location