activerecord-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ module ActiveRecord
2
+ module View
3
+ module Introspection
4
+ class SQLite3 < Abstract
5
+ SQL_DEFINITION = /\ACREATE\s+VIEW.+?\bAS\b\s+(.+)\z/i
6
+
7
+ def process_view_definition(result)
8
+ return result if result.blank?
9
+
10
+ result[SQL_DEFINITION, 1]
11
+ end
12
+
13
+ def fetch_view_definition_query(view_name, **options)
14
+ master_table.project(sql_definition).where(type_is_view.and(view_name_eq(view_name)))
15
+ end
16
+
17
+ # @!attribute [r] master_table
18
+ # @return [Arel::Table]
19
+ def master_table
20
+ @_master_table ||= Arel::Table.new 'sqlite_master', self
21
+ end
22
+
23
+ def sql_definition
24
+ master_table[:sql]
25
+ end
26
+
27
+ def view_name_eq(name)
28
+ master_table[:name].eq(name)
29
+ end
30
+
31
+ # @return [Arel::Nodes::Equality]
32
+ def type_is_view
33
+ master_table[:type].eq('view')
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ module ActiveRecord
2
+ module View
3
+ module Introspection
4
+ class ViewDefinition
5
+ include Virtus.value_object strict: true
6
+
7
+ values do
8
+ attribute :name, String
9
+ attribute :definition, String
10
+ attribute :adapter, String
11
+ attribute :materialized, Boolean, default: false
12
+ end
13
+
14
+ alias_method :sql, :definition
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveRecord
2
+ module View
3
+ module MaterializedViewMethods
4
+ extend ActiveSupport::Concern
5
+
6
+ REFRESH_QUERY = %[REFRESH MATERIALIZED VIEW %s]
7
+
8
+ module ClassMethods
9
+ def refresh_view_query
10
+ sprintf REFRESH_QUERY, quoted_table_name
11
+ end
12
+
13
+ def refresh_view!
14
+ connection.execute refresh_view_query
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module ActiveRecord
2
+ module View
3
+ # @api private
4
+ module ReadOnly
5
+ extend ActiveSupport::Concern
6
+
7
+ READONLY_CLASS_METHODS = %i[destroy destroy_all delete delete_all update_all update]
8
+
9
+ included do
10
+ before_destroy :indestructibly_readonly!
11
+
12
+ relation.class.prepend ActiveRecord::View::ReadOnly::ClassMethods
13
+ end
14
+
15
+ def readonly?
16
+ true
17
+ end
18
+
19
+ def delete
20
+ raise ActiveRecord::ReadOnlyRecord
21
+ end
22
+
23
+ protected
24
+ def indestructibly_readonly!
25
+ raise ActiveRecord::ReadOnlyRecord
26
+ end
27
+
28
+ module ClassMethods
29
+ def attempt_update(*args)
30
+ raise ActiveRecord::ReadOnlyRecord, "This is a read-only view"
31
+ end
32
+
33
+ READONLY_CLASS_METHODS.each do |m|
34
+ alias_method m, :attempt_update
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveRecord
2
+ module View
3
+ # Namespace for schema / migration logic
4
+ module Schema
5
+ %w[abstract].each do |mod|
6
+ require "activerecord/view/schema/#{mod}"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveRecord
2
+ module View
3
+ module Utility
4
+ # Strip newlines and excess whitespace from SQL statements
5
+ module_function
6
+
7
+ def cleanup(raw_sql)
8
+ raw_sql.strip.gsub /\n\s+/, ' '
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module View
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveRecord
2
+ module View
3
+ module ViewMethods
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ # Get the view's current definition query.
8
+ #
9
+ # @param [Boolean] raise_error
10
+ # @return [String, nil]
11
+ def view_definition(raise_error: false)
12
+ ActiveRecord::View.definition_for self, raise_error: raise_error
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,262 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexa Grey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: dux
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: virtus
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.9'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.9'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: combustion
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: database_cleaner
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: pry
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: mysql2
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: sqlite3
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ - !ruby/object:Gem::Dependency
174
+ name: pg
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ - !ruby/object:Gem::Dependency
188
+ name: simplecov
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ type: :development
195
+ prerelease: false
196
+ version_requirements: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ description: SQL views with ActiveRecord
202
+ email:
203
+ - devel@mouse.vc
204
+ executables: []
205
+ extensions: []
206
+ extra_rdoc_files: []
207
+ files:
208
+ - ".gitignore"
209
+ - ".rspec"
210
+ - ".travis.yml"
211
+ - CODE_OF_CONDUCT.md
212
+ - Gemfile
213
+ - LICENSE.txt
214
+ - README.md
215
+ - Rakefile
216
+ - activerecord-view.gemspec
217
+ - bin/console
218
+ - bin/setup
219
+ - lib/activerecord-view.rb
220
+ - lib/activerecord/view.rb
221
+ - lib/activerecord/view/error.rb
222
+ - lib/activerecord/view/integration.rb
223
+ - lib/activerecord/view/integration/command_recorder_methods.rb
224
+ - lib/activerecord/view/integration/model_methods.rb
225
+ - lib/activerecord/view/integration/schema_methods.rb
226
+ - lib/activerecord/view/introspection.rb
227
+ - lib/activerecord/view/introspection/abstract.rb
228
+ - lib/activerecord/view/introspection/mysql.rb
229
+ - lib/activerecord/view/introspection/postgres.rb
230
+ - lib/activerecord/view/introspection/sqlite3.rb
231
+ - lib/activerecord/view/introspection/view_definition.rb
232
+ - lib/activerecord/view/materialized_view_methods.rb
233
+ - lib/activerecord/view/read_only.rb
234
+ - lib/activerecord/view/schema.rb
235
+ - lib/activerecord/view/utility.rb
236
+ - lib/activerecord/view/version.rb
237
+ - lib/activerecord/view/view_methods.rb
238
+ homepage: https://github.com/scryptmouse/activerecord-view
239
+ licenses: []
240
+ metadata: {}
241
+ post_install_message:
242
+ rdoc_options: []
243
+ require_paths:
244
+ - lib
245
+ required_ruby_version: !ruby/object:Gem::Requirement
246
+ requirements:
247
+ - - "~>"
248
+ - !ruby/object:Gem::Version
249
+ version: '2.1'
250
+ required_rubygems_version: !ruby/object:Gem::Requirement
251
+ requirements:
252
+ - - ">="
253
+ - !ruby/object:Gem::Version
254
+ version: '0'
255
+ requirements: []
256
+ rubyforge_project:
257
+ rubygems_version: 2.4.5
258
+ signing_key:
259
+ specification_version: 4
260
+ summary: SQL views with ActiveRecord
261
+ test_files: []
262
+ has_rdoc: