insights_export 0.2.1 → 0.2.2
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -2
- data/lib/insights_export/configuration.rb +5 -0
- data/lib/insights_export/export_models.rb +87 -68
- data/lib/insights_export/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b3cc11cb225f05541df399578423bb370fad699
|
4
|
+
data.tar.gz: bbc9af0241b380c0f91d99a425016e3ccf5e87c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7ddc9fffe0f31cef0d90059e648eb0f7b21ab153b1056525c1868fc4b989b35f6743756ec990dc088e76cc530c5ce3f4d490e71c122e18cd73240f534809e1e
|
7
|
+
data.tar.gz: b5c8921153667c08f4e5754703781c5700d5ef684b67f209a8fb76e2587933ad2493599eabd9626ba42e46155d17315533fb78e6cd29aadb572f237e77b7783f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,8 +9,7 @@ To configure the gem, create a file like `config/initializers/insights_export.rb
|
|
9
9
|
```rb
|
10
10
|
InsightsExport.configure do |config|
|
11
11
|
# The path to the export file
|
12
|
-
|
13
|
-
config.export_path = "#{Rails.root}/config/insights2.yml"
|
12
|
+
config.export_path = "#{Rails.root}/config/insights.yml"
|
14
13
|
|
15
14
|
# Export only models in this list
|
16
15
|
# Array of strings to filter or blank to export all.
|
@@ -19,5 +18,8 @@ InsightsExport.configure do |config|
|
|
19
18
|
# Exclude these models from the export
|
20
19
|
# Array of strings to filter or blank to export all.
|
21
20
|
config.except_models = []
|
21
|
+
|
22
|
+
# Print a backtrace when the export throws an exception.
|
23
|
+
config.debug = false
|
22
24
|
end
|
23
25
|
```
|
@@ -12,10 +12,15 @@ module InsightsExport
|
|
12
12
|
# Array of strings to filter or empty array to export all.
|
13
13
|
attr_accessor :except_models
|
14
14
|
|
15
|
+
# Print a backtrace when the export throws an exception.
|
16
|
+
# Default: false
|
17
|
+
attr_accessor :debug
|
18
|
+
|
15
19
|
def initialize
|
16
20
|
@export_path = "#{Rails.root}/config/insights.yml"
|
17
21
|
@only_models = []
|
18
22
|
@except_models = []
|
23
|
+
@debug = false
|
19
24
|
end
|
20
25
|
end
|
21
26
|
|
@@ -67,88 +67,107 @@ module InsightsExport
|
|
67
67
|
|
68
68
|
model_strings = models.map(&:to_s)
|
69
69
|
|
70
|
-
|
70
|
+
return_object = {}
|
71
|
+
|
72
|
+
models.each do |model|
|
71
73
|
begin
|
72
74
|
columns_hash = model.columns_hash
|
73
75
|
rescue
|
74
76
|
next
|
75
77
|
end
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
79
|
+
begin
|
80
|
+
model_structure = {
|
81
|
+
enabled: true,
|
82
|
+
model: model.to_s,
|
83
|
+
table_name: model.table_name,
|
84
|
+
primary_key: model.primary_key,
|
85
|
+
columns: columns_hash.map do |key, column|
|
86
|
+
obj = if column.type.in? %i(datetime date)
|
87
|
+
{ type: :time }
|
88
|
+
elsif column.type.in? %i(integer decimal float)
|
89
|
+
{ type: :number }
|
90
|
+
elsif column.type.in? %i(string text)
|
91
|
+
{ type: :string }
|
92
|
+
elsif column.type.in? %i(boolean)
|
93
|
+
{ type: :boolean }
|
94
|
+
elsif column.type.in? %i(json)
|
95
|
+
{ type: :payload }
|
96
|
+
elsif column.type.in? %i(geography)
|
97
|
+
{ type: :geo }
|
98
|
+
else
|
99
|
+
puts "Warning! Unknown column type: :#{column.type} for #{model.to_s}, column #{key}"
|
100
|
+
{ unknown: column.type }
|
101
|
+
end
|
102
|
+
|
103
|
+
if key == model.primary_key
|
104
|
+
obj[:index] = :primary_key
|
105
|
+
end
|
103
106
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
# },
|
112
|
-
links: {
|
113
|
-
incoming: {},
|
114
|
-
outgoing: {}
|
107
|
+
[key.to_sym, obj]
|
108
|
+
end.to_h,
|
109
|
+
custom: {},
|
110
|
+
links: {
|
111
|
+
incoming: {},
|
112
|
+
outgoing: {}
|
113
|
+
}
|
115
114
|
}
|
116
|
-
}
|
117
|
-
|
118
|
-
model.reflections.each do |association_name, reflection|
|
119
|
-
reflection_class = reflection.class_name.gsub(/^::/, '')
|
120
|
-
|
121
|
-
next unless model_strings.include?(reflection_class)
|
122
115
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
116
|
+
model.reflections.each do |association_name, reflection|
|
117
|
+
begin
|
118
|
+
reflection_class = reflection.class_name.gsub(/^::/, '')
|
119
|
+
|
120
|
+
next unless model_strings.include?(reflection_class)
|
121
|
+
|
122
|
+
if reflection.macro == :belongs_to
|
123
|
+
raise 'Bla bla' if model.to_s == 'Product'
|
124
|
+
# reflection_class # User
|
125
|
+
# reflection.foreign_key # user_id
|
126
|
+
# reflection.association_primary_key # id
|
127
|
+
|
128
|
+
model_structure[:columns].delete(reflection.foreign_key.to_sym)
|
129
|
+
model_structure[:links][:outgoing][association_name] = {
|
130
|
+
model: reflection_class,
|
131
|
+
model_key: reflection.association_primary_key,
|
132
|
+
my_key: reflection.foreign_key
|
133
|
+
}
|
134
|
+
elsif reflection.macro.in? %i(has_one has_many)
|
135
|
+
# skip has_many :through associations
|
136
|
+
if reflection.options.try(:[], :through).present?
|
137
|
+
next
|
138
|
+
end
|
127
139
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
140
|
+
model_structure[:links][:incoming][association_name] = {
|
141
|
+
model: reflection_class,
|
142
|
+
model_key: reflection.foreign_key,
|
143
|
+
my_key: reflection.association_primary_key
|
144
|
+
}
|
145
|
+
else
|
146
|
+
puts "Warning! Unknown reflection :#{reflection.macro} for association '#{association_name}' on model '#{model.to_s}'"
|
147
|
+
end
|
148
|
+
rescue => error
|
149
|
+
puts "!! Error when exporting association '#{association_name}' on model '#{model.to_s}'"
|
150
|
+
print_exception(error)
|
138
151
|
end
|
139
|
-
|
140
|
-
model_structure[:links][:incoming][association_name] = {
|
141
|
-
model: reflection_class,
|
142
|
-
model_key: reflection.foreign_key,
|
143
|
-
my_key: reflection.association_primary_key
|
144
|
-
}
|
145
|
-
else
|
146
|
-
puts "Warning! Unknown reflection :#{reflection.macro} for association #{association_name} on model #{model.to_s}"
|
147
152
|
end
|
153
|
+
|
154
|
+
return_object[model.to_s] = model_structure
|
155
|
+
rescue => error
|
156
|
+
puts "!! Error when exporting model '#{model.to_s}'"
|
157
|
+
print_exception(error)
|
148
158
|
end
|
159
|
+
end
|
160
|
+
|
161
|
+
return_object.sort_by { |k, v| k }.to_h.deep_stringify_keys
|
162
|
+
end
|
149
163
|
|
150
|
-
|
151
|
-
|
164
|
+
def self.print_exception(error)
|
165
|
+
puts "!! Exception: #{error.message}"
|
166
|
+
if InsightsExport.configuration.debug
|
167
|
+
puts error.backtrace
|
168
|
+
else
|
169
|
+
puts "-> Set config.debug = true to see the full backtrace"
|
170
|
+
end
|
152
171
|
end
|
153
172
|
end
|
154
173
|
end
|