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

Add conditional migration for native matview VarsPerHistory #73

Merged
merged 38 commits into from
Feb 3, 2021

Conversation

rod-glover
Copy link
Contributor

@rod-glover rod-glover commented Jan 19, 2021

Resolves #72

TODO:

  • Expose all views and matviews in pycds (enable, e.g., import VarsPerHistory from pycds).
  • Add tests for VarsPerHistory function/content
  • Add index to VarsPerHistory
  • Make this migration conditional on Postgres version (or just catch the error when attempting to add/drop the matview)
  • Add drop-if-exists functionality for views, matviews

@jameshiebert
Copy link
Contributor

Are you on the failing tests here?

@rod-glover
Copy link
Contributor Author

rod-glover commented Jan 22, 2021

@jameshiebert , my clever idea of importing items from deeper packages into the top-level package created a circular import. I think that fixing that won't be simple; just converting to "absolute" imports in the lower level packages does not work. I'm inclined to do without, given that I am at this point thinking about doing this quite soon. But if you have any other knowledge to add, I'd be happy to hear it.

Copy link
Contributor

@jameshiebert jameshiebert left a comment

Choose a reason for hiding this comment

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

Yeah, you definitely can't import the pycds module from inside pycds's own __init__.py 😉 If you really want to go down that path, take everything that's defined in __init__.py and define it somewhere else to be imported. But generally, __init__.py should be super conservative.

@rod-glover
Copy link
Contributor Author

Yeah, I just figured out that moving the contents of __init__.py elsewhere is the answer. Thanks for confirming.

Copy link
Contributor

@jameshiebert jameshiebert left a comment

Choose a reason for hiding this comment

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

Still in progress, but here's a dump of the first few comments.

Copy link
Contributor

@jameshiebert jameshiebert left a comment

Choose a reason for hiding this comment

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

This mostly looks good to me, modulo a few minutes comments here and there. I didn't read the testing code super closely, but I did a quick coverage test and most of the modules score pretty high (we do need to work on some upgrades to our testing packages, but I filed a different issue for that).

Overall looks pretty good to me. Thanks for working on this!

schema=schema_name,
)
op.drop_native_materialized_view(VarsPerHistory, schema=schema_name)
op.create_table(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the create_table operation be done *regardless of whether the target database supports native matviews? Presumably, the target revision on a downgrade should have the vars_per_history_mv, no matter what.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The downgrade does have the table. It deletes the matview and replaces it with a table.

Copy link
Contributor

Choose a reason for hiding this comment

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

But only inside the if db_supports_matviews(engine): block. I'm arguing that the migration should ensure that table is created always. Whether the database supports matviews or not.

Copy link
Contributor Author

@rod-glover rod-glover Jan 25, 2021

Choose a reason for hiding this comment

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

Terminology: A migration is a sequence of revisions. A revision is like a git commit: it builds on previous revisions. This revision is concerned with the matview. A previous revision created the table. So all this revision requires is to drop the previously created table and replace it with the matview. The downgrade does the reverse.

@rod-glover
Copy link
Contributor Author

rod-glover commented Jan 25, 2021

@jameshiebert , I addressed your comment re. creating table vars_per_history_mv.

I believe we are ready to migrate metnorth now, if we do the following:

  1. Stamp metnorth as revision 522eed334c85, which indicates it has the tables and nothing else.
  2. Manually remove the native matview vars_per_history_mv.
  3. Upgrade to this revision, which will restore the native matview.

I suggest this particular path because it is simpler than also adding facilities to conditionally drop matviews. It's not much of a simplification, but this PR is getting big and complicated and I'd rather not add to it unnecessarily.

Any further comments or feedback? If not, then I can merge this PR and do the above steps.

@jameshiebert
Copy link
Contributor

Looks good @rod-glover . The steps you outline sound good to me, and appear to be a great path forward.

@rod-glover rod-glover merged commit 72a4ef6 into master Feb 3, 2021
@rod-glover rod-glover deleted the i72-matview-VarsPerHistory branch February 4, 2021 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add (conditional?) migration for native matview VarsPerHistory
2 participants