activerecord-database-views 0.0.1.pre2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTc2MzdlM2NhM2RlM2NmZWJjMDAwZWMwYzA0NzRmMjA1ZjlhMjRmZQ==
4
+ YmUzNjA2OTc4NGVmMDg3MmQwMDQ5YWYzOGFiZGJlZjRiYjcwNzRhMg==
5
5
  data.tar.gz: !binary |-
6
- MGMxY2QxNDA0MGJkYTkzYmQ0ODk4YWE0ZTkyMmVmMjQ3MWUzOTc0OQ==
6
+ ZmJmMDk3MGZhZWFkYjAzYzdlNzlmNDkxMzU1OWY4ODU5MjEyMmI0Yg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDhmNjQ2N2MyMGUzMTNkYzU3YzkyMDQ1ZDE2M2I4Yzc3YTk1NjQwZDk3NGYx
10
- ODljZTJhZGIwZDNmNDRkZTQ2NGE5M2Q0M2VjNGFmZDRkNjNkOWYxOTEwNzE1
11
- ZjM0NWQ0NTcxOGU3OWIzNmZiMDdhYzlkZjNiMWQwYjZmOTRmMGE=
9
+ YTAxZDQ0ZjUwYmIxYmM0MDJlOTI3NTliYzY3YTg3NzEyZjQwZjMwZWZjNGFj
10
+ NDllZWEyYzhiYmFhNmE4NmUzMWU3ZmY1OTZmYmM5YmRhYjY4YWU2ZTZjZmQ1
11
+ ODM4ZjA1OTJmOWQwYWMxNDIyNzFlZGYzNThiM2Y3NmY2NDBhOTc=
12
12
  data.tar.gz: !binary |-
13
- YzlhMjdjZWYxZjFkY2ZlMTBiOWI2MTRmODE2Y2JjNzAwYWJiMDYyYjM4Nzk4
14
- ZDllYzI4ZGVkMmYwNTA3ZjkwNmEwMTE4YjgzOTc1ODRmNThkZWYyZWFjNTBj
15
- NTBhMzIwM2MzOTU1NjY1MjBkY2E0NTJjYzRjOTUxMmJhYTM5MTQ=
13
+ YWJmMmJiZDA1ZDcwY2E5ZGRlYmU2MjBkZWVmMmMzZjRiZDE5MTdjNmY5NTky
14
+ ZmIyYWFlYjRmMzExOWU2YTU4MTFlYmEwMGRiMzliZjg2ZTc3ZmFlMzBjODMy
15
+ MmUzMjAwMjg2NTBlOTg1ZmE2YmNkN2I3NzNiNGY1YmRjNzI4ZDY=
@@ -1,136 +1,20 @@
1
- class ActiveRecord::Base
2
- MISSING_RELATION_REGEX = /relation \"(.*)\" does not exist/
3
- class << self
1
+ require 'activerecord-database-views/view'
2
+ require 'activerecord-database-views/view_collection'
4
3
 
5
- #===========================================
6
- #============= Functions ===================
7
- #===========================================
8
-
9
- def reload_functions!
10
- drop_functions!
11
- load_functions!
12
- end
13
-
14
- def without_functions(&block)
15
- drop_functions!
16
- block.call
17
- load_functions!
18
- end
19
-
20
- def drop_functions!
21
- init_functions
22
- @functions_to_load.keys.each do |function_name|
23
- path = @functions_to_load[function_name]
24
- sql = File.read(path)
25
- function_name = sql.match(/FUNCTION (.*) RETURNS/)[1]
26
- connection.execute("DROP FUNCTION IF EXISTS #{function_name} CASCADE;")
27
- end
28
- end
29
-
30
- def load_functions!
31
- init_functions
32
- while @functions_to_load.present?
33
- function_name = @functions_to_load.keys.first
34
- load_function(function_name)
35
- end
36
- end
37
-
38
- private
39
-
40
- def load_function(function_name)
41
- path = @functions_to_load[function_name]
42
- Rails.logger.info "\nLOADING VIEW: #{function_name} (#{path}) \n#{'-' * 100}"
43
- sql = File.read(path)
44
- connection.execute(sql)
45
- @functions_to_load.delete(function_name)
46
- Rails.logger.info "\nFUNCTION LOADED SUCCESSFULLY"
47
- end
48
-
49
- def init_functions
50
- @functions_to_load = Dir.glob("db/functions/**/*.sql").inject({}) do |acc, path|
51
- acc[File.basename(path, '.sql')] = Rails.root.join(path); acc
52
- end
53
- end
54
-
55
- #=======================================
56
- #============= Views ===================
57
- #=======================================
58
-
59
- public
60
-
61
- def reload_views!
62
- reload_functions!
63
- drop_views!
64
- load_views!
65
- end
66
-
67
- def without_views(&block)
68
- drop_views!
69
- block.call
70
- load_views!
71
- end
72
-
73
- def drop_views!
74
- init_views
75
- @views_to_load.keys.each do |view_name|
76
- connection.execute("DROP VIEW IF EXISTS #{view_name} CASCADE;")
77
- end
78
- end
79
-
80
- #This is weird, but some views were imported as tables during one import...
81
- def drop_views_as_tables!
82
- init_views
83
- @views_to_load.keys.each do |view_name|
84
- begin
85
- connection.execute("DROP TABLE IF EXISTS #{view_name} CASCADE;")
86
- rescue ActiveRecord::StatementInvalid => exc
87
- if exc.message[%{"#{view_name}" is not a table}]
88
- connection.execute("DROP VIEW IF EXISTS #{view_name} CASCADE;")
89
- else
90
- raise exc
91
- end
92
- end
93
- end
94
- end
95
-
96
- def load_views!
97
- init_views
98
- while @views_to_load.present?
99
- view_name = @views_to_load.keys.first
100
- load_view(view_name)
101
- end
102
- end
103
-
104
- private
105
-
106
- def load_view(view_name)
107
- begin
108
- path = @views_to_load[view_name]
109
-
110
- Rails.logger.info "\nLOADING VIEW: #{view_name} (#{path}) \n#{'-' * 100}"
111
- sql = File.read(path)
112
- Rails.logger.info("\n\n #{sql}")
113
-
114
- connection.execute("CREATE OR REPLACE VIEW #{view_name} AS #{sql};")
115
- Rails.logger.info "\nVIEW LOADED SUCCESSFULLY"
4
+ module ActiveRecord::DatabaseViews
5
+ def self.views
6
+ ViewCollection.new
7
+ end
116
8
 
117
- @views_to_load.delete(view_name)
118
- rescue ActiveRecord::StatementInvalid => exc
119
- related_view_name = exc.message.scan(MISSING_RELATION_REGEX).flatten.first
120
- if @views_to_load[related_view_name].present?
121
- load_view(related_view_name)
122
- retry
123
- else
124
- raise exc
125
- end
126
- end
127
- end
9
+ def self.without
10
+ views.drop!
11
+ yield if block_given?
12
+ views.load!
13
+ end
128
14
 
129
- def init_views
130
- @views_to_load = Dir.glob("db/views/**/*.sql").inject({}) do |acc, path|
131
- acc[File.basename(path, '.sql')] = Rails.root.join(path); acc
132
- end
15
+ def self.reload!
16
+ ActiveRecord::Base.transaction do
17
+ without
133
18
  end
134
-
135
19
  end
136
20
  end
@@ -1,7 +1,7 @@
1
1
  module Activerecord
2
2
  module Database
3
3
  module Views
4
- VERSION = "0.0.1.pre2"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,35 @@
1
+ module ActiveRecord::DatabaseViews
2
+ class View
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def drop!
10
+ call_sql!("DROP VIEW IF EXISTS #{name} CASCADE;")
11
+ end
12
+
13
+ def load!
14
+ call_sql!("CREATE OR REPLACE VIEW #{name} AS #{sql};")
15
+ end
16
+
17
+ def name
18
+ File.basename(path, '.sql')
19
+ end
20
+
21
+ private
22
+
23
+ def full_path
24
+ Rails.root.join(path)
25
+ end
26
+
27
+ def sql
28
+ File.read(full_path)
29
+ end
30
+
31
+ def call_sql!(sql)
32
+ ActiveRecord::Base.connection.execute(sql)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ module ActiveRecord::DatabaseViews
2
+ class ViewCollection
3
+ MISSING_RELATION_REGEX = /relation \"(.*)\" does not exist/
4
+
5
+ include Enumerable
6
+
7
+ attr_reader :views
8
+
9
+ def initialize
10
+ @views = view_paths.map { |path| View.new(path) }
11
+ end
12
+
13
+ def drop!
14
+ views.each(&:drop!)
15
+ end
16
+
17
+ def each
18
+ views.each { |view| yield view }
19
+ end
20
+
21
+ def load!
22
+ while views.present?
23
+ load_view(views.first)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def load_view(view)
30
+ begin
31
+ view.load! and views.delete(view)
32
+ rescue ActiveRecord::StatementInvalid => exc
33
+ if (related_view = retrieve_related_view(exc))
34
+ load_view(related_view)
35
+ retry
36
+ else
37
+ raise exc
38
+ end
39
+ end
40
+ end
41
+
42
+ def retrieve_related_view(exception)
43
+ related_view_name = exception.message.scan(MISSING_RELATION_REGEX).flatten.first
44
+ return false if related_view_name.blank?
45
+ find { |view| view.name == related_view_name }
46
+ end
47
+
48
+ def view_paths
49
+ Dir.glob("db/views/**/*.sql")
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-database-views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - stevo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
11
+ date: 2013-12-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Facilitates storing and reloading of DB views within Rails applications
14
14
  email:
@@ -19,6 +19,8 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/activerecord-database-views.rb
21
21
  - lib/activerecord-database-views/version.rb
22
+ - lib/activerecord-database-views/view.rb
23
+ - lib/activerecord-database-views/view_collection.rb
22
24
  homepage: https://github.com/stevo/activerecord-database-views
23
25
  licenses: []
24
26
  metadata: {}
@@ -33,9 +35,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
35
  version: '0'
34
36
  required_rubygems_version: !ruby/object:Gem::Requirement
35
37
  requirements:
36
- - - ! '>'
38
+ - - ! '>='
37
39
  - !ruby/object:Gem::Version
38
- version: 1.3.1
40
+ version: '0'
39
41
  requirements: []
40
42
  rubyforge_project: ! '[none]'
41
43
  rubygems_version: 2.1.10