sql_view 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/sql_view/view/view_generator.rb +3 -3
- data/lib/sql_view/version.rb +1 -1
- data/lib/sql_view.rb +24 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a7e04a9a62b624adce19a6462f76ddea7548e82aead8a8e639de633f4fa7279
|
4
|
+
data.tar.gz: a19ed68c58a457aac9aa4e65e38f247ed9fd0176fc9eb4aa8bf5b1a42722f3dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eedc2c396f6d43e3a954889a4f66b6416b71ad2d98fd3bb44e50599069a6791998e873a22ad88f6c80f3add2f70abd3a7d19df86702a745a1e1494836cc54892
|
7
|
+
data.tar.gz: 6cd70bb813ce62ea92d7754601fb57df410b0bb1946bd8c251085d20ce18d2b5125f853bd9a6b7f6bcb538175979b923d49573611a855816c563e5160a92e9c4
|
@@ -14,7 +14,7 @@ module SqlView
|
|
14
14
|
class #{class_name}View < SqlView::Model
|
15
15
|
#{top_code}
|
16
16
|
|
17
|
-
schema
|
17
|
+
schema ->#{schema_code}
|
18
18
|
|
19
19
|
extend_model_with do
|
20
20
|
# sample how you can extend it, similar to regular AR model
|
@@ -62,9 +62,9 @@ FILE
|
|
62
62
|
|
63
63
|
def schema_code
|
64
64
|
if args[0].present?
|
65
|
-
"{ #{args[0]} }"
|
65
|
+
" { #{args[0]} }"
|
66
66
|
else
|
67
|
-
" {
|
67
|
+
" {#{ "\n # ActiveRecord::Relation or SQL\n # for example: User.where(active: true)\n }" }"
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
data/lib/sql_view/version.rb
CHANGED
data/lib/sql_view.rb
CHANGED
@@ -4,9 +4,16 @@ require_relative "./sql_view/statements.rb"
|
|
4
4
|
require "sql_view/version"
|
5
5
|
require "sql_view/railtie"
|
6
6
|
|
7
|
+
#
|
8
|
+
#
|
9
|
+
# TODO for now in a single file
|
10
|
+
#
|
11
|
+
#
|
12
|
+
|
7
13
|
module SqlView
|
8
|
-
|
9
|
-
|
14
|
+
def SqlView.log(message)
|
15
|
+
puts message
|
16
|
+
end
|
10
17
|
|
11
18
|
class Model
|
12
19
|
class_attribute :view, :sql_view_options
|
@@ -16,9 +23,7 @@ module SqlView
|
|
16
23
|
end
|
17
24
|
|
18
25
|
def self.inherited(subclass)
|
19
|
-
# puts subclass
|
20
26
|
subclass.sql_view_options = {}
|
21
|
-
# SqlView.klasses[subclass] = subclass.sql_view
|
22
27
|
end
|
23
28
|
|
24
29
|
def self.view_name=(name)
|
@@ -57,34 +62,40 @@ module SqlView
|
|
57
62
|
@parent = parent
|
58
63
|
end
|
59
64
|
|
60
|
-
def refresh
|
61
|
-
|
62
|
-
|
65
|
+
def refresh(concurrently: false)
|
66
|
+
concurrently_or_not = concurrently ? " CONCURRENTLY " : " "
|
67
|
+
sql = <<-SQL
|
68
|
+
REFRESH#{materialized_or_not}VIEW#{concurrently_or_not}#{parent.view_name};
|
69
|
+
SQL
|
70
|
+
execute(sql)
|
63
71
|
end
|
64
72
|
|
65
73
|
def up
|
66
74
|
view_sql = parent.sql_view_options[:sql_or_proc].call
|
67
75
|
raise "Please configure schema for #{parent} (association or SQL) for the view" if view_sql.to_s == ""
|
68
76
|
sql = <<-SQL
|
69
|
-
CREATE
|
77
|
+
CREATE#{materialized_or_not}VIEW #{parent.view_name} AS
|
70
78
|
#{view_sql.respond_to?(:to_sql) ? view_sql.to_sql : view_sql };
|
71
79
|
SQL
|
72
|
-
|
73
|
-
ActiveRecord::Base.connection.execute sql#.wp
|
80
|
+
execute(sql)
|
74
81
|
end
|
75
82
|
|
76
83
|
def down
|
77
84
|
sql = <<-SQL
|
78
|
-
|
85
|
+
DROP#{materialized_or_not}VIEW IF EXISTS #{parent.view_name};
|
79
86
|
SQL
|
80
|
-
|
87
|
+
execute(sql)
|
88
|
+
end
|
89
|
+
|
90
|
+
def execute(sql)
|
91
|
+
SqlView.log sql
|
81
92
|
ActiveRecord::Base.connection.execute sql#.wp
|
82
93
|
end
|
83
94
|
|
84
95
|
private
|
85
96
|
|
86
97
|
def materialized_or_not
|
87
|
-
parent.sql_view_options[:materialized] ? "MATERIALIZED" :
|
98
|
+
parent.sql_view_options[:materialized] ? " MATERIALIZED " : " "
|
88
99
|
end
|
89
100
|
|
90
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql_view
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|