viewy 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2131a4a4481e70cd523aa43db9a3be81d62991f4
4
- data.tar.gz: 8b143540aad962f88e4f60d0a145dc832916e6a0
3
+ metadata.gz: 4f4635fbdb32ad47ab83ca1d5dfefd749cff4372
4
+ data.tar.gz: dbf922b26c5ccd6c53d1dcf0a0f784a390212671
5
5
  SHA512:
6
- metadata.gz: c0af07200b730212c7aad3d55096bf7c4cd3dfdb8b7305b8a7a2ae0fdec389f221be51a20b8eb9aa2d27500c46036cd9f69aaa472cc7aa84b47bd77f21ce6f04
7
- data.tar.gz: 6f0933b02568ddfbf3a024129359f423d24e28771a9cddff370488ba2e705285ce9bbc591b98572a678d1f752d5fe6e6395c186b9cb9222920ca9d10e1caed9d
6
+ metadata.gz: 6573e51501306fd23d8fa4b8aaf329666124522ff8dbc19e45ad6e747c6910cd4224c2f5bac2665c480f29b2cf399bfadb1ba4e5b4426e80cde675ec183be31e
7
+ data.tar.gz: ce10805be334f3faf47494eb971e6134ad1194664c5d409a4584dfa4b02a0a130615196e2de23634a8828972abca012098204610777c2a01bd3b7b0eed61490c
data/lib/viewy.rb CHANGED
@@ -29,6 +29,16 @@ module Viewy
29
29
  view_refresher.refresh_materialized_view('all_view_dependencies')
30
30
  end
31
31
 
32
+ # @return [Array<String>] the ordered names of all views in the system in safe dependency order
33
+ def self.view_names_in_dependency_order
34
+ Viewy::DependencyManagement::ViewSorter.new.sorted_views
35
+ end
36
+
37
+ # @return [Array<String>] the ordered names of the materialized views in the system in safe dependency order
38
+ def self.materialized_view_names_in_dependency_order
39
+ Viewy::DependencyManagement::ViewSorter.new.sorted_materialized_views
40
+ end
41
+
32
42
  # The connection used by viewy to manage views
33
43
  #
34
44
  # @return [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] An ActiveRecord connection
@@ -12,8 +12,7 @@ module Viewy
12
12
  #
13
13
  # @return [PG::Result] the result of the refresh statement on the materialized view
14
14
  def refresh!
15
- view_dep = Viewy::Models::MaterializedViewDependency.find(table_name)
16
- view_dep.view_dependencies.each do |view_dependency|
15
+ sorted_view_dependencies.each do |view_dependency|
17
16
  ActiveRecord::Base.connection.execute("REFRESH MATERIALIZED VIEW #{view_dependency}")
18
17
  end
19
18
  refresh_without_dependencies!
@@ -27,6 +26,14 @@ module Viewy
27
26
  def refresh_without_dependencies!
28
27
  ActiveRecord::Base.connection.execute("REFRESH MATERIALIZED VIEW #{table_name}")
29
28
  end
29
+
30
+ # Provides an array of sorted view depenedencies
31
+ #
32
+ # @return [Array<String>]
33
+ def sorted_view_dependencies
34
+ view_dep = Viewy::Models::MaterializedViewDependency.find(table_name)
35
+ Viewy::DependencyManagement::ViewSorter.new.sorted_materialized_view_subset(view_names: view_dep.view_dependencies)
36
+ end
30
37
  end
31
38
  end
32
39
  end
@@ -1,3 +1,4 @@
1
+ require_relative 'dependency_management/view_sorter'
1
2
  require_relative 'dependency_management/view_refresher'
2
3
 
3
4
  module Viewy
@@ -7,22 +7,18 @@ module Viewy
7
7
  # @!attribute connection [r]
8
8
  # @return [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] the underlying Postgres connection
9
9
  class ViewRefresher
10
- include TSort
11
-
12
10
  attr_reader :connection
13
11
 
14
- alias_method :ordered_views, :tsort
15
- private :tsort, :ordered_views
16
-
17
12
  # @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] An ActiveRecord connection
18
13
  # to a Postgres Database
19
14
  def initialize(connection)
20
15
  @connection = connection
16
+ @sorter = Viewy::DependencyManagement::ViewSorter.new
21
17
  end
22
18
 
23
19
  # This method will refresh all materialized views in order of dependency
24
20
  def refresh_all_materialized_views
25
- ordered_views.each do |view_name|
21
+ @sorter.sorted_materialized_views.each do |view_name|
26
22
  refresh_materialized_view(view_name)
27
23
  end
28
24
  end
@@ -34,32 +30,6 @@ module Viewy
34
30
  connection.execute(refresh_sql(view_name))
35
31
  end
36
32
 
37
- # Note: this method memoizes the result of the first call
38
- #
39
- # @return [Hash] a hash with materialized view names as keys and their dependencies as an array of names
40
- private def views
41
- @views ||= generate_view_hash
42
- end
43
-
44
- # @return [Hash] a hash with materialized view names as keys and their dependencies as an array of names
45
- private def generate_view_hash
46
- views = Viewy::Models::MaterializedViewDependency.all.select(&:materialized_view).map do |dep|
47
- [dep.view_name, dep.view_dependencies]
48
- end
49
- Hash[views]
50
- end
51
-
52
- private def tsort_each_node
53
- views.each_key do |view|
54
- yield view
55
- end
56
- end
57
-
58
- # @param name [String] the name of a materialized view
59
- private def tsort_each_child(view, &block)
60
- views[view].each(&block)
61
- end
62
-
63
33
  # @param name [String] the name of a materialized view
64
34
  # @return [String] the SQL statement needed to refresh the passed view
65
35
  def refresh_sql(name)
@@ -0,0 +1,65 @@
1
+ module Viewy
2
+ module DependencyManagement
3
+ class ViewSorter
4
+ include TSort
5
+
6
+ private :tsort
7
+
8
+ # @return [Array<String>] the ordered names of all views in the system in safe dependency order
9
+ def sorted_views
10
+ @views = generate_view_hash(Viewy::Models::ViewDependency.all)
11
+ tsort
12
+ end
13
+
14
+ # @return [Array<String>] the ordered names of the materialized views in the system in safe dependency order
15
+ def sorted_materialized_views
16
+ @views = generate_materialized_view_hash(Viewy::Models::MaterializedViewDependency.all)
17
+ tsort
18
+ end
19
+
20
+ # @return [Array<String>] the ordered names of the subset of views in the system in safe dependency order
21
+ def sorted_view_subset(view_names:)
22
+ @views = generate_view_hash(Viewy::Models::ViewDependency.where(view_name: view_names))
23
+ tsort
24
+ end
25
+
26
+ # @return [Array<String>] the ordered names of the subset of materialized views in safe dependency order
27
+ def sorted_materialized_view_subset(view_names:)
28
+ @views = generate_materialized_view_hash(Viewy::Models::MaterializedViewDependency.where(view_name: view_names))
29
+ tsort
30
+ end
31
+
32
+ # @return [Hash] a hash with materialized view names as keys and their dependencies as an array of names
33
+ private def views
34
+ @views
35
+ end
36
+
37
+ # @return [Hash] a hash with all view names as keys and their dependencies as an array of names
38
+ private def generate_view_hash(view_collection)
39
+ views = view_collection.map do |dep|
40
+ [dep.view_name, dep.view_dependencies]
41
+ end
42
+ Hash[views]
43
+ end
44
+
45
+ # @return [Hash] a hash with materialized view names as keys and their dependencies as an array of names
46
+ private def generate_materialized_view_hash(view_collection)
47
+ views = view_collection.select(&:materialized_view).map do |dep|
48
+ [dep.view_name, dep.view_dependencies]
49
+ end
50
+ Hash[views]
51
+ end
52
+
53
+ private def tsort_each_node
54
+ views.each_key do |view|
55
+ yield view
56
+ end
57
+ end
58
+
59
+ # @param name [String] the name of a materialized view
60
+ private def tsort_each_child(view, &block)
61
+ views[view].each(&block)
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/viewy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Viewy
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viewy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emerson Huitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-05 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: shoulda-matchers
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '3.0'
69
69
  description: Viewy is a tool for managing views and materialized views in Postgres
70
70
  from within a rails app.
71
71
  email:
@@ -86,6 +86,7 @@ files:
86
86
  - lib/viewy/acts_as_view.rb
87
87
  - lib/viewy/dependency_management.rb
88
88
  - lib/viewy/dependency_management/view_refresher.rb
89
+ - lib/viewy/dependency_management/view_sorter.rb
89
90
  - lib/viewy/dependency_manager.rb
90
91
  - lib/viewy/engine.rb
91
92
  - lib/viewy/models.rb