ibm_db 0.9.4 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,8 +22,10 @@ if ActiveRecord::Base.connection.supports_migrations?
22
22
  end
23
23
  end
24
24
 
25
- class MigrationTest < Test::Unit::TestCase
25
+ class IBMDBMigrationTest < Test::Unit::TestCase
26
26
  self.use_transactional_fixtures = false
27
+
28
+ fixtures :people
27
29
 
28
30
  def setup
29
31
  ActiveRecord::Migration.verbose = true
@@ -40,7 +42,7 @@ if ActiveRecord::Base.connection.supports_migrations?
40
42
  Reminder.reset_column_information
41
43
 
42
44
  %w(last_name key bio age height wealth birthday favorite_day
43
- male administrator).each do |column|
45
+ moment_of_truth male administrator funny).each do |column|
44
46
  Person.connection.remove_column('people', column) rescue nil
45
47
  end
46
48
  Person.connection.remove_column("people", "first_name") rescue nil
@@ -59,7 +61,8 @@ if ActiveRecord::Base.connection.supports_migrations?
59
61
  assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
60
62
 
61
63
  # Orcl nds shrt indx nms. Sybs 2.
62
- unless current_adapter?(:OracleAdapter, :SybaseAdapter, :IBM_DBAdapter) # incompatible index size
64
+ # OpenBase does not have named indexes. You must specify a single column name
65
+ unless current_adapter?(:OracleAdapter, :SybaseAdapter, :OpenBaseAdapter)
63
66
  assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
64
67
  assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
65
68
  assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
@@ -72,11 +75,15 @@ if ActiveRecord::Base.connection.supports_migrations?
72
75
 
73
76
  # quoting
74
77
  # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
75
- assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) }
76
- assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) }
77
-
78
+ # OpenBase does not have named indexes. You must specify a single column name
79
+ unless current_adapter?(:OpenBaseAdapter)
80
+ assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) }
81
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) }
82
+ end
83
+
78
84
  # Sybase adapter does not support indexes on :boolean columns
79
- unless current_adapter?(:SybaseAdapter)
85
+ # OpenBase does not have named indexes. You must specify a single column
86
+ unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter)
80
87
  assert_nothing_raised { Person.connection.add_index("people", %w(last_name first_name administrator), :name => "named_admin") }
81
88
  assert_nothing_raised { Person.connection.remove_index("people", :name => "named_admin") }
82
89
  end
@@ -108,11 +115,17 @@ if ActiveRecord::Base.connection.supports_migrations?
108
115
  end
109
116
 
110
117
  def test_create_table_with_defaults
118
+ # MySQL doesn't allow defaults on TEXT or BLOB columns.
119
+ mysql = current_adapter?(:MysqlAdapter)
120
+ ibm_ids_zOS = ActiveRecord::Base.connection.servertype.class.name.include?('::IBM_IDS')||
121
+ ActiveRecord::Base.connection.servertype.class.name.include?('::IBM_DB2_ZOS')
122
+
111
123
  Person.connection.create_table :testings do |t|
112
124
  t.column :one, :string, :default => "hello"
113
125
  t.column :two, :boolean, :default => true
114
126
  t.column :three, :boolean, :default => false
115
127
  t.column :four, :integer, :default => 1
128
+ t.column :five, :text, :default => "hello" unless mysql || ibm_ids_zOS
116
129
  end
117
130
 
118
131
  columns = Person.connection.columns(:testings)
@@ -120,11 +133,13 @@ if ActiveRecord::Base.connection.supports_migrations?
120
133
  two = columns.detect { |c| c.name == "two" }
121
134
  three = columns.detect { |c| c.name == "three" }
122
135
  four = columns.detect { |c| c.name == "four" }
136
+ five = columns.detect { |c| c.name == "five" } unless mysql || ibm_ids_zOS
123
137
 
124
138
  assert_equal "hello", one.default
125
139
  assert_equal true, two.default
126
140
  assert_equal false, three.default
127
141
  assert_equal 1, four.default
142
+ assert_equal "hello", five.default unless mysql || ibm_ids_zOS
128
143
 
129
144
  ensure
130
145
  Person.connection.drop_table :testings rescue nil
@@ -153,21 +168,20 @@ if ActiveRecord::Base.connection.supports_migrations?
153
168
  clasz.connection.drop_table(clasz.table_name) rescue nil
154
169
  end
155
170
 
171
+ #Sexy migration test for column of type xml
156
172
  def test_new_xml_migrations
157
173
  clasz = Class.new(ActiveRecord::Base)
158
174
  clasz.table_name = 'test_new_xml_migrations'
159
175
  assert_nothing_raised do
160
176
  clasz.connection.create_table clasz.table_name do |t|
161
- t.xml :xml_col,:xml_col1,:xml_col2
162
- t.string :varchar_col
163
- t.integer :int_col
177
+ t.xml :xml_col
164
178
  end
165
179
  end
166
180
  ensure
167
181
  clasz.connection.drop_table(clasz.table_name) rescue nil
168
182
  end
169
183
  end
170
-
184
+
171
185
  def test_create_table_with_limits
172
186
  assert_nothing_raised do
173
187
  Person.connection.create_table :testings do |t|
@@ -205,10 +219,9 @@ if ActiveRecord::Base.connection.supports_migrations?
205
219
  Person.connection.drop_table :testings rescue nil
206
220
  end
207
221
 
208
- # SQL Server and Sybase will not allow you to add a NOT NULL column
209
- # to a table without specifying a default value, so the
210
- # following test must be skipped
211
- unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :IBM_DBAdapter)
222
+ # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL
223
+ # column to a table without a default value.
224
+ unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :SQLiteAdapter, :IBM_DBAdapter)
212
225
  def test_add_column_not_null_without_default
213
226
  Person.connection.create_table :testings do |t|
214
227
  t.column :foo, :string
@@ -235,7 +248,12 @@ if ActiveRecord::Base.connection.supports_migrations?
235
248
  assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
236
249
 
237
250
  assert_raises(ActiveRecord::StatementInvalid) do
238
- Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
251
+ unless current_adapter?(:OpenBaseAdapter)
252
+ Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
253
+ else
254
+ Person.connection.insert("INSERT INTO testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) VALUES (2, 'hello', NULL)",
255
+ "Testing Insert","id",2)
256
+ end
239
257
  end
240
258
  ensure
241
259
  Person.connection.drop_table :testings rescue nil
@@ -245,9 +263,7 @@ if ActiveRecord::Base.connection.supports_migrations?
245
263
  # functionality. This allows us to more easily catch INSERT being broken,
246
264
  # but SELECT actually working fine.
247
265
  def test_native_decimal_insert_manual_vs_automatic
248
- # SQLite3 always uses float in violation of SQL
249
- # 16 decimal places
250
- correct_value = (current_adapter?(:SQLiteAdapter) ? '0.123456789012346E20' : '0012345678901234567890.0123456789').to_d
266
+ correct_value = '0012345678901234567890.0123456789'.to_d
251
267
 
252
268
  Person.delete_all
253
269
  Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
@@ -256,6 +272,8 @@ if ActiveRecord::Base.connection.supports_migrations?
256
272
  # Do a manual insertion
257
273
  if current_adapter?(:OracleAdapter)
258
274
  Person.connection.execute "insert into people (id, wealth) values (people_seq.nextval, 12345678901234567890.0123456789)"
275
+ elsif current_adapter?(:OpenBaseAdapter)
276
+ Person.connection.execute "insert into people (wealth) values ('12345678901234567890.0123456789')"
259
277
  else
260
278
  Person.connection.execute "insert into people (wealth) values (12345678901234567890.0123456789)"
261
279
  end
@@ -265,7 +283,9 @@ if ActiveRecord::Base.connection.supports_migrations?
265
283
  assert_kind_of BigDecimal, row.wealth
266
284
 
267
285
  # If this assert fails, that means the SELECT is broken!
268
- assert_equal correct_value, row.wealth
286
+ unless current_adapter?(:SQLite3Adapter)
287
+ assert_equal correct_value, row.wealth
288
+ end
269
289
 
270
290
  # Reset to old state
271
291
  Person.delete_all
@@ -278,7 +298,9 @@ if ActiveRecord::Base.connection.supports_migrations?
278
298
  assert_kind_of BigDecimal, row.wealth
279
299
 
280
300
  # If these asserts fail, that means the INSERT (create function, or cast to SQL) is broken!
281
- assert_equal correct_value, row.wealth
301
+ unless current_adapter?(:SQLite3Adapter)
302
+ assert_equal correct_value, row.wealth
303
+ end
282
304
 
283
305
  # Reset to old state
284
306
  Person.connection.del_column "people", "wealth" rescue nil
@@ -294,10 +316,19 @@ if ActiveRecord::Base.connection.supports_migrations?
294
316
  Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
295
317
  Person.connection.add_column "people", "birthday", :datetime
296
318
  Person.connection.add_column "people", "favorite_day", :date
319
+ Person.connection.add_column "people", "moment_of_truth", :datetime
297
320
  Person.connection.add_column "people", "male", :boolean
298
- assert_nothing_raised { Person.create :first_name => 'bob', :last_name => 'bobsen', :bio => "I was born ....", :age => 18, :height => 1.78, :wealth => BigDecimal.new("12345678901234567890.0123456789"), :birthday => 18.years.ago, :favorite_day => 10.days.ago, :male => true }
299
- bob = Person.find(:first)
321
+ Person.reset_column_information
300
322
 
323
+ assert_nothing_raised do
324
+ Person.create :first_name => 'bob', :last_name => 'bobsen',
325
+ :bio => "I was born ....", :age => 18, :height => 1.78,
326
+ :wealth => BigDecimal.new("12345678901234567890.0123456789"),
327
+ :birthday => 18.years.ago, :favorite_day => 10.days.ago,
328
+ :moment_of_truth => "1782-10-10 21:40:18", :male => true
329
+ end
330
+
331
+ bob = Person.find(:first)
301
332
  assert_equal 'bob', bob.first_name
302
333
  assert_equal 'bobsen', bob.last_name
303
334
  assert_equal "I was born ....", bob.bio
@@ -305,13 +336,11 @@ if ActiveRecord::Base.connection.supports_migrations?
305
336
 
306
337
  # Test for 30 significent digits (beyond the 16 of float), 10 of them
307
338
  # after the decimal place.
308
- if current_adapter?(:SQLiteAdapter)
309
- # SQLite3 uses float in violation of SQL. Test for 16 decimal places.
310
- assert_equal BigDecimal.new('0.123456789012346E20'), bob.wealth
311
- else
339
+
340
+ unless current_adapter?(:SQLite3Adapter)
312
341
  assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth
313
342
  end
314
-
343
+
315
344
  assert_equal true, bob.male?
316
345
 
317
346
  assert_equal String, bob.first_name.class
@@ -327,10 +356,34 @@ if ActiveRecord::Base.connection.supports_migrations?
327
356
  assert_equal Date, bob.favorite_day.class
328
357
  end
329
358
 
359
+ # Test DateTime column and defaults, including timezone.
360
+ # FIXME: moment of truth may be Time on 64-bit platforms.
361
+ if bob.moment_of_truth.is_a?(DateTime)
362
+ assert_equal DateTime.now.offset, bob.moment_of_truth.offset
363
+ assert_not_equal 0, bob.moment_of_truth.offset
364
+ assert_not_equal "Z", bob.moment_of_truth.zone
365
+ assert_equal DateTime::ITALY, bob.moment_of_truth.start
366
+ end
367
+
330
368
  assert_equal TrueClass, bob.male?.class
331
369
  assert_kind_of BigDecimal, bob.wealth
332
370
  end
333
371
 
372
+ if current_adapter?(:MysqlAdapter)
373
+ def test_unabstracted_database_dependent_types
374
+ Person.delete_all
375
+
376
+ ActiveRecord::Migration.add_column :people, :intelligence_quotient, :tinyint
377
+ Person.reset_column_information
378
+ Person.create :intelligence_quotient => 300
379
+ jonnyg = Person.find(:first)
380
+ assert_equal 127, jonnyg.intelligence_quotient
381
+ jonnyg.destroy
382
+ ensure
383
+ ActiveRecord::Migration.remove_column :people, :intelligence_quotient rescue nil
384
+ end
385
+ end
386
+
334
387
  def test_add_remove_single_field_using_string_arguments
335
388
  assert !Person.column_methods_hash.include?(:last_name)
336
389
 
@@ -359,52 +412,81 @@ if ActiveRecord::Base.connection.supports_migrations?
359
412
  assert !Person.column_methods_hash.include?(:last_name)
360
413
  end
361
414
 
362
- unless current_adapter?(:IBM_DBAdapter) # rename not supported
363
- def test_add_rename
364
- Person.delete_all
365
-
366
- begin
367
- Person.connection.add_column "people", "girlfriend", :string
368
- Person.create :girlfriend => 'bobette'
369
-
370
- Person.connection.rename_column "people", "girlfriend", "exgirlfriend"
415
+ unless current_adapter?(:IBM_DBAdapter) # rename not supported
416
+ def test_add_rename
417
+ Person.delete_all
418
+ begin
419
+ Person.connection.add_column "people", "girlfriend", :string
420
+ Person.reset_column_information
421
+ Person.create :girlfriend => 'bobette'
422
+
423
+ Person.connection.rename_column "people", "girlfriend", "exgirlfriend"
371
424
 
372
- Person.reset_column_information
373
- bob = Person.find(:first)
425
+ Person.reset_column_information
426
+ bob = Person.find(:first)
427
+
428
+ assert_equal "bobette", bob.exgirlfriend
429
+ ensure
430
+ Person.connection.remove_column("people", "girlfriend") rescue nil
431
+ Person.connection.remove_column("people", "exgirlfriend") rescue nil
432
+ end
433
+ end
434
+ end
374
435
 
375
- assert_equal "bobette", bob.exgirlfriend
376
- ensure
377
- Person.connection.remove_column("people", "girlfriend") rescue nil
378
- Person.connection.remove_column("people", "exgirlfriend") rescue nil
379
- end
436
+ unless current_adapter?(:IBM_DBAdapter) # rename not supported
437
+ def test_rename_column_using_symbol_arguments
438
+ begin
439
+ names_before = Person.find(:all).map(&:first_name)
440
+ Person.connection.rename_column :people, :first_name, :nick_name
441
+ Person.reset_column_information
442
+ assert Person.column_names.include?("nick_name")
443
+ assert_equal names_before, Person.find(:all).map(&:nick_name)
444
+ ensure
445
+ Person.connection.remove_column("people","nick_name")
446
+ Person.connection.add_column("people","first_name", :string)
380
447
  end
381
448
  end
449
+ end
382
450
 
383
- unless current_adapter?(:IBM_DBAdapter) # rename not supported
384
- def test_rename_column_using_symbol_arguments
385
- begin
386
- Person.connection.rename_column :people, :first_name, :nick_name
387
- Person.reset_column_information
388
- assert Person.column_names.include?("nick_name")
389
- ensure
390
- Person.connection.remove_column("people","nick_name")
391
- Person.connection.add_column("people","first_name", :string)
392
- end
393
- end
451
+ unless current_adapter?(:IBM_DBAdapter) # rename not supported
452
+ def test_rename_column
453
+ begin
454
+ names_before = Person.find(:all).map(&:first_name)
455
+ Person.connection.rename_column "people", "first_name", "nick_name"
456
+ Person.reset_column_information
457
+ assert Person.column_names.include?("nick_name")
458
+ assert_equal names_before, Person.find(:all).map(&:nick_name)
459
+ ensure
460
+ Person.connection.remove_column("people","nick_name")
461
+ Person.connection.add_column("people","first_name", :string)
462
+ end
394
463
  end
464
+ end
395
465
 
396
- unless current_adapter?(:IBM_DBAdapter) # rename not supported
397
- def test_rename_column
398
- begin
399
- Person.connection.rename_column "people", "first_name", "nick_name"
400
- Person.reset_column_information
401
- assert Person.column_names.include?("nick_name")
402
- ensure
403
- Person.connection.remove_column("people","nick_name")
404
- Person.connection.add_column("people","first_name", :string)
405
- end
466
+ unless current_adapter?(:IBM_DBAdapter) # rename not supported
467
+ def test_rename_column_with_sql_reserved_word
468
+ begin
469
+ assert_nothing_raised { Person.connection.rename_column "people", "first_name", "group" }
470
+ Person.reset_column_information
471
+ assert Person.column_names.include?("group")
472
+ ensure
473
+ Person.connection.remove_column("people", "group") rescue nil
474
+ Person.connection.add_column("people", "first_name", :string) rescue nil
475
+ end
476
+ end
477
+ end
478
+
479
+ unless current_adapter?(:IBM_DBAdapter) #incompatible types changes
480
+ def test_change_type_of_not_null_column
481
+ assert_nothing_raised do
482
+ Topic.connection.change_column "topics", "written_on", :datetime, :null => false
483
+ Topic.reset_column_information
484
+
485
+ Topic.connection.change_column "topics", "written_on", :datetime, :null => false
486
+ Topic.reset_column_information
406
487
  end
407
488
  end
489
+ end
408
490
 
409
491
  def test_rename_table
410
492
  begin
@@ -426,6 +508,21 @@ if ActiveRecord::Base.connection.supports_migrations?
426
508
  ActiveRecord::Base.connection.drop_table :octopi rescue nil
427
509
  end
428
510
  end
511
+
512
+ def test_change_column_nullability
513
+ Person.delete_all
514
+ Person.connection.add_column "people", "funny", :boolean
515
+ Person.reset_column_information
516
+ assert Person.columns_hash["funny"].null, "Column 'funny' must initially allow nulls"
517
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
518
+ Person.connection.change_column "people", "funny", :boolean, :null => false, :default => true
519
+ Person.reset_column_information
520
+ assert !Person.columns_hash["funny"].null, "Column 'funny' must *not* allow nulls at this point"
521
+ Person.connection.change_column "people", "funny", :boolean, :null => true
522
+ Person.reset_column_information
523
+ assert Person.columns_hash["funny"].null, "Column 'funny' must allow nulls again at this point"
524
+ end
525
+ end
429
526
 
430
527
  def test_rename_table_with_an_index
431
528
  begin
@@ -450,49 +547,55 @@ if ActiveRecord::Base.connection.supports_migrations?
450
547
  end
451
548
  end
452
549
 
453
- unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
454
- def test_change_column
455
- Person.connection.add_column 'people', 'age', :integer
456
- old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
457
- assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
458
- assert_nothing_raised { Person.connection.change_column "people", "age", :string }
459
- new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
460
- assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
461
- assert new_columns.find { |c| c.name == 'age' and c.type == :string }
462
-
463
- old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
464
- assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
465
- assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
466
- new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
467
- assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
468
- assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
469
- assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
470
- end
550
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
551
+ def test_change_column
552
+ Person.connection.add_column 'people', 'age', :integer
553
+ old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
554
+ assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
555
+
556
+ assert_nothing_raised { Person.connection.change_column "people", "age", :string }
557
+
558
+ new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
559
+ assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
560
+ assert new_columns.find { |c| c.name == 'age' and c.type == :string }
561
+
562
+ old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
563
+ assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
564
+ assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
565
+ new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
566
+ assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
567
+ assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
568
+ assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
471
569
  end
570
+ end
472
571
 
473
572
  def test_change_column_with_nil_default
474
573
  Person.connection.add_column "people", "contributor", :boolean, :default => true
475
574
  Person.reset_column_information
476
575
  assert Person.new.contributor?
477
-
576
+
478
577
  unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
479
578
  assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil }
480
579
  Person.reset_column_information
481
580
  assert !Person.new.contributor?
482
581
  assert_nil Person.new.contributor
483
582
  end
583
+ ensure
584
+ Person.connection.remove_column("people", "contributor") rescue nil
484
585
  end
485
586
 
486
587
  def test_change_column_with_new_default
487
588
  Person.connection.add_column "people", "administrator", :boolean, :default => true
488
589
  Person.reset_column_information
489
590
  assert Person.new.administrator?
490
-
491
- unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
492
- assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
493
- Person.reset_column_information
494
- assert !Person.new.administrator?
495
- end
591
+
592
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
593
+ assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
594
+ Person.reset_column_information
595
+ assert !Person.new.administrator?
596
+ end
597
+ ensure
598
+ Person.connection.remove_column("people", "administrator") rescue nil
496
599
  end
497
600
 
498
601
  def test_change_column_default
@@ -500,7 +603,19 @@ if ActiveRecord::Base.connection.supports_migrations?
500
603
  Person.reset_column_information
501
604
  assert_equal "Tester", Person.new.first_name
502
605
  end
503
-
606
+
607
+ def test_change_column_quotes_column_names
608
+ Person.connection.create_table :testings do |t|
609
+ t.column :select, :string
610
+ end
611
+ unless current_adapter?(:IBM_DBAdapter) # altering a string column to size lesser than the previous not allowed
612
+ assert_nothing_raised { Person.connection.change_column :testings, :select, :string, :limit => 10 }
613
+ end
614
+ assert_nothing_raised { Person.connection.execute "insert into testings (#{Person.connection.quote_column_name('select')}) values ('7 chars')" }
615
+ ensure
616
+ Person.connection.drop_table :testings rescue nil
617
+ end
618
+
504
619
  def test_change_column_default_to_null
505
620
  Person.connection.change_column_default "people", "first_name", nil
506
621
  Person.reset_column_information
@@ -511,8 +626,8 @@ if ActiveRecord::Base.connection.supports_migrations?
511
626
  assert !Reminder.table_exists?
512
627
 
513
628
  WeNeedReminders.up
514
-
515
- assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
629
+
630
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
516
631
  assert_equal "hello world", Reminder.find(:first).content
517
632
 
518
633
  WeNeedReminders.down
@@ -544,17 +659,17 @@ if ActiveRecord::Base.connection.supports_migrations?
544
659
 
545
660
  # TODO: set world_population >= 2**62 to cover 64-bit platforms and test
546
661
  # is_a?(Bignum)
547
- unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
548
- assert_kind_of Integer, b.world_population
549
- else
550
- assert_kind_of BigDecimal, b.world_population
551
- end
662
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
663
+ assert_kind_of Integer, b.world_population
664
+ else
665
+ assert_kind_of BigDecimal, b.world_population
666
+ end
552
667
  assert_equal 6000000000, b.world_population
553
- unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
554
- assert_kind_of Fixnum, b.my_house_population
555
- else
556
- assert_kind_of BigDecimal, b.my_house_population
557
- end
668
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
669
+ assert_kind_of Fixnum, b.my_house_population
670
+ else
671
+ assert_kind_of BigDecimal, b.my_house_population
672
+ end
558
673
  assert_equal 3, b.my_house_population
559
674
  assert_kind_of BigDecimal, b.bank_balance
560
675
  assert_equal BigDecimal("1586.43"), b.bank_balance
@@ -562,7 +677,7 @@ if ActiveRecord::Base.connection.supports_migrations?
562
677
  assert_equal BigDecimal("1000234000567.95"), b.big_bank_balance
563
678
 
564
679
  # This one is fun. The 'value_of_e' field is defined as 'DECIMAL' with
565
- # precision/scale explictly left out. By the SQL standard, numbers
680
+ # precision/scale explicitly left out. By the SQL standard, numbers
566
681
  # assigned to this field should be truncated but that's seldom respected.
567
682
  if current_adapter?(:PostgreSQLAdapter, :SQLite2Adapter)
568
683
  # - PostgreSQL changes the SQL spec on columns declared simply as
@@ -584,11 +699,11 @@ if ActiveRecord::Base.connection.supports_migrations?
584
699
  assert_equal 3, b.value_of_e
585
700
  else
586
701
  # - SQL standard is an integer
587
- unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
588
- assert_kind_of Fixnum, b.value_of_e
589
- else
590
- assert_kind_of BigDecimal, b.value_of_e
591
- end
702
+ unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
703
+ assert_kind_of Fixnum, b.value_of_e
704
+ else
705
+ assert_kind_of BigDecimal, b.value_of_e
706
+ end
592
707
  assert_equal 2, b.value_of_e
593
708
  end
594
709
 
@@ -735,7 +850,6 @@ if ActiveRecord::Base.connection.supports_migrations?
735
850
  WeNeedReminders.up
736
851
  assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
737
852
  assert_equal "hello world", Reminder.find(:first).content
738
-
739
853
  WeNeedReminders.down
740
854
  assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
741
855
  ensure
@@ -745,33 +859,25 @@ if ActiveRecord::Base.connection.supports_migrations?
745
859
  Reminder.reset_sequence_name
746
860
  end
747
861
 
748
- # FrontBase does not support default values on BLOB/CLOB columns
749
- unless current_adapter?(:FrontBaseAdapter, :IBM_DBAdapter) # incompatible default value
750
- # zOS 9: [IBM][CLI Driver][DB2] SQL0574N
751
- # DEFAULT value or IDENTITY attribute value is not valid for column \"DATA\" in table \"\".
752
- # Reason code: \"\". SQLSTATE=42894 SQLCODE=-574:
753
- # CREATE TABLE binary_testings (
754
- # id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 100) PRIMARY KEY,
755
- # data blob DEFAULT BLOB('') NOT NULL) ">
756
- def test_create_table_with_binary_column
757
- Person.connection.drop_table :binary_testings rescue nil
758
-
759
- assert_nothing_raised {
760
- Person.connection.create_table :binary_testings do |t|
761
- t.column "data", :binary, :default => "", :null => false
762
- end
763
- }
862
+ def test_create_table_with_binary_column
863
+ Person.connection.drop_table :binary_testings rescue nil
764
864
 
765
- columns = Person.connection.columns(:binary_testings)
766
- data_column = columns.detect { |c| c.name == "data" }
767
-
768
- if current_adapter?(:OracleAdapter)
769
- assert_equal "empty_blob()", data_column.default
770
- else
771
- assert_equal "", data_column.default
865
+ assert_nothing_raised {
866
+ Person.connection.create_table :binary_testings do |t|
867
+ t.column "data", :binary, :null => false
772
868
  end
773
- Person.connection.drop_table :binary_testings rescue nil
869
+ }
870
+
871
+ columns = Person.connection.columns(:binary_testings)
872
+ data_column = columns.detect { |c| c.name == "data" }
873
+
874
+ if current_adapter?(:MysqlAdapter)
875
+ assert_equal '', data_column.default
876
+ else
877
+ assert_nil data_column.default
774
878
  end
879
+
880
+ Person.connection.drop_table :binary_testings rescue nil
775
881
  end
776
882
 
777
883
  def test_migrator_with_duplicates
@@ -783,14 +889,15 @@ if ActiveRecord::Base.connection.supports_migrations?
783
889
  def test_migrator_with_missing_version_numbers
784
890
  ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 500)
785
891
  assert !Person.column_methods_hash.include?(:middle_name)
786
- assert_equal 4, ActiveRecord::Migrator.current_version
787
-
788
- ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2)
789
- assert !Reminder.table_exists?
790
- assert Person.column_methods_hash.include?(:last_name)
791
- assert_equal 2, ActiveRecord::Migrator.current_version
892
+ assert_equal 4, ActiveRecord::Migrator.current_version
893
+
894
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2)
895
+ Person.reset_column_information
896
+ assert !Reminder.table_exists?
897
+ assert Person.column_methods_hash.include?(:last_name)
898
+ assert_equal 2, ActiveRecord::Migrator.current_version
792
899
  end
793
-
900
+
794
901
  def test_create_table_with_custom_sequence_name
795
902
  return unless current_adapter? :OracleAdapter
796
903
 
@@ -827,7 +934,66 @@ if ActiveRecord::Base.connection.supports_migrations?
827
934
  Person.connection.execute("select suitably_short_seq.nextval from dual")
828
935
  end
829
936
  end
830
-
831
937
  end
938
+
939
+ uses_mocha 'Sexy migration tests' do
940
+ class IBMDBSexyMigrationsTest < Test::Unit::TestCase
941
+ def test_references_column_type_adds_id
942
+ with_new_table do |t|
943
+ t.expects(:column).with('customer_id', :integer, {})
944
+ t.references :customer
945
+ end
946
+ end
947
+
948
+ def test_references_column_type_with_polymorphic_adds_type
949
+ with_new_table do |t|
950
+ t.expects(:column).with('taggable_type', :string, {})
951
+ t.expects(:column).with('taggable_id', :integer, {})
952
+ t.references :taggable, :polymorphic => true
953
+ end
954
+ end
955
+
956
+ def test_belongs_to_works_like_references
957
+ with_new_table do |t|
958
+ t.expects(:column).with('customer_id', :integer, {})
959
+ t.belongs_to :customer
960
+ end
961
+ end
962
+
963
+ def test_timestamps_creates_updated_at_and_created_at
964
+ with_new_table do |t|
965
+ t.expects(:column).with(:created_at, :datetime)
966
+ t.expects(:column).with(:updated_at, :datetime)
967
+ t.timestamps
968
+ end
969
+ end
970
+
971
+ def test_integer_creates_integer_column
972
+ with_new_table do |t|
973
+ t.expects(:column).with(:foo, 'integer', {})
974
+ t.expects(:column).with(:bar, 'integer', {})
975
+ t.integer :foo, :bar
976
+ end
977
+ end
978
+
979
+ def test_string_creates_string_column
980
+ with_new_table do |t|
981
+ t.expects(:column).with(:foo, 'string', {})
982
+ t.expects(:column).with(:bar, 'string', {})
983
+ t.string :foo, :bar
984
+ end
985
+ end
986
+
987
+ protected
988
+ def with_new_table
989
+ Person.connection.create_table :delete_me do |t|
990
+ yield t
991
+ end
992
+ ensure
993
+ Person.connection.drop_table :delete_me rescue nil
994
+ end
995
+
996
+ end # SexyMigrationsTest
997
+ end # uses_mocha
832
998
  end
833
999