modalfields 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.4
1
+ 1.1.6
@@ -165,7 +165,7 @@ module ModalFields
165
165
  # changes can be easily reviewed.
166
166
  def update(modify=true)
167
167
  dbmodels.each do |model, file|
168
- new_fields, modified_fields, deleted_fields = diff(model)
168
+ new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
169
169
  unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
170
170
  pre, start_fields, fields, end_fields, post = split_model_file(file)
171
171
  deleted_names = deleted_fields.map{|f| f.name.to_s}
@@ -199,10 +199,11 @@ module ModalFields
199
199
 
200
200
  def check
201
201
  dbmodels.each do |model, file|
202
- new_fields, modified_fields, deleted_fields = diff(model)
202
+ new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
203
203
  unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
204
204
  rel_file = file.sub(/\A#{Rails.root}/,'')
205
205
  puts "#{model} (#{rel_file}):"
206
+ puts " (deleted)" if deleted_model
206
207
  [['+',new_fields],['*',modified_fields],['-',deleted_fields]].each do |prefix, fields|
207
208
  puts fields.map{|field| " #{prefix} #{field}"}*"\n" unless fields.empty?
208
209
  # TODO: report index differences
@@ -212,6 +213,71 @@ module ModalFields
212
213
  end
213
214
  end
214
215
 
216
+ def migration
217
+ up = ""
218
+ down = ""
219
+ dbmodels.each do |model, file|
220
+ new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
221
+ unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
222
+ up << "\n"
223
+ down << "\n"
224
+ rel_file = file.sub(/\A#{Rails.root}/,'')
225
+ if deleted_model && modified_fields.empty? && new_fields.empty?
226
+ up << " create_table #{model.table_name.to_sym.inspect} do |t|\n"
227
+ deleted_fields.each do |field|
228
+ up << " t.#{field.type} #{field.name.inspect}"
229
+ unless field.attributes.empty?
230
+ up << ", " + field.attributes.inspect.unwrap('{}')
231
+ end
232
+ up << "\n"
233
+ end
234
+ up << " end\n"
235
+ down << " drop_table #{model.table_name.to_sym.inspect}\n"
236
+ else
237
+ deleted_fields.each do |field|
238
+ up << " add_column #{model.table_name.to_sym.inspect}, #{field.name.inspect}, #{field.type.inspect}"
239
+ unless field.attributes.empty?
240
+ up << ", " + field.attributes.inspect.unwrap('{}')
241
+ end
242
+ up << "\n"
243
+ down << " remove_column #{model.table_name.to_sym.inspect}, #{field.name.inspect}\n"
244
+ end
245
+ modified_fields.each do |field|
246
+ changed = model.fields_info.find{|f| f.name==field.name}
247
+ up << " change_column #{model.table_name.to_sym.inspect}, #{changed.name.inspect}, #{changed.type.inspect}"
248
+ unless changed.attributes.empty?
249
+ up << ", " + changed.attributes.inspect.unwrap('{}')
250
+ end
251
+ up << "\n"
252
+ down << " change_column #{model.table_name.to_sym.inspect}, #{field.name.inspect}, #{field.type.inspect}"
253
+ unless field.attributes.empty?
254
+ down << ", " + field.attributes.inspect.unwrap('{}')
255
+ end
256
+ down << "\n"
257
+ end
258
+ new_fields.each do |field|
259
+ up << " remove_column #{model.table_name.to_sym.inspect}, #{field.name.inspect}\n"
260
+ down << " add_column #{model.table_name.to_sym.inspect}, #{field.name.inspect}, #{field.type.inspect}"
261
+ unless field.attributes.empty?
262
+ down << ", " + field.attributes.inspect.unwrap('{}')
263
+ end
264
+ down << "\n"
265
+ end
266
+ end
267
+ # TODO: indices
268
+ end
269
+ end
270
+ unless up.blank?
271
+ puts "\n\# up:"
272
+ puts up
273
+ end
274
+ unless down.blank?
275
+ puts "\n\# down:"
276
+ puts down
277
+ end
278
+ puts ""
279
+ end
280
+
215
281
  def validate(declaration)
216
282
  definition = definitions[declaration.type.to_sym]
217
283
  raise "Field type #{declaration.type} not defined" unless definition
@@ -263,6 +329,7 @@ module ModalFields
263
329
  def diff(model)
264
330
  # model.columns will fail if the table does not exist
265
331
  existing_fields = model.columns rescue []
332
+ deleted_model = existing_fields.empty?
266
333
  # up to ActiveRecord 3.1 we had primary_key_name in AssociationReflection; not itis foreign_key
267
334
  association_fields = model.reflect_on_all_associations(:belongs_to).map{ |r|
268
335
  r.respond_to?(:primary_key_name) ? r.primary_key_name : r.foreign_key
@@ -337,7 +404,7 @@ module ModalFields
337
404
  end
338
405
  FieldDeclaration.new(f.name.to_sym, f.type.to_sym, [], attributes)
339
406
  }
340
- [new_fields, modified_fields, deleted_fields]
407
+ [new_fields, modified_fields, deleted_fields, deleted_model]
341
408
  end
342
409
 
343
410
  # Break up the lines of a model definition file into sections delimited by the fields declaration.
@@ -0,0 +1,6 @@
1
+ namespace :fields do
2
+ desc "Show migration required to sync schema with field declarations"
3
+ task :migration=>:environment do
4
+ ModalFields.migration
5
+ end
6
+ end
data/modalfields.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "modalfields"
8
- s.version = "1.1.5"
8
+ s.version = "1.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/modalfields/tasks.rb",
33
33
  "lib/tasks/check.rake",
34
34
  "lib/tasks/migrate.rake",
35
+ "lib/tasks/migration.rake",
35
36
  "lib/tasks/update.rake",
36
37
  "modalfields.gemspec",
37
38
  "test/create_database.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalfields
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -178,6 +178,7 @@ files:
178
178
  - lib/modalfields/tasks.rb
179
179
  - lib/tasks/check.rake
180
180
  - lib/tasks/migrate.rake
181
+ - lib/tasks/migration.rake
181
182
  - lib/tasks/update.rake
182
183
  - modalfields.gemspec
183
184
  - test/create_database.rb
@@ -213,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
213
214
  version: '0'
214
215
  segments:
215
216
  - 0
216
- hash: 3314962331882742653
217
+ hash: 4589863407518218042
217
218
  required_rubygems_version: !ruby/object:Gem::Requirement
218
219
  none: false
219
220
  requirements: