rails_db_objects 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +28 -28
- data/lib/rails_db_objects/db_objects_creator.rb +318 -231
- data/lib/rails_db_objects/railtie.rb +15 -15
- data/lib/rails_db_objects/version.rb +3 -3
- data/lib/tasks/rails_db_objects_tasks.rake +45 -45
- data/test/dummy/Rakefile +6 -6
- data/test/dummy/bin/bundle +3 -3
- data/test/dummy/bin/rails +4 -4
- data/test/dummy/bin/rake +4 -4
- data/test/dummy/bin/setup +29 -29
- data/test/dummy/config/application.rb +25 -25
- data/test/dummy/config/boot.rb +5 -5
- data/test/dummy/config/environment.rb +5 -5
- data/test/rails_db_objects_test.rb +7 -7
- data/test/test_helper.rb +20 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a34d7b5731efea0e2ae83c0e0a66f62eafbe7ab9
|
4
|
+
data.tar.gz: 6e9758078a65334f7d408ed25d3cc60bd1b8b3b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95fa51efaa72f6dffefa6600a6c02838f0739b093cd2d0b913c6f01140364498cbecaa7eb8e9783befaef0527e832b265a2f81f02571c3b3746aa5ed1c34dbfc
|
7
|
+
data.tar.gz: 9e506abd8ecc4c071100981d93d39f4ffa05f4afe39e80210a42e587c4c9a6fd39b884714d8e64d9ec77229ed70b6433e3f650ce102e0919c9eb3c74c454f5f4
|
data/Rakefile
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
begin
|
2
|
-
require 'bundler/setup'
|
3
|
-
rescue LoadError
|
4
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
-
end
|
6
|
-
|
7
|
-
require 'rdoc/task'
|
8
|
-
|
9
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'RailsDbObjects'
|
12
|
-
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.rdoc')
|
14
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
-
end
|
16
|
-
|
17
|
-
Bundler::GemHelper.install_tasks
|
18
|
-
|
19
|
-
require 'rake/testtask'
|
20
|
-
|
21
|
-
Rake::TestTask.new(:test) do |t|
|
22
|
-
t.libs << 'lib'
|
23
|
-
t.libs << 'test'
|
24
|
-
t.pattern = 'test/**/*_test.rb'
|
25
|
-
t.verbose = false
|
26
|
-
end
|
27
|
-
|
28
|
-
task default: :test
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsDbObjects'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: :test
|
@@ -1,231 +1,318 @@
|
|
1
|
-
module RailsDbObjects
|
2
|
-
class DbObjectsCreator
|
3
|
-
attr_reader :objects
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@objects = {}
|
7
|
-
end
|
8
|
-
|
9
|
-
def register_files(files)
|
10
|
-
files.each do |file|
|
11
|
-
object_name = File.basename(file, File.extname(file))
|
12
|
-
object_type = File.dirname(file.to_s).to_s.split('/').last.upcase
|
13
|
-
|
14
|
-
@objects[object_type] ||= {}
|
15
|
-
|
16
|
-
content = File.read(file)
|
17
|
-
content_lines = content.split("\n")
|
18
|
-
|
19
|
-
# Reject the commented lines from the file
|
20
|
-
sql_content = content_lines.reject { |x| x.strip =~ /^--/ || x.strip =~ /^#/ }.join("\n")
|
21
|
-
|
22
|
-
file_obj = {
|
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
|
-
@objects[object_type]
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
def
|
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
|
-
if object[:
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
1
|
+
module RailsDbObjects
|
2
|
+
class DbObjectsCreator
|
3
|
+
attr_reader :objects
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@objects = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def register_files(files)
|
10
|
+
files.each do |file|
|
11
|
+
object_name = File.basename(file, File.extname(file))
|
12
|
+
object_type = File.dirname(file.to_s).to_s.split('/').last.upcase
|
13
|
+
|
14
|
+
@objects[object_type] ||= {}
|
15
|
+
|
16
|
+
content = File.read(file)
|
17
|
+
content_lines = content.split("\n")
|
18
|
+
|
19
|
+
# Reject the commented lines from the file
|
20
|
+
sql_content = content_lines.reject { |x| x.strip =~ /^--/ || x.strip =~ /^#/ }.join("\n")
|
21
|
+
|
22
|
+
file_obj = {
|
23
|
+
name: object_name,
|
24
|
+
type: object_type,
|
25
|
+
debug: false,
|
26
|
+
directives: [],
|
27
|
+
path: file,
|
28
|
+
sql_content: sql_content,
|
29
|
+
status: :none,
|
30
|
+
requires: [],
|
31
|
+
silent: false,
|
32
|
+
keep: false,
|
33
|
+
nodrop: false,
|
34
|
+
deleted: false,
|
35
|
+
nocreate: false,
|
36
|
+
dbschema: Rails.configuration.rails_db_objects[:objects_dbschema],
|
37
|
+
dropsql: [],
|
38
|
+
createsql: [],
|
39
|
+
vanilla: false,
|
40
|
+
condition: [],
|
41
|
+
dropconditionruby: [],
|
42
|
+
createconditionruby: [],
|
43
|
+
beforedropruby: [],
|
44
|
+
beforedropsql: [],
|
45
|
+
afterdropruby: [],
|
46
|
+
afterdropsql: [],
|
47
|
+
beforecreateruby: [],
|
48
|
+
beforecreatesql: [],
|
49
|
+
aftercreateruby: [],
|
50
|
+
aftercreatesql: []
|
51
|
+
}
|
52
|
+
|
53
|
+
# Detect directives in commentary
|
54
|
+
directives = extract_from_comments(content_lines)
|
55
|
+
|
56
|
+
# puts "directives: #{directives.inspect}"
|
57
|
+
directives.each { |directive| prepare_directive(file_obj, directive) }
|
58
|
+
|
59
|
+
@objects[object_type][object_name] = file_obj
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def drop_objects
|
64
|
+
reset_objects_status!
|
65
|
+
@objects.keys.each do |object_type|
|
66
|
+
@objects[object_type].each do |_name, object|
|
67
|
+
drop_object(object)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_objects
|
73
|
+
reset_objects_status!
|
74
|
+
@objects.keys.each do |object_type|
|
75
|
+
@objects[object_type].each do |_name, object|
|
76
|
+
create_object(object)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def full(name)
|
84
|
+
prefix = @dbschema.blank? ? '' : "#{@dbschema}."
|
85
|
+
"#{prefix}#{wrap_name(name)}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def wrap_name(name)
|
89
|
+
adapter_name = ActiveRecord::Base.connection.adapter_name
|
90
|
+
|
91
|
+
case adapter_name
|
92
|
+
when 'PostgreSQL'
|
93
|
+
"\"#{name}\""
|
94
|
+
when 'MySQL'
|
95
|
+
"`#{name}`"
|
96
|
+
when 'SQLServer'
|
97
|
+
"[#{name}]"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def extract_from_comments(content_lines)
|
102
|
+
dir_lines = content_lines.select { |x| x.strip =~ /^--/ || x.strip =~ /^#/ }.map(&:strip)
|
103
|
+
dir_lines.map { |x| /^--/.match?(x) ? x[2..-1] : x[1..-1] }.select { |x| x =~ /^!/ }
|
104
|
+
end
|
105
|
+
|
106
|
+
# rubocop:disable Metrics/AbcSize
|
107
|
+
def prepare_directive(file_obj, directive)
|
108
|
+
file_obj[:debug] = /^!debug/.match?(directive) unless file_obj[:debug]
|
109
|
+
file_obj[:directives] << directive
|
110
|
+
file_obj[:requires] += directive.split(' ')[1..-1] if /^!require /.match?(directive)
|
111
|
+
file_obj[:vanilla] = /^!vanilla/.match?(directive) unless file_obj[:vanilla]
|
112
|
+
file_obj[:nocreate] = file_obj[:deleted] = /^!deleted/.match?(directive) unless file_obj[:deleted]
|
113
|
+
file_obj[:silent] = /^!silent/.match?(directive) unless file_obj[:silent]
|
114
|
+
file_obj[:nodrop] = file_obj[:keep] = /^!keep/.match?(directive) unless file_obj[:keep]
|
115
|
+
file_obj[:dbschema] = directive.split(' ')[1..-1] if /^!schema/.match?(directive)
|
116
|
+
file_obj[:dropconditionruby] << directive.split(' ')[1..-1].join(' ') if /^!dropconditionruby /.match?(directive)
|
117
|
+
file_obj[:createconditionruby] << directive.split(' ')[1..-1].join(' ') if /^!createconditionruby /.match?(directive)
|
118
|
+
file_obj[:condition] << directive.split(' ')[1..-1].join(' ') if /^!condition /.match?(directive)
|
119
|
+
file_obj[:beforedropsql] << directive.split(' ')[1..-1].join(' ') if /^!beforedropsql /.match?(directive)
|
120
|
+
file_obj[:dropsql] << directive.split(' ')[1..-1].join(' ') if /^!dropsql /.match?(directive)
|
121
|
+
file_obj[:afterdropsql] << directive.split(' ')[1..-1].join(' ') if /^!afterdropsql /.match?(directive)
|
122
|
+
file_obj[:beforecreatesql] << directive.split(' ')[1..-1].join(' ') if /^!beforecreatesql /.match?(directive)
|
123
|
+
file_obj[:createsql] << directive.split(' ')[1..-1].join(' ') if /^!createsql /.match?(directive)
|
124
|
+
file_obj[:aftercreatesql] << directive.split(' ')[1..-1].join(' ') if /^!aftercreatesql /.match?(directive)
|
125
|
+
end
|
126
|
+
# rubocop:enable Metrics/AbcSize
|
127
|
+
|
128
|
+
def reset_objects_status!
|
129
|
+
@objects.keys.each do |object_type|
|
130
|
+
@objects[object_type].each do |_name, object|
|
131
|
+
object[:status] = :none
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def conditional_ruby(condition, object)
|
137
|
+
interpolated_command = object.instance_eval('"' + condition.gsub(/\"/, '\"') + '"')
|
138
|
+
condition = object.instance_eval(interpolated_command)
|
139
|
+
if object[:debug]
|
140
|
+
puts '=' * 80
|
141
|
+
puts "interpolated_command: #{interpolated_command}"
|
142
|
+
puts '=' * 80
|
143
|
+
puts "condition: #{condition.inspect}"
|
144
|
+
puts '=' * 80
|
145
|
+
end
|
146
|
+
condition ? true : false
|
147
|
+
end
|
148
|
+
|
149
|
+
def interpolate_sql(sql, object)
|
150
|
+
object.instance_eval('"' + sql.gsub(/\"/, '\"') + '"')
|
151
|
+
end
|
152
|
+
|
153
|
+
# rubocop:disable Metrics/AbcSize
|
154
|
+
def drop_object(object)
|
155
|
+
return if object[:nodrop]
|
156
|
+
|
157
|
+
return if object[:status] == :loaded
|
158
|
+
|
159
|
+
object_type = object[:type]
|
160
|
+
name = object[:name]
|
161
|
+
@dbschema = (object[:dbschema] || []).clone
|
162
|
+
|
163
|
+
raise "Error: Circular file reference! (#{object_type} #{name})" if object[:status] == :inprogress
|
164
|
+
|
165
|
+
full_name = full(name)
|
166
|
+
|
167
|
+
if object[:dropconditionruby].compact.any?
|
168
|
+
condition = conditional_ruby(object[:dropconditionruby].join("\n"), object)
|
169
|
+
if condition
|
170
|
+
puts "RUBY CONDITION MET FOR #{object_type} #{full_name} #{condition}"
|
171
|
+
else
|
172
|
+
puts "RUBY CONDITION NOT MET FOR #{object_type} #{full_name} #{condition}"
|
173
|
+
return
|
174
|
+
end
|
175
|
+
elsif object[:condition].compact.any?
|
176
|
+
condition = !ActiveRecord::Base.connection.select_rows(object[:condition].join("\n")).empty?
|
177
|
+
if condition
|
178
|
+
puts "SQL CONDITION MET FOR #{object_type} #{full_name}"
|
179
|
+
else
|
180
|
+
puts "SQL CONDITION NOT MET FOR #{object_type} #{full_name}"
|
181
|
+
return
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
if object[:debug]
|
186
|
+
puts '=' * 80
|
187
|
+
puts "DROP OBJECT #{name} / #{object[:path]}"
|
188
|
+
puts '=' * 80
|
189
|
+
puts object.to_json
|
190
|
+
puts '=' * 80
|
191
|
+
end
|
192
|
+
|
193
|
+
object[:requires].each do |requirement|
|
194
|
+
requires = requirement.split('/', 2)
|
195
|
+
required_object = requires.last
|
196
|
+
required_type = requires.first.upcase if requires.length == 2
|
197
|
+
required_type ||= object_type
|
198
|
+
drop_object @objects[required_type.upcase][required_object]
|
199
|
+
end
|
200
|
+
|
201
|
+
sql = if object[:dropsql].compact.empty? && !object[:vanilla]
|
202
|
+
"DROP #{object_type} #{full_name};"
|
203
|
+
else
|
204
|
+
interpolate_sql(object[:dropsql].compact.join(";\n"), object)
|
205
|
+
end
|
206
|
+
|
207
|
+
begin
|
208
|
+
conditional_ruby(object[:beforedropruby].join("\n"), object) unless object[:beforedropruby].empty?
|
209
|
+
ActiveRecord::Base.connection.execute(object[:beforedropsql].join("\n")) unless object[:beforedropsql].empty?
|
210
|
+
ActiveRecord::Base.connection.execute(sql)
|
211
|
+
ActiveRecord::Base.connection.execute(object[:afterdropsql].join("\n")) unless object[:afterdropsql].empty?
|
212
|
+
puts "DROP #{object_type} #{full_name}... OK"
|
213
|
+
conditional_ruby(object[:afterdropruby].join("\n"), object) unless object[:afterdropruby].empty?
|
214
|
+
rescue StandardError => e
|
215
|
+
unless object[:debug]
|
216
|
+
puts '#' * 80
|
217
|
+
puts e.message.to_s
|
218
|
+
# puts "#"*80
|
219
|
+
# puts "#{e.backtrace}"
|
220
|
+
puts '#' * 80
|
221
|
+
puts "WARNING: #{sql}... ERROR"
|
222
|
+
puts '#' * 80
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
object[:status] = :loaded
|
227
|
+
end
|
228
|
+
|
229
|
+
def create_object(object)
|
230
|
+
return if object[:nocreate]
|
231
|
+
|
232
|
+
return if object[:status] == :loaded
|
233
|
+
|
234
|
+
object_type = object[:type]
|
235
|
+
name = object[:name]
|
236
|
+
|
237
|
+
raise "Error: Circular file reference! (#{object_type} #{name})" if object[:status] == :inprogress
|
238
|
+
|
239
|
+
object[:status] = :inprogress
|
240
|
+
@dbschema = (object[:dbschema] || []).clone
|
241
|
+
|
242
|
+
full_name = full(name)
|
243
|
+
|
244
|
+
if object[:createconditionruby].compact.any?
|
245
|
+
condition = conditional_ruby(object[:createconditionruby].join("\n"), object)
|
246
|
+
if condition
|
247
|
+
puts "RUBY CONDITION MET FOR #{object_type} #{full_name} / #{condition}"
|
248
|
+
else
|
249
|
+
puts "RUBY CONDITION NOT MET FOR #{object_type} #{full_name}"
|
250
|
+
return
|
251
|
+
end
|
252
|
+
elsif object[:condition].compact.any?
|
253
|
+
condition = ActiveRecord::Base.connection.select_rows(object[:condition].join("\n")).empty?
|
254
|
+
if condition
|
255
|
+
puts "CONDITION MET FOR #{object_type} #{full_name}"
|
256
|
+
else
|
257
|
+
puts "CONDITION NOT MET FOR #{object_type} #{full_name}"
|
258
|
+
return
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
if object[:debug]
|
263
|
+
puts '=' * 80
|
264
|
+
puts "CREATE OBJECT #{name} / #{object[:path]}"
|
265
|
+
puts '=' * 80
|
266
|
+
puts object.to_json
|
267
|
+
puts '=' * 80
|
268
|
+
end
|
269
|
+
# skip empty sql content
|
270
|
+
if object[:sql_content].strip.blank?
|
271
|
+
puts "#{object_type} #{name} EMPTY... SKIPPING"
|
272
|
+
return
|
273
|
+
end
|
274
|
+
|
275
|
+
create_dependencies(object, object_type)
|
276
|
+
|
277
|
+
sql = if object[:vanilla]
|
278
|
+
interpolate_sql(object[:sql_content], object)
|
279
|
+
elsif object[:createsql].compact.empty?
|
280
|
+
"CREATE #{object_type} #{full_name}\n#{object[:sql_content]}"
|
281
|
+
else
|
282
|
+
interpolate_sql(object[:createsql].compact.join(";\n"), object)
|
283
|
+
end
|
284
|
+
|
285
|
+
begin
|
286
|
+
conditional_ruby(object[:beforecreateruby].join("\n"), object) unless object[:beforecreateruby].empty?
|
287
|
+
ActiveRecord::Base.connection.execute(object[:beforecreatesql].join("\n")) unless object[:beforecreatesql].empty?
|
288
|
+
ActiveRecord::Base.connection.execute(sql.to_s)
|
289
|
+
ActiveRecord::Base.connection.execute(object[:aftercreatesql].join("\n")) unless object[:aftercreatesql].empty?
|
290
|
+
conditional_ruby(object[:aftercreateruby].join("\n"), object) unless object[:aftercreateruby].empty?
|
291
|
+
puts "CREATE #{object_type} #{full_name}... OK"
|
292
|
+
rescue StandardError => e
|
293
|
+
unless object[:silent]
|
294
|
+
puts '#' * 80
|
295
|
+
puts e.message.to_s
|
296
|
+
# puts '#' * 80
|
297
|
+
# puts "#{e.backtrace}"
|
298
|
+
puts '#' * 80
|
299
|
+
puts "WARNING: CREATE #{object_type} #{full_name}... ERROR"
|
300
|
+
puts '#' * 80
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
object[:status] = :loaded
|
305
|
+
end
|
306
|
+
# rubocop:enable Metrics/AbcSize
|
307
|
+
|
308
|
+
def create_dependencies(object, object_type)
|
309
|
+
object[:requires].each do |requirement|
|
310
|
+
requires = requirement.split('/', 2)
|
311
|
+
required_object = requires.last
|
312
|
+
required_type = requires.first.upcase if requires.length == 2
|
313
|
+
required_type ||= object_type
|
314
|
+
create_object(@objects[required_type.upcase][required_object])
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
class Railtie < Rails::Railtie
|
2
|
-
railtie_name :rails_db_objects
|
3
|
-
|
4
|
-
config.rails_db_objects = ActiveSupport::OrderedHash.new
|
5
|
-
|
6
|
-
initializer 'rails_db_objects.initialize' do |app|
|
7
|
-
app.config.rails_db_objects[:objects_path] = %w[db/objects/**/]
|
8
|
-
app.config.rails_db_objects[:objects_ext] = '*.sql'
|
9
|
-
app.config.rails_db_objects[:objects_dbschema] = ['dbo']
|
10
|
-
end
|
11
|
-
|
12
|
-
rake_tasks do
|
13
|
-
load 'tasks/rails_db_objects_tasks.rake'
|
14
|
-
end
|
15
|
-
end
|
1
|
+
class Railtie < Rails::Railtie
|
2
|
+
railtie_name :rails_db_objects
|
3
|
+
|
4
|
+
config.rails_db_objects = ActiveSupport::OrderedHash.new
|
5
|
+
|
6
|
+
initializer 'rails_db_objects.initialize' do |app|
|
7
|
+
app.config.rails_db_objects[:objects_path] = %w[db/objects/**/]
|
8
|
+
app.config.rails_db_objects[:objects_ext] = '*.sql'
|
9
|
+
app.config.rails_db_objects[:objects_dbschema] = ['dbo']
|
10
|
+
end
|
11
|
+
|
12
|
+
rake_tasks do
|
13
|
+
load 'tasks/rails_db_objects_tasks.rake'
|
14
|
+
end
|
15
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module RailsDbObjects
|
2
|
-
VERSION = '0.
|
3
|
-
end
|
1
|
+
module RailsDbObjects
|
2
|
+
VERSION = '1.0.0'.freeze
|
3
|
+
end
|
@@ -1,45 +1,45 @@
|
|
1
|
-
namespace :db do
|
2
|
-
desc 'Generate all the database objects of the current project'
|
3
|
-
task create_objects: :environment do
|
4
|
-
creator = RailsDbObjects::DbObjectsCreator.new
|
5
|
-
|
6
|
-
objects_path = Rails.configuration.rails_db_objects[:objects_path]
|
7
|
-
objects_ext = Rails.configuration.rails_db_objects[:objects_ext]
|
8
|
-
|
9
|
-
objects_path.each do |path|
|
10
|
-
creator.register_files(Dir[File.join(path, objects_ext)].map { |x| File.expand_path(x) })
|
11
|
-
end
|
12
|
-
|
13
|
-
creator.create_objects
|
14
|
-
end
|
15
|
-
|
16
|
-
desc 'Drop all the database objects of the current project'
|
17
|
-
task drop_objects: :environment do
|
18
|
-
creator = RailsDbObjects::DbObjectsCreator.new
|
19
|
-
|
20
|
-
objects_path = Rails.configuration.rails_db_objects[:objects_path]
|
21
|
-
objects_ext = Rails.configuration.rails_db_objects[:objects_ext]
|
22
|
-
|
23
|
-
objects_path.each do |path|
|
24
|
-
creator.register_files(Dir[File.join(path, objects_ext)].map { |x| File.expand_path(x) })
|
25
|
-
end
|
26
|
-
|
27
|
-
creator.drop_objects
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
require 'rake/hooks'
|
32
|
-
|
33
|
-
before 'db:migrate' do
|
34
|
-
Rake::Task['db:drop_objects'].invoke
|
35
|
-
end
|
36
|
-
before 'db:rollback' do
|
37
|
-
Rake::Task['db:drop_objects'].invoke
|
38
|
-
end
|
39
|
-
|
40
|
-
after 'db:migrate' do
|
41
|
-
Rake::Task['db:create_objects'].invoke
|
42
|
-
end
|
43
|
-
after 'db:rollback' do
|
44
|
-
Rake::Task['db:create_objects'].invoke
|
45
|
-
end
|
1
|
+
namespace :db do
|
2
|
+
desc 'Generate all the database objects of the current project'
|
3
|
+
task create_objects: :environment do
|
4
|
+
creator = RailsDbObjects::DbObjectsCreator.new
|
5
|
+
|
6
|
+
objects_path = Rails.configuration.rails_db_objects[:objects_path]
|
7
|
+
objects_ext = Rails.configuration.rails_db_objects[:objects_ext]
|
8
|
+
|
9
|
+
objects_path.each do |path|
|
10
|
+
creator.register_files(Dir[File.join(path, objects_ext)].map { |x| File.expand_path(x) })
|
11
|
+
end
|
12
|
+
|
13
|
+
creator.create_objects
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Drop all the database objects of the current project'
|
17
|
+
task drop_objects: :environment do
|
18
|
+
creator = RailsDbObjects::DbObjectsCreator.new
|
19
|
+
|
20
|
+
objects_path = Rails.configuration.rails_db_objects[:objects_path]
|
21
|
+
objects_ext = Rails.configuration.rails_db_objects[:objects_ext]
|
22
|
+
|
23
|
+
objects_path.each do |path|
|
24
|
+
creator.register_files(Dir[File.join(path, objects_ext)].map { |x| File.expand_path(x) })
|
25
|
+
end
|
26
|
+
|
27
|
+
creator.drop_objects
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
require 'rake/hooks'
|
32
|
+
|
33
|
+
before 'db:migrate' do
|
34
|
+
Rake::Task['db:drop_objects'].invoke
|
35
|
+
end
|
36
|
+
before 'db:rollback' do
|
37
|
+
Rake::Task['db:drop_objects'].invoke
|
38
|
+
end
|
39
|
+
|
40
|
+
after 'db:migrate' do
|
41
|
+
Rake::Task['db:create_objects'].invoke
|
42
|
+
end
|
43
|
+
after 'db:rollback' do
|
44
|
+
Rake::Task['db:create_objects'].invoke
|
45
|
+
end
|
data/test/dummy/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
-
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
-
|
4
|
-
require File.expand_path('config/application', __dir__)
|
5
|
-
|
6
|
-
Rails.application.load_tasks
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('config/application', __dir__)
|
5
|
+
|
6
|
+
Rails.application.load_tasks
|
data/test/dummy/bin/bundle
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
#!/usr/bin/env ruby.exe
|
2
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
3
|
-
load Gem.bin_path('bundler', 'bundle')
|
1
|
+
#!/usr/bin/env ruby.exe
|
2
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
3
|
+
load Gem.bin_path('bundler', 'bundle')
|
data/test/dummy/bin/rails
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/env ruby.exe
|
2
|
-
APP_PATH = File.expand_path('../config/application', __dir__)
|
3
|
-
require_relative '../config/boot'
|
4
|
-
require 'rails/commands'
|
1
|
+
#!/usr/bin/env ruby.exe
|
2
|
+
APP_PATH = File.expand_path('../config/application', __dir__)
|
3
|
+
require_relative '../config/boot'
|
4
|
+
require 'rails/commands'
|
data/test/dummy/bin/rake
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/env ruby.exe
|
2
|
-
require_relative '../config/boot'
|
3
|
-
require 'rake'
|
4
|
-
Rake.application.run
|
1
|
+
#!/usr/bin/env ruby.exe
|
2
|
+
require_relative '../config/boot'
|
3
|
+
require 'rake'
|
4
|
+
Rake.application.run
|
data/test/dummy/bin/setup
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
#!/usr/bin/env ruby.exe
|
2
|
-
require 'pathname'
|
3
|
-
|
4
|
-
# path to your application root.
|
5
|
-
APP_ROOT = Pathname.new File.expand_path('..', __dir__)
|
6
|
-
|
7
|
-
Dir.chdir APP_ROOT do
|
8
|
-
# This script is a starting point to setup your application.
|
9
|
-
# Add necessary setup steps to this file:
|
10
|
-
|
11
|
-
puts '== Installing dependencies =='
|
12
|
-
system 'gem install bundler --conservative'
|
13
|
-
system 'bundle check || bundle install'
|
14
|
-
|
15
|
-
# puts "\n== Copying sample files =="
|
16
|
-
# unless File.exist?("config/database.yml")
|
17
|
-
# system "cp config/database.yml.sample config/database.yml"
|
18
|
-
# end
|
19
|
-
|
20
|
-
puts "\n== Preparing database =="
|
21
|
-
system 'bin/rake db:setup'
|
22
|
-
|
23
|
-
puts "\n== Removing old logs and tempfiles =="
|
24
|
-
system 'rm -f log/*'
|
25
|
-
system 'rm -rf tmp/cache'
|
26
|
-
|
27
|
-
puts "\n== Restarting application server =="
|
28
|
-
system 'touch tmp/restart.txt'
|
29
|
-
end
|
1
|
+
#!/usr/bin/env ruby.exe
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('..', __dir__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts '== Installing dependencies =='
|
12
|
+
system 'gem install bundler --conservative'
|
13
|
+
system 'bundle check || bundle install'
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system 'bin/rake db:setup'
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system 'rm -f log/*'
|
25
|
+
system 'rm -rf tmp/cache'
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system 'touch tmp/restart.txt'
|
29
|
+
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
require File.expand_path('boot', __dir__)
|
2
|
-
|
3
|
-
require 'rails/all'
|
4
|
-
|
5
|
-
Bundler.require(*Rails.groups)
|
6
|
-
require 'rails_db_objects'
|
7
|
-
|
8
|
-
module Dummy
|
9
|
-
class Application < Rails::Application
|
10
|
-
# Settings in config/environments/* take precedence over those specified here.
|
11
|
-
# Application configuration should go into files in config/initializers
|
12
|
-
# -- all .rb files in that directory are automatically loaded.
|
13
|
-
|
14
|
-
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
15
|
-
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
16
|
-
# config.time_zone = 'Central Time (US & Canada)'
|
17
|
-
|
18
|
-
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
19
|
-
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
20
|
-
# config.i18n.default_locale = :de
|
21
|
-
|
22
|
-
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
-
config.active_record.raise_in_transactional_callbacks = true
|
24
|
-
end
|
25
|
-
end
|
1
|
+
require File.expand_path('boot', __dir__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require(*Rails.groups)
|
6
|
+
require 'rails_db_objects'
|
7
|
+
|
8
|
+
module Dummy
|
9
|
+
class Application < Rails::Application
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
11
|
+
# Application configuration should go into files in config/initializers
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
13
|
+
|
14
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
15
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
16
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
17
|
+
|
18
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
19
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
20
|
+
# config.i18n.default_locale = :de
|
21
|
+
|
22
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
+
config.active_record.raise_in_transactional_callbacks = true
|
24
|
+
end
|
25
|
+
end
|
data/test/dummy/config/boot.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# Set up gems listed in the Gemfile.
|
2
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
|
3
|
-
|
4
|
-
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
5
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
|
1
|
+
# Set up gems listed in the Gemfile.
|
2
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
|
3
|
+
|
4
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
5
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# Load the Rails application.
|
2
|
-
require File.expand_path('application', __dir__)
|
3
|
-
|
4
|
-
# Initialize the Rails application.
|
5
|
-
Rails.application.initialize!
|
1
|
+
# Load the Rails application.
|
2
|
+
require File.expand_path('application', __dir__)
|
3
|
+
|
4
|
+
# Initialize the Rails application.
|
5
|
+
Rails.application.initialize!
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class RailsDbObjectsTest < ActiveSupport::TestCase
|
4
|
-
test 'truth' do
|
5
|
-
assert_kind_of Module, RailsDbObjects
|
6
|
-
end
|
7
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RailsDbObjectsTest < ActiveSupport::TestCase
|
4
|
+
test 'truth' do
|
5
|
+
assert_kind_of Module, RailsDbObjects
|
6
|
+
end
|
7
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
# Configure Rails Environment
|
2
|
-
ENV['RAILS_ENV'] = 'test'
|
3
|
-
|
4
|
-
require File.expand_path('../test/dummy/config/environment.rb', __dir__)
|
5
|
-
ActiveRecord::Migrator.migrations_paths = [File.expand_path('../test/dummy/db/migrate', __dir__)]
|
6
|
-
require 'rails/test_help'
|
7
|
-
|
8
|
-
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
9
|
-
# to be shown.
|
10
|
-
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
11
|
-
|
12
|
-
# Load support files
|
13
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
14
|
-
|
15
|
-
# Load fixtures from the engine
|
16
|
-
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
17
|
-
ActiveSupport::TestCase.fixture_path = File.expand_path('fixtures', __dir__)
|
18
|
-
ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
|
19
|
-
ActiveSupport::TestCase.fixtures :all
|
20
|
-
end
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
|
+
|
4
|
+
require File.expand_path('../test/dummy/config/environment.rb', __dir__)
|
5
|
+
ActiveRecord::Migrator.migrations_paths = [File.expand_path('../test/dummy/db/migrate', __dir__)]
|
6
|
+
require 'rails/test_help'
|
7
|
+
|
8
|
+
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
9
|
+
# to be shown.
|
10
|
+
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
11
|
+
|
12
|
+
# Load support files
|
13
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
14
|
+
|
15
|
+
# Load fixtures from the engine
|
16
|
+
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
17
|
+
ActiveSupport::TestCase.fixture_path = File.expand_path('fixtures', __dir__)
|
18
|
+
ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
|
19
|
+
ActiveSupport::TestCase.fixtures :all
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_db_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ''
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-hooks
|