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

Order views by Postgres oid #117

Merged
merged 1 commit into from
Jan 1, 2016
Merged

Order views by Postgres oid #117

merged 1 commit into from
Jan 1, 2016

Conversation

derekprior
Copy link
Contributor

Our previous strategy of:

  1. Relying on postgres to give us the views from pg_views and
    pg_matviews in the correct order
  2. Always dumping materialized views last

was incorrect on both counts. Those tables have no specific ordering so
the results can come back in any order. Also, we can have views that
depend on materialized views.

We need to get all of the views - materialized and otherwise - from a
single query and we need them to be ordered in the same order they were
created.

I inspected the definitions of pg_views and pg_matviews and noticed
they are both built from pg_class. Querying on that table gives us a way
to get both the views and materialized views in a single query. Ordering
by 'oid' is the simplest way to order them by insertion order. Dropping
a view that depends on a child view also requires dropping that child
view, so ordering in this manner should be sufficient.

This technically means we don't need the connection decorator I
introduced in the previous change, but we know we will be using that
functionality very soon in another change, so I'm leaving it be for now.

I ran the improved test of #views in the postgres_spec hundreds of
times and got a consistent, valid order every time.

"parents",
"children",
"people",
"people_with_names"

Choose a reason for hiding this comment

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

Put a comma after the last item of a multiline array.

Our previous strategy of:

1. Relying on postgres to give us the views from `pg_views` and
   `pg_matviews` in the correct order
2. Always dumping materialized views last

was incorrect on both counts. Those tables have no specific ordering so
the results can come back in any order. Also, we can have views that
depend on materialized views.

We need to get all of the views - materialized and otherwise - from a
single query and we need them to be ordered in the same order they were
created.

I inspected the definitions of `pg_views` and `pg_matviews` and noticed
they are both built from pg_class. Querying on that table gives us a way
to get both the views and materialized views in a single query. Ordering
by 'oid' is the simplest way to order them by insertion order. Dropping
a view that depends on a child view also requires dropping that child
view, so ordering in this manner should be sufficient.

This technically means we don't need the connection decorator I
introduced in the previous change, but we know we will be using that
functionality very soon in another change, so I'm leaving it be for now.

I ran the improved test of `#views` in the `postgres_spec` hundreds of
times and got a consistent, valid order every time.
@calebhearth
Copy link
Contributor

Looks great. Good find Derek. Thanks for looking into it.

On Dec 31, 2015, at 4:11 PM, Derek Prior notifications@github.com wrote:

Our previous strategy of:

Relying on postgres to give us the views from pg_views and pg_matviews in the correct order
Always dumping materialized views last
was incorrect on both counts. Those tables have no specific ordering so
the results can come back in any order. Also, we can have views that
depend on materialized views.

We need to get all of the views - materialized and otherwise - from a
single query and we need them to be ordered in the same order they were
created.

I inspected the definitions of pg_views and pg_matviews and noticed
they are both built from pg_class. Querying on that table gives us a way
to get both the views and materialized views in a single query. Ordering
by 'oid' is the simplest way to order them by insertion order. Dropping
a view that depends on a child view also requires dropping that child
view, so ordering in this manner should be sufficient.

This technically means we don't need the connection decorator I
introduced in the previous change, but we know we will be using that
functionality very soon in another change, so I'm leaving it be for now.

I ran the improved test of #views in the postgres_spec hundreds of
times and got a consistent, valid order every time.

You can view, comment on, or merge this pull request online at:

#117

Commit Summary

Order views by Postgres oid
File Changes

M lib/scenic/adapters/postgres/views.rb (35)
M spec/scenic/adapters/postgres/views_spec.rb (34)
M spec/scenic/adapters/postgres_spec.rb (44)
Patch Links:

https://github.com/thoughtbot/scenic/pull/117.patch
https://github.com/thoughtbot/scenic/pull/117.diff

Reply to this email directly or view it on GitHub.

@derekprior derekprior merged commit fa81d50 into master Jan 1, 2016
@derekprior derekprior deleted the dp-view-ordering branch January 1, 2016 14:46
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.

None yet

3 participants