yaddl 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yaddl.rb +164 -1
- data/lib/yaddl/version.rb +1 -1
- metadata +2 -2
data/lib/yaddl.rb
CHANGED
@@ -59,9 +59,166 @@ class Generator
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
def schema_diff
|
63
|
+
|
64
|
+
puts("","--- MIGRATIONS ---","") unless @quiet
|
65
|
+
|
66
|
+
schema = {}
|
67
|
+
table_name = nil
|
68
|
+
table_var = nil
|
69
|
+
Dir.glob("#{Rails.root}/db/migrate/*.rb").sort.each do |file|
|
70
|
+
File.read(file).each_line do |line|
|
71
|
+
if line =~ /^\s*create_table \:([a-z_]+) do \|([a-z_])+\|.*$/
|
72
|
+
table_name = line.sub(/^\s*create_table \:([a-z_]+) do \|([a-z_])+\|.*$/, '\1').strip
|
73
|
+
table_var = line.sub(/^\s*create_table \:([a-z_]+) do \|([a-z_])+\|.*$/, '\2').strip
|
74
|
+
schema[table_name] = {}
|
75
|
+
elsif line =~ /^\s*end\s*$/
|
76
|
+
table_name = nil
|
77
|
+
table_var = nil
|
78
|
+
down = false
|
79
|
+
elsif line =~ /^\s*def down.*$/
|
80
|
+
else
|
81
|
+
if table_name
|
82
|
+
if line =~ /^\s*#{table_var}\.([a-z_]+)(\s+|\()\:([a-z_]+).*$/
|
83
|
+
attr_type = line.sub(/^\s*#{table_var}\.([a-z_]+)(\s+|\()\:([a-z_]+).*$/, '\1').strip
|
84
|
+
attr_name = line.sub(/^\s*#{table_var}\.([a-z_]+)(\s+|\()\:([a-z_]+).*$/, '\3').strip
|
85
|
+
|
86
|
+
if attr_type == "references"
|
87
|
+
schema[table_name][attr_name+"_id"] ||= {}
|
88
|
+
schema[table_name][attr_name+"_id"]["type"] = "integer"
|
89
|
+
if line.include?("polymorphic: true")
|
90
|
+
schema[table_name][attr_name+"_type"] ||= {}
|
91
|
+
schema[table_name][attr_name+"_type"]["type"] = "string"
|
92
|
+
#add_index :sync_models, [:model_id, :model_type], name: "index_sync_models_on_model_id_and_model_type"
|
93
|
+
end
|
94
|
+
if line.include?("index: true")
|
95
|
+
schema[table_name]["_indexes"] = {}
|
96
|
+
if line.include?("polymorphic: true")
|
97
|
+
schema[table_name]["_indexes"]["#{attr_name}_id_and_#{attr_name}_type"] = {}
|
98
|
+
else
|
99
|
+
schema[table_name]["_indexes"]["#{attr_name}_id"] = {}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
schema[table_name][attr_name] ||= {}
|
104
|
+
schema[table_name][attr_name]["type"] = attr_type
|
105
|
+
end
|
106
|
+
end
|
107
|
+
if line =~ /^\s*#{table_var}\.timestamps\s*$/
|
108
|
+
schema[table_name]["created_at"] ||= {}
|
109
|
+
schema[table_name]["created_at"]["type"] = "datetime"
|
110
|
+
schema[table_name]["updated_at"] ||= {}
|
111
|
+
schema[table_name]["updated_at"]["type"] = "datetime"
|
112
|
+
end
|
113
|
+
else
|
114
|
+
if !down
|
115
|
+
if line =~ /^\s*add_index(\s+|\().*$/
|
116
|
+
attr_table= line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\2').strip
|
117
|
+
index_column = line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\3').strip
|
118
|
+
|
119
|
+
schema[attr_table]["_indexes"] = {}
|
120
|
+
schema[attr_table]["_indexes"][index_column] = {}
|
121
|
+
end
|
122
|
+
if line =~ /^\s*remove_column(\s+|\().*$/
|
123
|
+
attr_table= line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\2').strip
|
124
|
+
attr_name = line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\3').strip
|
125
|
+
schema[attr_table] ||= {}
|
126
|
+
schema[attr_table].delete(attr_name)
|
127
|
+
end
|
128
|
+
if line =~ /^\s*(add_column|change_column)(\s+|\().*$/
|
129
|
+
#add_column(table_name, column_name, type, options)
|
130
|
+
#change_column(table_name, column_name, type, options)
|
131
|
+
attr_table= line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\2').strip
|
132
|
+
attr_name = line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\3').strip
|
133
|
+
attr_type = line.sub(/^\s*[a-z_]+(\s+|\()\:([a-z_]+).*,.*\:([a-z_]+).*,.*\:([a-z_]+).*$/,'\4').strip
|
134
|
+
|
135
|
+
schema[attr_table] ||= {}
|
136
|
+
attr_changed = !!schema[attr_table][attr_name]
|
137
|
+
schema[attr_table][attr_name] ||= {}
|
138
|
+
schema[attr_table][attr_name]["type"] = attr_type
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
models.reject{ |k,v| k[0] == "@"}.each do |name,model|
|
148
|
+
model['has_one'] ||= {}
|
149
|
+
model['has_many'] ||= {}
|
150
|
+
model['belongs_to'] ||= {}
|
151
|
+
model['attributes'] ||= {}
|
152
|
+
model['methods'] ||= {}
|
153
|
+
model['code'] ||= {}
|
154
|
+
model['code']['top'] ||= []
|
155
|
+
model['code']['before'] ||= []
|
156
|
+
model['code']['after'] ||= []
|
157
|
+
model['code']['controller'] ||= []
|
158
|
+
|
159
|
+
table_name = name.pluralize.underscore.downcase
|
160
|
+
next unless schema[table_name]
|
161
|
+
|
162
|
+
attrs = {}
|
163
|
+
model['attributes'].each do |k,v|
|
164
|
+
attrs[k] ||= {}
|
165
|
+
attrs[k]['type'] = v['type'].sub(/yaml|hash|object|cache/i, "text")
|
166
|
+
end
|
167
|
+
model['belongs_to'].each do |k,v|
|
168
|
+
attrs[k+"_id"] ||= {}
|
169
|
+
attrs[k+"_id"]['type'] = 'integer'
|
170
|
+
if v['polymorphic']
|
171
|
+
attrs[k+"_type"] ||= {}
|
172
|
+
attrs[k+"_type"]['type'] = 'string'
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
changes = []
|
177
|
+
summary = []
|
178
|
+
verb = ""
|
179
|
+
attrs.each do |k,v|
|
180
|
+
if schema[table_name][k]
|
181
|
+
if schema[table_name][k]['type'] != v['type']
|
182
|
+
verb = "Change" if verb == ""
|
183
|
+
verb = "Update" if verb == "Add"
|
184
|
+
summary << k
|
185
|
+
changes << "change_column :#{table_name}, :#{k}, :#{v['type']}"
|
186
|
+
end
|
187
|
+
else
|
188
|
+
verb = "Add" if verb == ""
|
189
|
+
verb = "Update" if verb == "Change"
|
190
|
+
summary << k
|
191
|
+
changes << "add_column :#{table_name}, :#{k}, :#{v['type']}"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
summary = [summary.first,"others"] if summary.count > 2
|
196
|
+
summary = verb + summary.join("_and_").camelize
|
197
|
+
v2 = verb == "Add" ? "To" : "On"
|
198
|
+
if changes.count > 0
|
199
|
+
index = ""
|
200
|
+
while Dir.glob("#{Rails.root}/db/migrate/*_#{summary}#{v2}#{name.pluralize}#{index}".underscore.downcase+".rb").count > 0
|
201
|
+
index = 1 if index = ""
|
202
|
+
index += 1
|
203
|
+
end
|
204
|
+
|
205
|
+
puts DateTime.now.strftime("%Y%m%d%H%M%S") + "_#{summary}#{v2}#{name.pluralize}#{index}".underscore.downcase + ".rb"
|
206
|
+
File.write("#{Rails.root}/db/migrate/" + DateTime.now.strftime("%Y%m%d%H%M%S") + "_#{summary}#{v2}#{name.pluralize}#{index}".underscore.downcase+".rb", "class #{summary}#{v2}#{ name.pluralize }#{index} < ActiveRecord::Migration
|
207
|
+
def change
|
208
|
+
#{changes.join("\n ")}
|
209
|
+
end
|
210
|
+
end
|
211
|
+
")
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
62
217
|
def scaffolds(options = "")
|
63
218
|
parse
|
64
219
|
|
220
|
+
schema_diff
|
221
|
+
|
65
222
|
puts("","--- SCAFFOLDS ---","") unless @quiet
|
66
223
|
|
67
224
|
models.reject{ |k,v| k[0] == "@"}.each do |name,model|
|
@@ -92,7 +249,8 @@ class Generator
|
|
92
249
|
`#{sc} #{options.gsub("--force","")} --skip --no-migrations`
|
93
250
|
|
94
251
|
File.delete("#{Rails.root}/app/views/#{name.underscore.pluralize}/_form.html.erb")
|
95
|
-
sc = "rails g scaffold #{name} " + model['attributes'].reject{ | k, v| v['hidden'] }.map{ |k,v| k + ':' + v['type'].sub(/yaml|hash|object|cache/i,"text") }.join(' ') + " " +
|
252
|
+
sc = "rails g scaffold #{name} " + model['attributes'].reject{ | k, v| v['hidden'] }.map{ |k,v| k + ':' + v['type'].sub(/yaml|hash|object|cache/i,"text") }.join(' ') + " " +
|
253
|
+
model['belongs_to'].reject{ |k, v| v['hidden'] }.map{ |k,v| k + ':references' + (v['polymorphic'] ? "{polymorphic}" : "") }.join(' ')
|
96
254
|
puts("form: #{sc}") unless @quiet
|
97
255
|
`#{sc} #{options.gsub("--force","")} --skip --no-migrations`
|
98
256
|
end if !options.include?("--no-scaffold") && !options.include?("--migrations-only")
|
@@ -113,6 +271,11 @@ class Generator
|
|
113
271
|
`cd #{Rails::root} && #{sc} --skip --no-test-framework`
|
114
272
|
end if options.include?("--migrations-only")
|
115
273
|
|
274
|
+
|
275
|
+
schema_diff() unless options.include?("--migrations-only")
|
276
|
+
#$ rails generate migration AddPartNumberToProducts part_number:string:index
|
277
|
+
#sc = "rails g model #{name} " + model['attributes'].map{ |k,v| k + ':' + v['type'].sub(/yaml|hash|object|cache/i,"text") }.join(' ') + " " + model['belongs_to'].map{ |k,v| k + ':references' + (v['polymorphic'] ? "{polymorphic}" : "") }.join(' ')
|
278
|
+
|
116
279
|
if options.include?("--migrations-only")
|
117
280
|
cleanup(ddl: true, mixins: false)
|
118
281
|
File.open("#{Rails.root}/db/schema.yaml", "w") do |f|
|
data/lib/yaddl/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaddl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-05-
|
12
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|