insights_export 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 830f36f902942abbc20985d74ef05fc45c27cf34
4
+ data.tar.gz: 162aa9da745f357847d55f22478f626c999a68f0
5
+ SHA512:
6
+ metadata.gz: 5500276d0aab589700a5d0dcecf8f17bd5735b9a90b58a0e22d1b53cd0a6478f77c2e562cb9cf5ad750f8dfddbee0cb3e3dc159f2b16927d0c9031c1f689513e
7
+ data.tar.gz: 5180a572f9c374fc1217401b7843a2f81d6d391ef336599275bf705763d5d762c58fad0cb5a170c6f44b9f6b95f4a65575c36baab2f4f5b6fccd9c1819e2a902
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'insights_export'
@@ -0,0 +1,137 @@
1
+ module InsightsExport
2
+ class ExportModels
3
+ def self.config_file
4
+ "#{Rails.root}/config/insights.yml"
5
+ end
6
+
7
+ def self.load
8
+ structure = YAML::load_file(config_file) rescue get_structure
9
+
10
+ structure.select { |k, v| v['enabled'] }.map do |k, v|
11
+ [k, v.merge({ 'columns' => v['columns'].select { |_, vv| vv.present? } })]
12
+ end.to_h
13
+ end
14
+
15
+ def self.export
16
+ input = YAML::load_file(config_file) rescue nil
17
+ structure = get_structure.deep_stringify_keys
18
+ output = {}
19
+
20
+ if input.present?
21
+ output = input.dup.deep_stringify_keys
22
+ structure.each do |model_name, model_structure|
23
+ # we already had this model in the output
24
+ if output[model_name].present?
25
+ model_structure.each do |key, value|
26
+ if key == 'custom'
27
+ next
28
+ elsif key == 'columns' || key == 'aggregate'
29
+ output[model_name][key] ||= {}
30
+ value.each do |value_key, value_value|
31
+ existing = output[model_name][key][value_key]
32
+ if existing != false
33
+ output[model_name][key][value_key] = (existing || {}).merge(value_value)
34
+ end
35
+ end
36
+ elsif key != 'enabled'
37
+ output[model_name][key] = value
38
+ end
39
+ end
40
+ else
41
+ output[model_name] = model_structure
42
+ end
43
+ end
44
+ else
45
+ output = structure
46
+ end
47
+
48
+ File.open(config_file, 'w') {|f| f.write output.deep_stringify_keys.to_yaml }
49
+ end
50
+
51
+ def self.get_structure
52
+ Rails.application.eager_load! if Rails.env.development?
53
+
54
+ models = ApplicationRecord.descendants
55
+
56
+ models.map do |model|
57
+ begin
58
+ columns_hash = model.columns_hash
59
+ rescue
60
+ next
61
+ end
62
+
63
+ model_structure = {
64
+ enabled: true,
65
+ model: model.to_s,
66
+ table_name: model.table_name,
67
+ primary_key: model.primary_key,
68
+ columns: columns_hash.map do |key, column|
69
+ obj = if column.type.in? %i(datetime date)
70
+ { type: :time }
71
+ elsif column.type.in? %i(integer decimal float)
72
+ { type: :number }
73
+ elsif column.type.in? %i(string text)
74
+ { type: :string }
75
+ elsif column.type.in? %i(boolean)
76
+ { type: :boolean }
77
+ elsif column.type.in? %i(json)
78
+ { type: :payload }
79
+ elsif column.type.in? %i(geography)
80
+ { type: :geo }
81
+ else
82
+ puts "Warning! Unknown column type: :#{column.type} for #{model.to_s}, column #{key}"
83
+ { unknown: column.type }
84
+ end
85
+
86
+ if key == model.primary_key
87
+ obj[:index] = :primary_key
88
+ end
89
+
90
+ [key.to_sym, obj]
91
+ end.to_h,
92
+ custom: {},
93
+ aggregate: {
94
+ count: {
95
+ sql: "count($$.#{model.primary_key})"
96
+ }
97
+ },
98
+ links: {
99
+ incoming: {},
100
+ outgoing: {}
101
+ }
102
+ }
103
+
104
+ model.reflections.each do |association_name, reflection|
105
+ if reflection.macro == :belongs_to
106
+ # reflection.class_name # User
107
+ # reflection.foreign_key # user_id
108
+ # reflection.association_primary_key # id
109
+
110
+ model_structure[:columns].delete(reflection.foreign_key.to_sym)
111
+ model_structure[:links][:outgoing][association_name] = {
112
+ model: reflection.class_name,
113
+ model_key: reflection.association_primary_key,
114
+ my_key: reflection.foreign_key
115
+ }
116
+ elsif reflection.macro.in? %i(has_one has_many)
117
+ # skip has_many :through associations
118
+ if reflection.options.try(:[], :through).present?
119
+ next
120
+ end
121
+
122
+ model_structure[:links][:incoming][association_name] = {
123
+ model: reflection.class_name,
124
+ model_key: reflection.foreign_key,
125
+ my_key: reflection.association_primary_key
126
+ }
127
+ else
128
+ puts "Warning! Unknown reflection :#{reflection.macro} for association #{association_name} on model #{model.to_s}"
129
+ end
130
+ end
131
+
132
+ [model.to_s, model_structure]
133
+ end.select(&:present?).sort_by { |k, v| k }.to_h.deep_stringify_keys
134
+ end
135
+ end
136
+ end
137
+
@@ -0,0 +1,7 @@
1
+ module InsightsExport
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ require 'immigrant/task'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ namespace :insights do
2
+ desc 'Export database structure to config/insights.yml'
3
+
4
+ task :export do
5
+ InsightsExport::ExportModels.export
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module InsightsExport
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'insights_export/export_models'
2
+
3
+ require 'insights_export/railtie' if defined?(Rails)
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insights_export
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marius Andra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: for use with insights.
28
+ email: marius.andra@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Rakefile
34
+ - lib/insights_export.rb
35
+ - lib/insights_export/export_models.rb
36
+ - lib/insights_export/railtie.rb
37
+ - lib/insights_export/task.rb
38
+ - lib/insights_export/version.rb
39
+ homepage: https://github.com/mariusandra/insights_export
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.5.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Export your database structure into config/insights.yml!
63
+ test_files: []