activerecord-database-views 0.0.1.pre1

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2Q2MjBkYzUzYjBkOTc2N2I3ZTBlNDVlZjUxMDY0ZDY5MGQyZjNhNg==
5
+ data.tar.gz: !binary |-
6
+ NTdmY2Q2NTg0N2UzYWRmNTFmNmE3NTRlMTg2NGI0MDJiMDgyYjU3Yg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjVjMDAzZjRkNWRhMTQ3MGFjOWE4OWU5NWI1ZTc2OWE1YWNmNjE5M2FlNmI5
10
+ YmU0MGU1M2FmZjg0YTNhOTBkYTllNzIxMTA4ZDNlYzc3YjU5NWMyMDBiN2Vh
11
+ ZTMzNDg2NmRlZDYyYmUzNDJlZDBjYWJjNzU1YTc3ZWQ3YjZiNzE=
12
+ data.tar.gz: !binary |-
13
+ M2RkODYxYjFmZDU4YzBkOWQ5MzM0M2RjOWI5MjY3YzBjODJlOTYzNzFmYjlm
14
+ MDJhMWY0ZmI1NTc2NmFiNTZhM2QyNDk0MzY4ZGY3Y2Q2OWRhZmFjZWUxN2U5
15
+ MmQ1ZjRjOTBmMjUxMDQwNzU2MmJmZGNmZDA1NGM4MDRhOTNmMWU=
@@ -0,0 +1,136 @@
1
+ class ActiveRecord::Base
2
+ MISSING_RELATION_REGEX = /relation \"(.*)\" does not exist/
3
+ class << self
4
+
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"
116
+
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
128
+
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
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,7 @@
1
+ module Activerecord
2
+ module Database
3
+ module Views
4
+ VERSION = "0.0.1.pre1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-database-views
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre1
5
+ platform: ruby
6
+ authors:
7
+ - stevo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Facilitates storing and reloading of DB views within Rails applications
14
+ email:
15
+ - blazej.kosmowski@selleo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/activerecord-database-views.rb
21
+ - lib/activerecord-database-views/version.rb
22
+ homepage: https://github.com//activerecord-database-views
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>'
37
+ - !ruby/object:Gem::Version
38
+ version: 1.3.1
39
+ requirements: []
40
+ rubyforge_project: ! '[none]'
41
+ rubygems_version: 2.1.10
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Facilitates reloading of DB views
45
+ test_files: []