-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathmaterializable.rb
More file actions
85 lines (73 loc) · 2.1 KB
/
materializable.rb
File metadata and controls
85 lines (73 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Scenic
module Generators
# @api private
module Materializable
extend ActiveSupport::Concern
included do
class_option :materialized,
type: :boolean,
required: false,
desc: "Makes the view materialized",
default: false
class_option :no_data,
type: :boolean,
required: false,
desc: "Adds WITH NO DATA when materialized view creates/updates",
default: false,
aliases: ["--no-data"]
class_option :side_by_side,
type: :boolean,
required: false,
desc: "Uses side-by-side strategy to update materialized view",
default: false,
aliases: ["--side-by-side"]
class_option :replace,
type: :boolean,
required: false,
desc: "Uses replace_view instead of update_view",
default: false
class_option :database,
type: :string,
required: false,
desc: "The database to use (requires Rails 6.0+)",
default: nil
end
private
def materialized?
options[:materialized]
end
def replace_view?
options[:replace]
end
def no_data?
options[:no_data]
end
def side_by_side?
options[:side_by_side]
end
def database
options[:database]&.to_sym
end
def validate_rails_version_for_multiple_databases!
return unless database
if Rails::VERSION::MAJOR < 6
raise ArgumentError,
"Multiple database support requires Rails 6.0 or higher. " \
"You are using Rails #{Rails::VERSION::STRING}."
end
end
def materialized_view_update_options
set_options = {no_data: no_data?, side_by_side: side_by_side?}
.select { |_, v| v }
if set_options.empty?
"true"
else
string_options = set_options.reduce("") do |memo, (key, value)|
memo + "#{key}: #{value}, "
end
"{ #{string_options.chomp(", ")} }"
end
end
end
end
end