mini_record 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44eb9e5c3b23585ea142c23f9b79cc8783a06204
4
- data.tar.gz: 78dcc1beff902922d33bfc660b819f22d73b8061
3
+ metadata.gz: 784a891255be111d6c3d06375ee93264c7692dfe
4
+ data.tar.gz: 45ffe1d08143af8c90cbe2f3902765b234d99fb8
5
5
  SHA512:
6
- metadata.gz: 1436a23b6d6fa2327ee7545258f3059b068cc0c85dd9964ff8d8ccc1b634fcd277c5a82d5644c6c9092e433a993e2e681f053f3bd7b70e8426bdc8e8e2ee91bd
7
- data.tar.gz: 662eeecaa4f45ff27854ec646e381b71959ac143766731ee585394c3b3f1b110778171878d27f684cc5cf7971332d22ea187fba58eaada58437f02ff55df1274
6
+ metadata.gz: 1efebe1b000c99b4eaa89ff150a7d782ca2a94db3f03d8fe782fa53656b243dca7ff3c419a9a93e2ef90f9a58ed83835985ad727c1cc995eb7e3d17debca98ce
7
+ data.tar.gz: 5198ecfa9d0f2169ebfceecffdb625b72b2e274bbf8b06875dab5ed832284a3e630a7a6a59b7084b5ac69c85d41648e2f5af6dc99ea385cea93d49a1af820e25
data/Gemfile CHANGED
@@ -3,11 +3,18 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in mini_record.gemspec
4
4
  gem 'rake'
5
5
  gem 'minitest'
6
+
7
+ # Test database adapters with "rake test DB=mysql2"
6
8
  gem 'sqlite3'
7
- gem 'mysql2'
8
- gem 'mysql'
9
9
  gem 'pg'
10
- gem 'activerecord', '<= 3.2'
10
+ gem 'mysql'
11
+ gem 'mysql2'
12
+
13
+ # Uncomment to test older versions, then "bundle update"
14
+ # gem 'activerecord', '<= 3.2'
15
+ # gem 'activerecord', '~> 4.0.0'
16
+ # gem 'activerecord', '~> 4.1.0'
17
+ gem 'activerecord', '>= 4.2.0'
11
18
 
12
19
  group :test do
13
20
  gem 'foreigner', '>= 1.4.2'
@@ -3,7 +3,7 @@ module MiniRecord
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)
5
5
  end
6
-
6
+
7
7
  module ClassMethods
8
8
  def init_table_definition(connection)
9
9
  #connection.create_table(table_name) unless connection.table_exists?(table_name)
@@ -23,7 +23,7 @@ module MiniRecord
23
23
  "Unsupported number of args for ActiveRecord::ConnectionAdapters::TableDefinition.new()"
24
24
  end
25
25
  end
26
-
26
+
27
27
  def schema_tables
28
28
  @@_schema_tables ||= []
29
29
  end
@@ -139,7 +139,7 @@ module MiniRecord
139
139
  alias :col :field
140
140
 
141
141
  def timestamps
142
- field :created_at, :updated_at, :as => :datetime, :null => false
142
+ field :created_at, :updated_at, :as => :datetime
143
143
  end
144
144
 
145
145
  def reset_table_definition!
@@ -178,9 +178,9 @@ module MiniRecord
178
178
  end
179
179
 
180
180
  def clear_tables!(dry_run = false)
181
- return unless MiniRecord.configuration.destructive == true
181
+ return unless MiniRecord.configuration.destructive == true
182
182
  (connection.tables - schema_tables).each do |name|
183
- logger.debug "[MiniRecord] Dropping table #{name}"
183
+ logger.debug "[MiniRecord] Dropping table #{name}" if logger
184
184
  unless dry_run
185
185
  connection.drop_table(name)
186
186
  schema_tables.delete(name)
@@ -196,12 +196,12 @@ module MiniRecord
196
196
 
197
197
  # Remove foreign keys for indexes with :foreign=>false option
198
198
  def remove_foreign_keys(dry_run)
199
- return unless MiniRecord.configuration.destructive == true
199
+ return unless MiniRecord.configuration.destructive == true
200
200
  indexes.each do |name, options|
201
201
  if options[:foreign]==false
202
202
  foreign_key = foreign_keys.detect { |fk| fk.options[:column] == options[:column].to_s }
203
203
  if foreign_key
204
- logger.debug "[MiniRecord] Removing Foreign Key #{foreign_key.options[:name]} on table #{table_name}"
204
+ logger.debug "[MiniRecord] Removing Foreign Key #{foreign_key.options[:name]} on table #{table_name}" if logger
205
205
  connection.remove_foreign_key(table_name, :name => foreign_key.options[:name]) unless dry_run
206
206
  foreign_keys.delete(foreign_key)
207
207
  end
@@ -216,7 +216,7 @@ module MiniRecord
216
216
  column = options[:column].to_s
217
217
  unless foreign_keys.detect { |fk| fk[:options][:column] == column }
218
218
  to_table = reflect_on_all_associations.detect { |a| a.foreign_key.to_s==column }.table_name
219
- logger.debug "[MiniRecord] Adding Foreign Key on #{table_name} to #{to_table}"
219
+ logger.debug "[MiniRecord] Adding Foreign Key on #{table_name} to #{to_table}" if logger
220
220
  connection.add_foreign_key(table_name, to_table, options) unless dry_run
221
221
  foreign_keys << { :options=> { :column=>column } }
222
222
  end
@@ -224,6 +224,51 @@ module MiniRecord
224
224
  end
225
225
  end
226
226
 
227
+ # Helper to determine if/how a field will change
228
+ def field_attr_changes(field_name)
229
+ field = field_name.to_s
230
+ changed = false # flag
231
+ new_attr = {}
232
+
233
+ # Next, iterate through our extended attributes, looking for any differences
234
+ # This catches stuff like :null, :precision, etc
235
+ # Ignore junk attributes that different versions of Rails include
236
+ [:name, :limit, :precision, :scale, :default, :null].each do |att|
237
+ value = fields[field][att]
238
+ value = true if att == :null && value.nil?
239
+
240
+ # Skip unspecified limit/precision/scale as DB will set them to defaults,
241
+ # and on subsequent runs, this will be erroneously detected as a change.
242
+ next if value.nil? and [:limit, :precision, :scale].include?(att)
243
+
244
+ old_value = fields_in_db[field].send(att)
245
+ # puts "#{field_name}[#{att}] = #{value.inspect} vs #{old_value.inspect}"
246
+
247
+ attr_changed = false
248
+ if att == :default
249
+ # Rails 4.2 changed behavior to pass DB values directly through, so we must re-map
250
+ if value.to_s =~ /^(false|f|0)$/i
251
+ attr_changed = true if old_value.to_s !~ /^(false|f|0)$/i
252
+ elsif value.to_s =~ /^(true|t|1)$/i
253
+ attr_changed = true if old_value.to_s !~ /^(true|t|1)$/i
254
+ elsif value.to_s != old_value.to_s
255
+ attr_changed = true
256
+ end
257
+ elsif value != old_value
258
+ attr_changed = true
259
+ end
260
+
261
+ if attr_changed
262
+ logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}##{att} " +
263
+ "from #{old_value.inspect} to #{value.inspect}" if logger
264
+ new_attr[att] = value
265
+ changed ||= attr_changed
266
+ end
267
+ end
268
+
269
+ [new_attr, changed]
270
+ end
271
+
227
272
  # dry-run
228
273
  def auto_upgrade_dry
229
274
  auto_upgrade!(true)
@@ -240,7 +285,7 @@ module MiniRecord
240
285
  # If table doesn't exist, create it
241
286
  unless connection.tables.include?(table_name)
242
287
  class << connection; attr_accessor :table_definition; end unless connection.respond_to?(:table_definition=)
243
- logger.debug "[MiniRecord] Creating Table #{table_name}"
288
+ logger.debug "[MiniRecord] Creating Table #{table_name}" if logger
244
289
  unless dry_run
245
290
  connection.table_definition = table_definition
246
291
  connection.create_table(table_name, *create_table_options)
@@ -274,7 +319,7 @@ module MiniRecord
274
319
  unless connection.tables.include?(table.to_s)
275
320
  foreign_key = association.options[:foreign_key] || association.foreign_key
276
321
  association_foreign_key = association.options[:association_foreign_key] || association.association_foreign_key
277
- logger.debug "[MiniRecord] Creating Join Table #{table} with keys #{foreign_key} and #{association_foreign_key}"
322
+ logger.debug "[MiniRecord] Creating Join Table #{table} with keys #{foreign_key} and #{association_foreign_key}" if logger
278
323
  unless dry_run
279
324
  connection.create_table(table, :id => false) do |t|
280
325
  t.integer foreign_key
@@ -283,7 +328,7 @@ module MiniRecord
283
328
  end
284
329
  index_name = connection.index_name(table, :column => [foreign_key, association_foreign_key])
285
330
  index_name = index_name[0...connection.index_name_length] if index_name.length > connection.index_name_length
286
- logger.debug "[MiniRecord] Creating Join Table Index #{index_name} (#{foreign_key}, #{association_foreign_key}) on #{table}"
331
+ logger.debug "[MiniRecord] Creating Join Table Index #{index_name} (#{foreign_key}, #{association_foreign_key}) on #{table}" if logger
287
332
  connection.add_index table, [foreign_key, association_foreign_key], :name => index_name, :unique => true unless dry_run or suppressed_indexes[association.name]
288
333
  end
289
334
  # Add join table to our schema tables
@@ -300,77 +345,48 @@ module MiniRecord
300
345
 
301
346
  # Group Destructive Actions
302
347
  if MiniRecord.configuration.destructive == true and connection.tables.include?(table_name)
303
-
348
+
304
349
  # Rename fields
305
350
  rename_fields.each do |old_name, new_name|
306
351
  old_column = fields_in_db[old_name.to_s]
307
352
  new_column = fields_in_db[new_name.to_s]
308
353
  if old_column && !new_column
309
- logger.debug "[MiniRecord] Renaming column #{table_name}.#{old_column.name} to #{new_name}"
354
+ logger.debug "[MiniRecord] Renaming column #{table_name}.#{old_column.name} to #{new_name}" if logger
310
355
  connection.rename_column(table_name, old_column.name, new_name) unless dry_run
311
356
  end
312
357
  end
313
-
358
+
314
359
  # Remove fields from db no longer in schema
315
360
  columns_to_delete = fields_in_db.keys - fields.keys & fields_in_db.keys
316
361
  columns_to_delete.each do |field|
317
362
  column = fields_in_db[field]
318
- logger.debug "[MiniRecord] Removing column #{table_name}.#{column.name}"
363
+ logger.debug "[MiniRecord] Removing column #{table_name}.#{column.name}" if logger
319
364
  connection.remove_column table_name, column.name unless dry_run
320
365
  end
321
-
366
+
322
367
  # Change attributes of existent columns
323
368
  (fields.keys & fields_in_db.keys).each do |field|
324
369
  if field != primary_key #ActiveRecord::Base.get_primary_key(table_name)
325
- changed = false # flag
326
- new_attr = {}
327
-
328
- # Special catch for precision/scale, since *both* must be specified together
329
- # Always include them in the attr struct, but they'll only get applied if changed = true
330
- new_attr[:precision] = fields[field][:precision]
331
- new_attr[:scale] = fields[field][:scale]
332
-
333
- # If we have precision this is also the limit
334
- fields[field][:limit] ||= fields[field][:precision]
335
-
336
- # Next, iterate through our extended attributes, looking for any differences
337
- # This catches stuff like :null, :precision, etc
338
- # Ignore junk attributes that different versions of Rails include
339
- [:name, :limit, :precision, :scale, :default, :null].each do |att|
340
- value = fields[field][att]
341
- value = true if att == :null && value.nil?
342
-
343
- # Skip unspecified limit/precision/scale as DB will set them to defaults,
344
- # and on subsequent runs, this will be erroneously detected as a change.
345
- next if value.nil? and [:limit, :precision, :scale].include?(att)
346
-
347
- old_value = fields_in_db[field].send(att)
348
- if value != old_value
349
- logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}##{att} " +
350
- "from #{old_value.inspect} to #{value.inspect}" if logger
351
- new_attr[att] = value
352
- changed = true
353
- end
354
- end
370
+ new_attr, changed = field_attr_changes(field)
355
371
 
356
372
  # Change the column if applicable
357
373
  new_type = fields[field].type.to_sym
358
374
  if changed
359
- logger.debug "[MiniRecord] Changing column #{table_name}.#{field} to new type #{new_type}"
375
+ logger.debug "[MiniRecord] Changing column #{table_name}.#{field} to new type #{new_type}" if logger
360
376
  connection.change_column table_name, field, new_type, new_attr unless dry_run
361
377
  end
362
378
  end
363
379
  end
364
-
380
+
365
381
  remove_foreign_keys(dry_run) if connection.respond_to?(:foreign_keys)
366
382
 
367
383
  # Remove old index
368
384
  index_names = indexes.collect{ |name, opts| (opts[:name] || name).to_s }
369
385
  (indexes_in_db.keys - index_names).each do |name|
370
- logger.debug "[MiniRecord] Removing index #{name} on #{table_name}"
386
+ logger.debug "[MiniRecord] Removing index #{name} on #{table_name}" if logger
371
387
  connection.remove_index(table_name, :name => name) unless dry_run
372
388
  end
373
-
389
+
374
390
  end
375
391
 
376
392
  if connection.tables.include?(table_name)
@@ -381,7 +397,7 @@ module MiniRecord
381
397
  options = {:limit => column.limit, :precision => column.precision, :scale => column.scale}
382
398
  options[:default] = column.default unless column.default.nil?
383
399
  options[:null] = column.null unless column.null.nil?
384
- logger.debug "[MiniRecord] Adding column #{table_name}.#{column.name}"
400
+ logger.debug "[MiniRecord] Adding column #{table_name}.#{column.name}" if logger
385
401
  connection.add_column table_name, column.name, column.type.to_sym, options unless dry_run
386
402
  end
387
403
  end
@@ -391,7 +407,7 @@ module MiniRecord
391
407
  options = options.dup
392
408
  index_name = (options[:name] || name).to_s
393
409
  unless connection.indexes(table_name).detect { |i| i.name == index_name }
394
- logger.debug "[MiniRecord] Adding index #{index_name} #{options[:column].inspect} on #{table_name}"
410
+ logger.debug "[MiniRecord] Adding index #{index_name} #{options[:column].inspect} on #{table_name}" if logger
395
411
  connection.add_index(table_name, options.delete(:column), options) unless dry_run
396
412
  end
397
413
  end
@@ -1,3 +1,3 @@
1
1
  module MiniRecord
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'rubygems' unless defined?(Gem)
2
2
  require 'bundler/setup'
3
+ require 'logger'
3
4
  require 'mini_record'
4
5
  require 'minitest/autorun'
5
6
 
@@ -39,24 +40,25 @@ class ActiveRecord::Base
39
40
 
40
41
  def auto_upgrade!(*args)
41
42
  ActiveRecord::Base.logs = StringIO.new
42
- ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(ActiveRecord::Base.logs)
43
+ ActiveRecord::Base.logger = Logger.new(ActiveRecord::Base.logs)
43
44
  silence_stream(STDERR) { super }
44
45
  end
45
46
 
46
47
  def auto_upgrade_dry
47
48
  ActiveRecord::Base.logs = StringIO.new
48
- ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(ActiveRecord::Base.logs)
49
+ ActiveRecord::Base.logger = Logger.new(ActiveRecord::Base.logs)
49
50
  silence_stream(STDERR) { super }
50
51
  end
51
52
  end
52
53
  end # ActiveRecord::Base
53
54
 
54
- # Setup Adatper
55
+ # Setup Adapter
56
+ puts "Testing with DB=#{ENV['DB'] || 'sqlite'}"
55
57
  case ENV['DB']
56
58
  when 'mysql'
57
- ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'test', :user => 'root')
59
+ ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'test', :username => 'root')
58
60
  when 'mysql2'
59
- ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test', :user => 'root')
61
+ ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test', :username => 'root')
60
62
  Bundler.require(:test) # require 'foreigner'
61
63
  Foreigner.load
62
64
  when 'pg', 'postgresql'
@@ -10,7 +10,7 @@ describe MiniRecord do
10
10
  conn.tables.each { |table| silence_stream(STDERR) { conn.execute "DROP TABLE IF EXISTS #{table}" } }
11
11
  end
12
12
 
13
- ActiveRecord::Base.descendants.each { |klass| Object.send(:remove_const, klass.to_s) if Object.const_defined?(klass.to_s) }
13
+ ActiveRecord::Base.descendants.each { |klass| Object.send(:remove_const, klass.to_s) if Object.const_defined?(klass.name.to_s) }
14
14
  ActiveSupport::DescendantsTracker.direct_descendants(ActiveRecord::Base).clear
15
15
  end
16
16
 
@@ -141,7 +141,7 @@ describe MiniRecord do
141
141
  assert_includes Foo.db_indexes, 'by_customer'
142
142
  # Run auto_upgrade! again and ensure no statements issued.
143
143
  Foo.auto_upgrade!
144
- assert_empty Foo.queries
144
+ refute_match /schema\s+change/, Foo.queries
145
145
  end
146
146
 
147
147
  it 'does not add already defined composite indexes' do
@@ -156,7 +156,7 @@ describe MiniRecord do
156
156
  assert_includes Foo.db_indexes, 'by_region_and_customer'
157
157
  # Run auto_upgrade! again and ensure no statements issued.
158
158
  Foo.auto_upgrade!
159
- assert_empty Foo.queries
159
+ refute_match /schema\s+change/, Foo.queries
160
160
  end
161
161
 
162
162
  it 'supports indexes with symbols for names' do
@@ -169,7 +169,7 @@ describe MiniRecord do
169
169
  assert_includes Foo.db_indexes, 'idx_for_some_field'
170
170
  # Run auto_upgrade! again and ensure no statements issued.
171
171
  Foo.auto_upgrade!
172
- assert_empty Foo.queries
172
+ refute_match /schema\s+change/, Foo.queries
173
173
  end
174
174
 
175
175
  it 'works with STI' do
@@ -244,7 +244,7 @@ describe MiniRecord do
244
244
  end
245
245
 
246
246
  it 'allow custom query' do
247
- skip unless conn.adapter_name =~ /mysql/i
247
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
248
248
 
249
249
  class Foo < ActiveRecord::Base
250
250
  col :name, :as => "ENUM('foo','bar')"
@@ -253,7 +253,7 @@ describe MiniRecord do
253
253
  assert_match /ENUM/, Foo.queries
254
254
 
255
255
  Foo.auto_upgrade!
256
- assert_empty Foo.queries
256
+ refute_match /schema\s+change/, Foo.queries
257
257
  assert_equal %w[id name], Foo.db_columns
258
258
  assert_equal %w[id name], Foo.schema_columns
259
259
 
@@ -282,6 +282,8 @@ describe MiniRecord do
282
282
  end
283
283
 
284
284
  it 'removes a column and index when relation is removed' do
285
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
286
+
285
287
  class Foo < ActiveRecord::Base
286
288
  key :name
287
289
  belongs_to :image, :polymorphic => true
@@ -304,7 +306,7 @@ describe MiniRecord do
304
306
  refute_includes Foo.db_columns, 'image_id'
305
307
  assert_empty Foo.db_indexes
306
308
  end
307
-
309
+
308
310
  it 'doesnt remove a column and index when relation is removed and destructive is false' do
309
311
  MiniRecord.configuration.destructive = false
310
312
  class Foo < ActiveRecord::Base
@@ -397,16 +399,17 @@ describe MiniRecord do
397
399
 
398
400
  it 'should not override previous defined column relation' do
399
401
  class Foo < ActiveRecord::Base
400
- key :user, :as => :references, :null => false, :limit => 4
402
+ key :user, :as => :references, :null => false, :limit => 4, :default => 42
401
403
  belongs_to :user
402
404
  end
403
405
  Foo.auto_upgrade!
404
406
  assert_equal 4, Foo.db_fields[:user_id].limit
405
407
  assert_equal false, Foo.db_fields[:user_id].null
408
+ assert_equal "42", Foo.db_fields[:user_id].default.to_s
406
409
  end
407
410
 
408
411
  it 'add/remove foreign key with :foreign option, when Foreigner gem used on mysql' do
409
- skip unless conn.adapter_name =~ /mysql/i
412
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
410
413
 
411
414
  class Book < ActiveRecord::Base
412
415
  belongs_to :publisher
@@ -428,10 +431,10 @@ describe MiniRecord do
428
431
 
429
432
  assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
430
433
  end
431
-
434
+
432
435
  it 'doesnt remove foreign key with :foreign option, when Foreigner gem used on mysql and destructive = false' do
433
436
  MiniRecord.configuration.destructive = false
434
- skip unless conn.adapter_name =~ /mysql/i
437
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
435
438
 
436
439
  class Book < ActiveRecord::Base
437
440
  belongs_to :publisher
@@ -456,7 +459,7 @@ describe MiniRecord do
456
459
  end
457
460
 
458
461
  it 'add/remove named foreign key with :foreign option, when Foreigner gem used on mysql' do
459
- skip unless conn.adapter_name =~ /mysql/i
462
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
460
463
 
461
464
  class Book < ActiveRecord::Base
462
465
  belongs_to :publisher
@@ -479,10 +482,10 @@ describe MiniRecord do
479
482
  assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
480
483
  Object.send(:remove_const, :Book)
481
484
  end
482
-
485
+
483
486
  it 'doesnt remove named foreign key with :foreign option, when Foreigner gem used on mysql and destructive = false' do
484
487
  MiniRecord.configuration.destructive = false
485
- skip unless conn.adapter_name =~ /mysql/i
488
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
486
489
 
487
490
  class Book < ActiveRecord::Base
488
491
  belongs_to :publisher
@@ -508,7 +511,7 @@ describe MiniRecord do
508
511
  end
509
512
 
510
513
  it 'support :foreign option in the index with custom :foreign_key in the belong_to association' do
511
- skip unless conn.adapter_name =~ /mysql/i
514
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
512
515
 
513
516
  class Book < ActiveRecord::Base
514
517
  belongs_to :second_publisher, :foreign_key => 'second_publisher_id', :class_name => 'Publisher'
@@ -530,10 +533,10 @@ describe MiniRecord do
530
533
 
531
534
  assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'second_publisher_id'}
532
535
  end
533
-
536
+
534
537
  it 'support :foreign option in the index with custom :foreign_key in the belong_to association and wont remove if destructive = false' do
535
538
  MiniRecord.configuration.destructive = false
536
- skip unless conn.adapter_name =~ /mysql/i
539
+ skip "foreign key tests only for mysql" unless conn.adapter_name =~ /mysql/i
537
540
 
538
541
  class Book < ActiveRecord::Base
539
542
  belongs_to :second_publisher, :foreign_key => 'second_publisher_id', :class_name => 'Publisher'
@@ -579,6 +582,8 @@ describe MiniRecord do
579
582
 
580
583
  describe 'relation #habtm' do
581
584
  it 'creates a join table with indexes for has_and_belongs_to_many relations' do
585
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
586
+
582
587
  tables = Tool.connection.tables
583
588
  assert_includes tables, 'purposes_tools'
584
589
 
@@ -592,19 +597,22 @@ describe MiniRecord do
592
597
  end
593
598
 
594
599
  it 'drops join table if has_and_belongs_to_many relation is deleted' do
600
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
601
+
595
602
  Tool.schema_tables.delete('purposes_tools')
596
603
  refute_includes ActiveRecord::Base.schema_tables, 'purposes_tools'
597
604
 
598
605
  ActiveRecord::Base.clear_tables!
599
606
  refute_includes Tool.connection.tables, 'purposes_tools'
600
607
  end
601
-
608
+
602
609
  it 'keeps join table if has_and_belongs_to_many relation is deleted and destructive = false' do
603
610
  MiniRecord.configuration.destructive = false
604
-
611
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
612
+
605
613
  tables = Tool.connection.tables
606
614
  assert_includes tables, 'purposes_tools'
607
-
615
+
608
616
  Tool.schema_tables.delete('purposes_tools')
609
617
  refute_includes ActiveRecord::Base.schema_tables, 'purposes_tools'
610
618
 
@@ -613,6 +621,8 @@ describe MiniRecord do
613
621
  end
614
622
 
615
623
  it 'has_and_belongs_to_many with custom join_table and foreign keys' do
624
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
625
+
616
626
  class Foo < ActiveRecord::Base
617
627
  has_and_belongs_to_many :watchers, :join_table => :watching, :foreign_key => :custom_foo_id, :association_foreign_key => :customer_id
618
628
  end
@@ -626,6 +636,8 @@ describe MiniRecord do
626
636
  end
627
637
 
628
638
  it 'creates a join table with indexes with long indexes names' do
639
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
640
+
629
641
  class Foo < ActiveRecord::Base
630
642
  has_and_belongs_to_many :people, :join_table => :long_people,
631
643
  :foreign_key => :custom_long_long_long_long_id,
@@ -638,6 +650,8 @@ describe MiniRecord do
638
650
  end
639
651
 
640
652
  it 'creates a join table without an index when suppressed for has_and_belongs_to_many relations' do
653
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
654
+
641
655
  class Foo < ActiveRecord::Base
642
656
  has_and_belongs_to_many :bars
643
657
  suppress_index :bars
@@ -648,6 +662,8 @@ describe MiniRecord do
648
662
  end
649
663
 
650
664
  it 'adds unique index' do
665
+ skip "habtm key tests only for mysql" unless conn.adapter_name =~ /mysql/i
666
+
651
667
  page = Page.create(:title => 'Foo')
652
668
  photogallery = Photogallery.create(:title => 'Bar')
653
669
  assert photogallery.valid?
@@ -698,19 +714,19 @@ describe MiniRecord do
698
714
  case conn.adapter_name
699
715
  when /sqlite/i
700
716
  # In sqlite there is a difference between limit: 4 and limit: 11
701
- assert_match 'altered', Foo.queries
702
- assert_equal 4, Foo.db_fields[:number].limit
717
+ assert_match 'foos.number#limit', Foo.queries
703
718
  assert_equal 4, Foo.schema_fields[:number].limit
719
+ assert_equal 4, Foo.db_fields[:number].limit
704
720
  when /mysql/i
705
721
  # In mysql according to this: http://goo.gl/bjZE7 limit: 4 is same of limit:11
706
- assert_empty Foo.queries
707
- assert_equal 4, Foo.db_fields[:number].limit
722
+ refute_match /schema\s+change/, Foo.queries
708
723
  assert_equal 4, Foo.schema_fields[:number].limit
724
+ assert_equal 4, Foo.db_fields[:number].limit
709
725
  when /postgres/i
710
726
  # In postgres limit: 4 will be translated to nil
711
727
  assert_match /ALTER COLUMN "number" TYPE integer$/, Foo.queries
712
- assert_equal nil, Foo.db_fields[:number].limit
713
728
  assert_equal 4, Foo.schema_fields[:number].limit
729
+ assert_equal nil, Foo.db_fields[:number].limit
714
730
  end
715
731
 
716
732
  # Change limit to string
@@ -719,7 +735,7 @@ describe MiniRecord do
719
735
  refute_empty Foo.queries
720
736
  assert_equal 255, Foo.db_fields[:string].limit
721
737
  end
722
-
738
+
723
739
  it 'should not change #limit if destructive = false' do
724
740
  MiniRecord.configuration.destructive = false
725
741
  class Foo < ActiveRecord::Base
@@ -739,12 +755,12 @@ describe MiniRecord do
739
755
  case conn.adapter_name
740
756
  when /sqlite/i
741
757
  # In sqlite there is a difference between limit: 4 and limit: 11
742
- assert_match Foo.queries, ""
758
+ assert_match Foo.queries, ""
743
759
  assert_equal nil, Foo.db_fields[:number].limit
744
760
  assert_equal 4, Foo.schema_fields[:number].limit
745
761
  when /mysql/i
746
762
  # In mysql according to this: http://goo.gl/bjZE7 limit: 4 is same of limit:11
747
- assert_empty Foo.queries
763
+ refute_match /schema\s+change/, Foo.queries
748
764
  assert_equal nil, Foo.db_fields[:number].limit
749
765
  assert_equal 4, Foo.schema_fields[:number].limit
750
766
  when /postgres/i
@@ -757,10 +773,42 @@ describe MiniRecord do
757
773
  # Change limit to string
758
774
  Foo.key :string, :limit => 255
759
775
  Foo.auto_upgrade!
760
- assert_empty Foo.queries
776
+ refute_match /schema\s+change/, Foo.queries
761
777
  assert_equal 100, Foo.db_fields[:string].limit
762
778
  end
763
779
 
780
+ it 'should handle integer defaults correctly' do
781
+ class Foo < ActiveRecord::Base
782
+ field :some_int, type: :integer, default: 33
783
+ field :some_bool, type: :boolean, default: false
784
+ field :some_bool2, type: :boolean, default: false
785
+ field :some_bool3, type: :boolean, default: true
786
+ auto_upgrade!
787
+ end
788
+
789
+ # Reopen class
790
+ class Foo < ActiveRecord::Base
791
+ field :some_int, type: :integer, default: 66
792
+ field :some_bool, type: :boolean, default: true
793
+ end
794
+
795
+ new_attr, changed = Foo.field_attr_changes(:some_int)
796
+ assert_equal 66, new_attr[:default]
797
+ assert_equal true, changed
798
+
799
+ new_attr, changed = Foo.field_attr_changes(:some_bool)
800
+ assert_equal true, changed
801
+ assert_equal true, new_attr[:default]
802
+
803
+ new_attr, changed = Foo.field_attr_changes(:some_bool2)
804
+ assert_equal false, changed
805
+ assert_empty new_attr
806
+
807
+ new_attr, changed = Foo.field_attr_changes(:some_bool3)
808
+ assert_equal false, changed
809
+ assert_empty new_attr
810
+ end
811
+
764
812
  it 'should change #null' do
765
813
  class Foo < ActiveRecord::Base
766
814
  key :string
@@ -781,10 +829,10 @@ describe MiniRecord do
781
829
 
782
830
  Foo.key :string, :null => false
783
831
  Foo.auto_upgrade!
784
- assert_match /alter/i, Foo.queries
832
+ assert_match /foos.string#null/i, Foo.queries
785
833
  refute Foo.db_fields[:string].null
786
834
  end
787
-
835
+
788
836
  it 'should not change #null if destructive = false' do
789
837
  MiniRecord.configuration.destructive = false
790
838
  class Foo < ActiveRecord::Base
@@ -817,19 +865,18 @@ describe MiniRecord do
817
865
  Foo.auto_upgrade!
818
866
  assert_equal 8, Foo.db_fields[:currency].precision
819
867
  assert_equal 2, Foo.db_fields[:currency].scale
820
- assert_equal 8, Foo.db_fields[:currency].limit
821
868
 
822
869
  Foo.auto_upgrade!
823
- refute_match /alter/i, Foo.queries
870
+ new_attr, changed = Foo.field_attr_changes(:currency)
871
+ assert_equal false, changed
824
872
 
825
873
  Foo.field :currency, :as => :decimal, :precision => 4, :scale => 2, :limit => 5
826
874
  Foo.auto_upgrade!
827
- assert_match /alter/i, Foo.queries
875
+ assert_match /foos.currency#limit/i, Foo.queries
828
876
  assert_equal 4, Foo.db_fields[:currency].precision
829
877
  assert_equal 2, Foo.db_fields[:currency].scale
830
- assert_equal 4, Foo.db_fields[:currency].limit
831
878
  end
832
-
879
+
833
880
  it 'should not change #scale #precision if destructive = false' do
834
881
  MiniRecord.configuration.destructive = false
835
882
  class Foo < ActiveRecord::Base
@@ -838,7 +885,6 @@ describe MiniRecord do
838
885
  Foo.auto_upgrade!
839
886
  assert_equal 8, Foo.db_fields[:currency].precision
840
887
  assert_equal 2, Foo.db_fields[:currency].scale
841
- assert_equal 8, Foo.db_fields[:currency].limit
842
888
 
843
889
  Foo.auto_upgrade!
844
890
  refute_match /alter/i, Foo.queries
@@ -848,7 +894,6 @@ describe MiniRecord do
848
894
  assert_match "", Foo.queries
849
895
  assert_equal 8, Foo.db_fields[:currency].precision
850
896
  assert_equal 2, Foo.db_fields[:currency].scale
851
- assert_equal 8, Foo.db_fields[:currency].limit
852
897
  end
853
898
 
854
899
  it 'should ignore abstract classes' do
@@ -897,7 +942,7 @@ describe MiniRecord do
897
942
  end
898
943
  Foo.auto_upgrade!
899
944
  assert_match /CREATE TABLE/, Foo.queries
900
-
945
+
901
946
  Foo.create :currency => 'USD'
902
947
 
903
948
  Foo.rename_field :currency, :new_name => :currency_iso
@@ -905,14 +950,7 @@ describe MiniRecord do
905
950
 
906
951
  Foo.auto_upgrade!
907
952
 
908
- case conn.adapter_name
909
- when /sqlite/i
910
- assert_match /CREATE TEMPORARY TABLE "altered_foos"/i, Foo.queries
911
- when /mysql/i
912
- assert_match /ALTER TABLE `foos` CHANGE `currency` `currency_iso`/i, Foo.queries
913
- when /postgres/i
914
- assert_match /ALTER TABLE "foos" RENAME COLUMN "currency" TO "currency_iso"/, Foo.queries
915
- end
953
+ assert_match /foos.currency to currency_iso/i, Foo.queries
916
954
 
917
955
  foo = Foo.first
918
956
  assert_equal 'USD', foo.currency_iso
@@ -921,7 +959,7 @@ describe MiniRecord do
921
959
  assert_match '', Foo.queries
922
960
 
923
961
  end
924
-
962
+
925
963
  it 'should note rename a column specified by rename_field if destructive = false' do
926
964
  MiniRecord.configuration.destructive = false
927
965
  class Foo < ActiveRecord::Base
@@ -929,7 +967,7 @@ describe MiniRecord do
929
967
  end
930
968
  Foo.auto_upgrade!
931
969
  assert_match /CREATE TABLE/, Foo.queries
932
-
970
+
933
971
  Foo.create :currency => 'USD'
934
972
 
935
973
  Foo.rename_field :currency, :new_name => :currency_iso
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davide D'Agostino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -74,3 +74,4 @@ test_files:
74
74
  - test/helper.rb
75
75
  - test/models.rb
76
76
  - test/test_mini_record.rb
77
+ has_rdoc: