-
-
Notifications
You must be signed in to change notification settings - Fork 240
Add topological sorting for dumped views using TSort #337
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,34 @@ | |||||||||
| module Scenic | ||||||||||
| # @api private | ||||||||||
| module SchemaDumper | ||||||||||
| # A hash to do topological sort | ||||||||||
| class TSortableHash < Hash | ||||||||||
| include TSort | ||||||||||
|
|
||||||||||
| alias tsort_each_node each_key | ||||||||||
| def tsort_each_child(node, &block) | ||||||||||
| fetch(node).each(&block) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| # Query for the dependencies between views | ||||||||||
| DEPENDENT_SQL = <<~SQL.freeze | ||||||||||
| SELECT distinct dependent_ns.nspname AS dependent_schema | ||||||||||
| , dependent_view.relname AS dependent_view | ||||||||||
| , source_ns.nspname AS source_schema | ||||||||||
| , source_table.relname AS source_table | ||||||||||
| FROM pg_depend | ||||||||||
| JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid | ||||||||||
| JOIN pg_class as dependent_view ON pg_rewrite.ev_class = dependent_view.oid | ||||||||||
| JOIN pg_class as source_table ON pg_depend.refobjid = source_table.oid | ||||||||||
| JOIN pg_namespace dependent_ns ON dependent_ns.oid = dependent_view.relnamespace | ||||||||||
| JOIN pg_namespace source_ns ON source_ns.oid = source_table.relnamespace | ||||||||||
| WHERE dependent_ns.nspname = ANY (current_schemas(false)) AND source_ns.nspname = ANY (current_schemas(false)) | ||||||||||
| AND source_table.relname != dependent_view.relname | ||||||||||
| AND source_table.relkind IN ('m', 'v') AND dependent_view.relkind IN ('m', 'v') | ||||||||||
| ORDER BY dependent_view.relname; | ||||||||||
| SQL | ||||||||||
|
|
||||||||||
| def tables(stream) | ||||||||||
| super | ||||||||||
| views(stream) | ||||||||||
|
|
@@ -22,9 +50,40 @@ def views(stream) | |||||||||
| private | ||||||||||
|
|
||||||||||
| def dumpable_views_in_database | ||||||||||
| @dumpable_views_in_database ||= Scenic.database.views.reject do |view| | ||||||||||
| if @ordered_dumpable_views_in_database | ||||||||||
| return @ordered_dumpable_views_in_database | ||||||||||
| end | ||||||||||
|
|
||||||||||
| existing_views = Scenic.database.views.reject do |view| | ||||||||||
| ignored?(view.name) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| @ordered_dumpable_views_in_database = | ||||||||||
| tsorted_views(existing_views.map(&:name)).map do |view_name| | ||||||||||
| existing_views.find { |ev| ev.name == view_name } | ||||||||||
|
msorc marked this conversation as resolved.
|
||||||||||
| end.compact | ||||||||||
| end | ||||||||||
|
|
||||||||||
| # When dumping the views, their order must be topologically | ||||||||||
| # sorted to take into account dependencies | ||||||||||
| def tsorted_views(views_names) | ||||||||||
| views_hash = TSortableHash.new | ||||||||||
|
|
||||||||||
| ::Scenic.database.execute(DEPENDENT_SQL).each do |relation| | ||||||||||
| source_v = relation["source_table"] | ||||||||||
| dependent = relation["dependent_view"] | ||||||||||
|
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Needs schema here in our app otherwise order is wrong. |
||||||||||
| views_hash[dependent] ||= [] | ||||||||||
| views_hash[source_v] ||= [] | ||||||||||
| views_hash[dependent] << source_v | ||||||||||
| views_names.delete(source_v) | ||||||||||
| views_names.delete(dependent) | ||||||||||
| end | ||||||||||
|
|
||||||||||
| # after dependencies, there might be some views left | ||||||||||
| # that don't have any dependencies | ||||||||||
| views_names.sort.each { |v| views_hash[v] = [] } | ||||||||||
|
|
||||||||||
| views_hash.tsort | ||||||||||
| end | ||||||||||
|
|
||||||||||
| unless ActiveRecord::SchemaDumper.private_instance_methods(false).include?(:ignored?) | ||||||||||
|
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Iterating over result does not need this order so this line can be removed.