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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/insights_export/export_models.rb', line 51
def self.get_structure
Rails.application.eager_load! if Rails.env.development?
models = ActiveRecord::Base.descendants
models = models.reject { |m| m.abstract_class? }
only_models = InsightsExport.configuration.only_models
if only_models.present?
models = models.select { |m| only_models.select { |lm| lm.is_a?(Regexp) ? lm.match(m.to_s) : lm == m.to_s }.present? }
end
except_models = InsightsExport.configuration.except_models
if except_models.present?
models = models.reject { |m| except_models.select { |lm| lm.is_a?(Regexp) ? lm.match(m.to_s) : lm == m.to_s }.present? }
end
models = models.sort_by { |m| m.to_s }
model_strings = models.map(&:to_s)
puts "InsightsExport: #{model_strings.join(', ')}"
return_object = {}
models.each do |model|
columns_hash = model.columns_hash
begin
model_structure = {
enabled: true,
model: model.to_s,
table_name: model.table_name,
primary_key: model.primary_key,
columns: columns_hash.map do |key, column|
obj = if column.type.in? %i(datetime date)
{ type: :time }
elsif column.type.in? %i(integer decimal float)
{ type: :number }
elsif column.type.in? %i(string text)
{ type: :string }
elsif column.type.in? %i(boolean)
{ type: :boolean }
elsif column.type.in? %i(json)
{ type: :payload }
elsif column.type.in? %i(geography)
{ type: :geo }
else
puts "Warning! Unknown column type: :#{column.type} for #{model.to_s}, column #{key}"
{ unknown: column.type }
end
if key == model.primary_key
obj[:index] = :primary_key
end
[key.to_sym, obj]
end.to_h,
custom: {},
links: {
incoming: {},
outgoing: {}
}
}
model.reflections.each do |association_name, reflection|
begin
reflection_class = reflection.class_name.gsub(/^::/, '')
next unless model_strings.include?(reflection_class)
if reflection.macro == :belongs_to
model_structure[:columns].delete(reflection.foreign_key.to_sym)
model_structure[:links][:outgoing][association_name] = {
model: reflection_class,
model_key: reflection.association_primary_key,
my_key: reflection.foreign_key
}
elsif reflection.macro.in? %i(has_one has_many)
if reflection.options.try(:[], :through).present?
next
end
model_structure[:links][:incoming][association_name] = {
model: reflection_class,
model_key: reflection.foreign_key,
my_key: reflection.association_primary_key
}
else
puts "Warning! Unknown reflection :#{reflection.macro} for association '#{association_name}' on model '#{model.to_s}'"
end
rescue => error
puts "!! Error when exporting association '#{association_name}' on model '#{model.to_s}'"
print_exception(error)
end
end
return_object[model.to_s] = model_structure
rescue => error
puts "!! Error when exporting model '#{model.to_s}'"
print_exception(error)
end
end
return_object.sort_by { |k, v| k }.to_h.deep_stringify_keys
end
|