ibm_db 2.5.5 → 2.5.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,6 +16,9 @@ require 'models/treasure'
16
16
  require 'models/matey'
17
17
  require 'models/ship'
18
18
  require 'models/book'
19
+ require 'models/admin'
20
+ require 'models/admin/account'
21
+ require 'models/admin/user'
19
22
 
20
23
  class FixturesTest < ActiveRecord::TestCase
21
24
  self.use_instantiated_fixtures = true
@@ -26,14 +29,14 @@ class FixturesTest < ActiveRecord::TestCase
26
29
  FIXTURES = %w( accounts binaries companies customers
27
30
  developers developers_projects entrants
28
31
  movies projects subscribers topics tasks )
29
- MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-_\w]*/
32
+ MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-\w]*/
30
33
 
31
34
  def test_clean_fixtures
32
35
  FIXTURES.each do |name|
33
36
  fixtures = nil
34
37
  assert_nothing_raised { fixtures = create_fixtures(name) }
35
38
  assert_kind_of(Fixtures, fixtures)
36
- fixtures.each { |name, fixture|
39
+ fixtures.each { |_name, fixture|
37
40
  fixture.each { |key, value|
38
41
  assert_match(MATCH_ATTRIBUTE_NAME, key)
39
42
  }
@@ -98,7 +101,7 @@ class FixturesTest < ActiveRecord::TestCase
98
101
  second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
99
102
  assert_nil(second_row["author_email_address"])
100
103
 
101
- # This checks for a caching problem which causes a bug in the fixtures
104
+ # This checks for a caching problem which causes a bug in the fixtures
102
105
  # class-level configuration helper.
103
106
  assert_not_nil topics, "Fixture data inserted, but fixture objects not returned from create"
104
107
  ensure
@@ -150,6 +153,17 @@ class FixturesTest < ActiveRecord::TestCase
150
153
  assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', FIXTURES_ROOT + "/naked/yml/companies")
151
154
  end
152
155
 
156
+ def test_nonexistent_fixture_file
157
+ nonexistent_fixture_path = FIXTURES_ROOT + "/imnothere"
158
+
159
+ #sanity check to make sure that this file never exists
160
+ assert Dir[nonexistent_fixture_path+"*"].empty?
161
+
162
+ assert_raise(FixturesFileNotFound) do
163
+ Fixtures.new( Account.connection, "companies", 'Company', nonexistent_fixture_path)
164
+ end
165
+ end
166
+
153
167
  def test_dirty_dirty_yaml_file
154
168
  assert_raise(Fixture::FormatError) do
155
169
  Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
@@ -230,9 +244,9 @@ if Account.connection.respond_to?(:reset_pk_sequence!)
230
244
 
231
245
  def test_create_fixtures_resets_sequences_when_not_cached
232
246
  @instances.each do |instance|
233
- max_id = create_fixtures(instance.class.table_name).inject(0) do |max_id, (name, fixture)|
247
+ max_id = create_fixtures(instance.class.table_name).inject(0) do |_max_id, (name, fixture)|
234
248
  fixture_id = fixture['id'].to_i
235
- fixture_id > max_id ? fixture_id : max_id
249
+ fixture_id > _max_id ? fixture_id : _max_id
236
250
  end
237
251
 
238
252
  # Clone the last fixture to check that it gets the next greatest id.
@@ -248,14 +262,19 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
248
262
  fixtures :topics, :developers, :accounts
249
263
 
250
264
  def test_without_complete_instantiation
251
- assert_nil @first
252
- assert_nil @topics
253
- assert_nil @developers
254
- assert_nil @accounts
265
+ assert !defined?(@first)
266
+ assert !defined?(@topics)
267
+ assert !defined?(@developers)
268
+ assert !defined?(@accounts)
255
269
  end
256
270
 
257
271
  def test_fixtures_from_root_yml_without_instantiation
258
- assert_nil @unknown
272
+ assert !defined?(@unknown), "@unknown is not defined"
273
+ end
274
+
275
+ def test_visibility_of_accessor_method
276
+ assert_equal false, respond_to?(:topics, false), "should be private method"
277
+ assert_equal true, respond_to?(:topics, true), "confirm to respond surely"
259
278
  end
260
279
 
261
280
  def test_accessor_methods
@@ -283,7 +302,7 @@ class FixturesWithoutInstanceInstantiationTest < ActiveRecord::TestCase
283
302
  fixtures :topics, :developers, :accounts
284
303
 
285
304
  def test_without_instance_instantiation
286
- assert_nil @first
305
+ assert !defined?(@first), "@first is not defined"
287
306
  assert_not_nil @topics
288
307
  assert_not_nil @developers
289
308
  assert_not_nil @accounts
@@ -367,7 +386,7 @@ end
367
386
  class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
368
387
  set_fixture_class :funny_jokes => 'Joke'
369
388
  fixtures :funny_jokes
370
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
389
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
371
390
  # and thus takes into account our set_fixture_class
372
391
  self.use_transactional_fixtures = false
373
392
 
@@ -379,7 +398,7 @@ end
379
398
  class FixtureNameIsNotTableNameFixturesTest < ActiveRecord::TestCase
380
399
  set_fixture_class :items => Book
381
400
  fixtures :items
382
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
401
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
383
402
  # and thus takes into account our set_fixture_class
384
403
  self.use_transactional_fixtures = false
385
404
 
@@ -391,7 +410,7 @@ end
391
410
  class FixtureNameIsNotTableNameMultipleFixturesTest < ActiveRecord::TestCase
392
411
  set_fixture_class :items => Book, :funny_jokes => Joke
393
412
  fixtures :items, :funny_jokes
394
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
413
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
395
414
  # and thus takes into account our set_fixture_class
396
415
  self.use_transactional_fixtures = false
397
416
 
@@ -407,7 +426,7 @@ end
407
426
  class CustomConnectionFixturesTest < ActiveRecord::TestCase
408
427
  set_fixture_class :courses => Course
409
428
  fixtures :courses
410
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
429
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
411
430
  # and thus takes into account our set_fixture_class
412
431
  self.use_transactional_fixtures = false
413
432
 
@@ -419,7 +438,7 @@ end
419
438
 
420
439
  class InvalidTableNameFixturesTest < ActiveRecord::TestCase
421
440
  fixtures :funny_jokes
422
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
441
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
423
442
  # and thus takes into account our lack of set_fixture_class
424
443
  self.use_transactional_fixtures = false
425
444
 
@@ -433,7 +452,7 @@ end
433
452
  class CheckEscapedYamlFixturesTest < ActiveRecord::TestCase
434
453
  set_fixture_class :funny_jokes => 'Joke'
435
454
  fixtures :funny_jokes
436
- # Set to false to blow away fixtures cache and ensure our fixtures are loaded
455
+ # Set to false to blow away fixtures cache and ensure our fixtures are loaded
437
456
  # and thus takes into account our set_fixture_class
438
457
  self.use_transactional_fixtures = false
439
458
 
@@ -511,7 +530,7 @@ class FasterFixturesTest < ActiveRecord::TestCase
511
530
  end
512
531
 
513
532
  class FoxyFixturesTest < ActiveRecord::TestCase
514
- fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers
533
+ fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers, :"admin/accounts", :"admin/users"
515
534
 
516
535
  def test_identifies_strings
517
536
  assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo"))
@@ -523,8 +542,8 @@ class FoxyFixturesTest < ActiveRecord::TestCase
523
542
  end
524
543
 
525
544
  def test_identifies_consistently
526
- assert_equal 1281023246, Fixtures.identify(:ruby)
527
- assert_equal 2140105598, Fixtures.identify(:sapphire_2)
545
+ assert_equal 207281424, Fixtures.identify(:ruby)
546
+ assert_equal 1066363776, Fixtures.identify(:sapphire_2)
528
547
  end
529
548
 
530
549
  TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
@@ -633,6 +652,11 @@ class FoxyFixturesTest < ActiveRecord::TestCase
633
652
  assert_kind_of DeadParrot, parrots(:polly)
634
653
  assert_equal pirates(:blackbeard), parrots(:polly).killer
635
654
  end
655
+
656
+ def test_namespaced_models
657
+ assert admin_accounts(:signals37).users.include?(admin_users(:david))
658
+ assert_equal 2, admin_accounts(:signals37).users.size
659
+ end
636
660
  end
637
661
 
638
662
  class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase
@@ -1,6 +1,5 @@
1
1
  require "cases/helper"
2
2
  require 'bigdecimal/util'
3
- require 'mocha'
4
3
 
5
4
  require 'models/person'
6
5
  require 'models/topic'
@@ -13,9 +12,9 @@ require MIGRATIONS_ROOT + "/interleaved/pass_3/2_i_raise_on_down"
13
12
 
14
13
  if ActiveRecord::Base.connection.supports_migrations?
15
14
  class BigNumber < ActiveRecord::Base; end
16
-
15
+
17
16
  class Reminder < ActiveRecord::Base; end
18
-
17
+
19
18
  class ActiveRecord::Migration
20
19
  class <<self
21
20
  attr_accessor :message_count
@@ -26,9 +25,27 @@ if ActiveRecord::Base.connection.supports_migrations?
26
25
  end
27
26
  end
28
27
 
28
+ class MigrationTableAndIndexTest < ActiveRecord::TestCase
29
+ def test_add_schema_info_respects_prefix_and_suffix
30
+ conn = ActiveRecord::Base.connection
31
+
32
+ conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
33
+ # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters
34
+ ActiveRecord::Base.table_name_prefix = 'p_'
35
+ ActiveRecord::Base.table_name_suffix = '_s'
36
+ conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
37
+
38
+ conn.initialize_schema_migrations_table
39
+
40
+ assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name]
41
+ ensure
42
+ ActiveRecord::Base.table_name_prefix = ""
43
+ ActiveRecord::Base.table_name_suffix = ""
44
+ end
45
+ end
46
+
29
47
  class MigrationTest < ActiveRecord::TestCase
30
48
  self.use_transactional_fixtures = false
31
-
32
49
  if (current_adapter?(:IBM_DBAdapter))
33
50
  #Rename is supported only for server zOS 9 , DB2 COBRA and Informix
34
51
  server_type = ActiveRecord::Base.connection.servertype.class.name
@@ -74,26 +91,38 @@ if ActiveRecord::Base.connection.supports_migrations?
74
91
 
75
92
  # Orcl nds shrt indx nms. Sybs 2.
76
93
  # OpenBase does not have named indexes. You must specify a single column name
77
- unless current_adapter?(:OracleAdapter, :SybaseAdapter, :OpenBaseAdapter)
94
+ unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter)
78
95
  assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
79
96
  assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
80
- assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
81
- assert_nothing_raised { Person.connection.remove_index("people", :name => "index_people_on_last_name_and_first_name") }
82
- assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
83
- assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") }
97
+ # Oracle adapter cannot have specified index name larger than 30 characters
98
+ # Oracle adapter is shortening index name when just column list is given
99
+ unless current_adapter?(:OracleAdapter)
100
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
101
+ assert_nothing_raised { Person.connection.remove_index("people", :name => :index_people_on_last_name_and_first_name) }
102
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
103
+ assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") }
104
+ end
84
105
  assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
85
106
  assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
107
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => 10) }
108
+ assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
109
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => {:last_name => 10}) }
110
+ assert_nothing_raised { Person.connection.remove_index("people", ["last_name"]) }
111
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => 10) }
112
+ assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
113
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => {:last_name => 10, :first_name => 20}) }
114
+ assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
86
115
  end
87
116
 
88
117
  # quoting
89
118
  # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
90
119
  # OpenBase does not have named indexes. You must specify a single column name
91
120
  unless current_adapter?(:OpenBaseAdapter)
92
- unless current_adapter?(:IBM_DBAdapter)
121
+ unless current_adapter?(:IBM_DBAdapter) #cannot assign a integer value to string column
93
122
  Person.update_all "#{Person.connection.quote_column_name 'key'}=#{Person.connection.quote_column_name 'id'}" #some databases (including sqlite2 won't add a unique index if existing data non unique)
94
123
  else
95
- Person.update_all "#{Person.connection.quote_column_name 'key'}=#{Person.connection.quote_column_name 'first_name'}" #some databases (including sqlite2 won't add a unique index if existing data non unique)
96
- end
124
+ Person.update_all "#{Person.connection.quote_column_name 'key'}=#{Person.connection.quote_column_name 'first_name'}"
125
+ end
97
126
  assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) }
98
127
  assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) }
99
128
  end
@@ -106,6 +135,95 @@ if ActiveRecord::Base.connection.supports_migrations?
106
135
  end
107
136
  end
108
137
 
138
+ def test_index_symbol_names
139
+ assert_nothing_raised { Person.connection.add_index :people, :primary_contact_id, :name => :symbol_index_name }
140
+ assert Person.connection.index_exists?(:people, :primary_contact_id, :name => :symbol_index_name)
141
+ assert_nothing_raised { Person.connection.remove_index :people, :name => :symbol_index_name }
142
+ assert !Person.connection.index_exists?(:people, :primary_contact_id, :name => :symbol_index_name)
143
+ end
144
+
145
+ def test_add_index_length_limit
146
+ good_index_name = 'x' * Person.connection.index_name_length
147
+ too_long_index_name = good_index_name + 'x'
148
+ assert_raise(ArgumentError) { Person.connection.add_index("people", "first_name", :name => too_long_index_name) }
149
+ assert !Person.connection.index_name_exists?("people", too_long_index_name, false)
150
+ assert_nothing_raised { Person.connection.add_index("people", "first_name", :name => good_index_name) }
151
+ assert Person.connection.index_name_exists?("people", good_index_name, false)
152
+ Person.connection.remove_index("people", :name => good_index_name)
153
+ end
154
+
155
+ def test_remove_nonexistent_index
156
+ # we do this by name, so OpenBase is a wash as noted above
157
+ unless current_adapter?(:OpenBaseAdapter)
158
+ assert_raise(ArgumentError) { Person.connection.remove_index("people", "no_such_index") }
159
+ end
160
+ end
161
+
162
+ def test_rename_index
163
+ unless current_adapter?(:OpenBaseAdapter)
164
+ # keep the names short to make Oracle and similar behave
165
+ Person.connection.add_index('people', [:first_name], :name => 'old_idx')
166
+ assert_nothing_raised { Person.connection.rename_index('people', 'old_idx', 'new_idx') }
167
+ # if the adapter doesn't support the indexes call, pick defaults that let the test pass
168
+ assert !Person.connection.index_name_exists?('people', 'old_idx', false)
169
+ assert Person.connection.index_name_exists?('people', 'new_idx', true)
170
+ end
171
+ end
172
+
173
+ def test_double_add_index
174
+ unless current_adapter?(:OpenBaseAdapter)
175
+ Person.connection.add_index('people', [:first_name], :name => 'some_idx')
176
+ assert_raise(ArgumentError) { Person.connection.add_index('people', [:first_name], :name => 'some_idx') }
177
+ end
178
+ end
179
+
180
+ def test_index_exists
181
+ Person.connection.create_table :testings do |t|
182
+ t.column :foo, :string, :limit => 100
183
+ t.column :bar, :string, :limit => 100
184
+ end
185
+ Person.connection.add_index :testings, :foo
186
+
187
+ assert Person.connection.index_exists?(:testings, :foo)
188
+ assert !Person.connection.index_exists?(:testings, :bar)
189
+ ensure
190
+ Person.connection.drop_table :testings rescue nil
191
+ end
192
+
193
+ def test_index_exists_on_multiple_columns
194
+ Person.connection.create_table :testings do |t|
195
+ t.column :foo, :string, :limit => 100
196
+ t.column :bar, :string, :limit => 100
197
+ end
198
+ Person.connection.add_index :testings, [:foo, :bar]
199
+
200
+ assert Person.connection.index_exists?(:testings, [:foo, :bar])
201
+ ensure
202
+ Person.connection.drop_table :testings rescue nil
203
+ end
204
+
205
+ def test_unique_index_exists
206
+ Person.connection.create_table :testings do |t|
207
+ t.column :foo, :string, :limit => 100
208
+ end
209
+ Person.connection.add_index :testings, :foo, :unique => true
210
+
211
+ assert Person.connection.index_exists?(:testings, :foo, :unique => true)
212
+ ensure
213
+ Person.connection.drop_table :testings rescue nil
214
+ end
215
+
216
+ def test_named_index_exists
217
+ Person.connection.create_table :testings do |t|
218
+ t.column :foo, :string, :limit => 100
219
+ end
220
+ Person.connection.add_index :testings, :foo, :name => "custom_index_name"
221
+
222
+ assert Person.connection.index_exists?(:testings, :foo, :name => "custom_index_name")
223
+ ensure
224
+ Person.connection.drop_table :testings rescue nil
225
+ end
226
+
109
227
  def testing_table_with_only_foo_attribute
110
228
  Person.connection.create_table :testings, :id => false do |t|
111
229
  t.column :foo, :string
@@ -268,7 +386,7 @@ if ActiveRecord::Base.connection.supports_migrations?
268
386
  assert_equal 'integer', four.sql_type
269
387
  assert_equal 'bigint', eight.sql_type
270
388
  assert_equal 'integer', eleven.sql_type
271
- elsif current_adapter?(:MysqlAdapter)
389
+ elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
272
390
  assert_match 'int(11)', default.sql_type
273
391
  assert_match 'tinyint', one.sql_type
274
392
  assert_match 'int', four.sql_type
@@ -291,7 +409,7 @@ if ActiveRecord::Base.connection.supports_migrations?
291
409
  t.column :foo, :string
292
410
  end
293
411
 
294
- assert_equal %w(foo testings_id), Person.connection.columns(:testings).map { |c| c.name }.sort
412
+ assert_equal %w(foo testing_id), Person.connection.columns(:testings).map { |c| c.name }.sort
295
413
  ensure
296
414
  Person.connection.drop_table :testings rescue nil
297
415
  ActiveRecord::Base.primary_key_prefix_type = nil
@@ -304,7 +422,7 @@ if ActiveRecord::Base.connection.supports_migrations?
304
422
  t.column :foo, :string
305
423
  end
306
424
 
307
- assert_equal %w(foo testingsid), Person.connection.columns(:testings).map { |c| c.name }.sort
425
+ assert_equal %w(foo testingid), Person.connection.columns(:testings).map { |c| c.name }.sort
308
426
  ensure
309
427
  Person.connection.drop_table :testings rescue nil
310
428
  ActiveRecord::Base.primary_key_prefix_type = nil
@@ -360,6 +478,13 @@ if ActiveRecord::Base.connection.supports_migrations?
360
478
  Person.connection.drop_table table_name rescue nil
361
479
  end
362
480
 
481
+ def test_create_table_without_a_block
482
+ table_name = :testings
483
+ Person.connection.create_table table_name
484
+ ensure
485
+ Person.connection.drop_table table_name rescue nil
486
+ end
487
+
363
488
  # Sybase, and SQLite3 will not allow you to add a NOT NULL
364
489
  # column to a table without a default value.
365
490
  unless current_adapter?(:SybaseAdapter, :SQLiteAdapter, :IBM_DBAdapter)
@@ -456,7 +581,7 @@ if ActiveRecord::Base.connection.supports_migrations?
456
581
  assert_equal 9, wealth_column.precision
457
582
  assert_equal 7, wealth_column.scale
458
583
  end
459
-
584
+
460
585
  def test_native_types
461
586
  Person.delete_all
462
587
  Person.connection.add_column "people", "last_name", :string
@@ -506,32 +631,37 @@ if ActiveRecord::Base.connection.supports_migrations?
506
631
  assert_equal Date, bob.favorite_day.class
507
632
  end
508
633
 
509
- # Test DateTime column and defaults, including timezone.
510
- # FIXME: moment of truth may be Time on 64-bit platforms.
511
- if bob.moment_of_truth.is_a?(DateTime)
512
-
513
- with_env_tz 'US/Eastern' do
514
- assert_equal DateTime.local_offset, bob.moment_of_truth.offset
515
- assert_not_equal 0, bob.moment_of_truth.offset
516
- assert_not_equal "Z", bob.moment_of_truth.zone
517
- # US/Eastern is -5 hours from GMT
518
- assert_equal Rational(-5, 24), bob.moment_of_truth.offset
519
- assert_match /\A-05:?00\Z/, bob.moment_of_truth.zone #ruby 1.8.6 uses HH:MM, prior versions use HHMM
520
- assert_equal DateTime::ITALY, bob.moment_of_truth.start
634
+ # Oracle adapter stores Time or DateTime with timezone value already in _before_type_cast column
635
+ # therefore no timezone change is done afterwards when default timezone is changed
636
+ unless current_adapter?(:OracleAdapter)
637
+ # Test DateTime column and defaults, including timezone.
638
+ # FIXME: moment of truth may be Time on 64-bit platforms.
639
+ if bob.moment_of_truth.is_a?(DateTime)
640
+
641
+ with_env_tz 'US/Eastern' do
642
+ bob.reload
643
+ assert_equal DateTime.local_offset, bob.moment_of_truth.offset
644
+ assert_not_equal 0, bob.moment_of_truth.offset
645
+ assert_not_equal "Z", bob.moment_of_truth.zone
646
+ # US/Eastern is -5 hours from GMT
647
+ assert_equal Rational(-5, 24), bob.moment_of_truth.offset
648
+ assert_match(/\A-05:?00\Z/, bob.moment_of_truth.zone) #ruby 1.8.6 uses HH:MM, prior versions use HHMM
649
+ assert_equal DateTime::ITALY, bob.moment_of_truth.start
650
+ end
521
651
  end
522
652
  end
523
653
 
524
- assert_equal TrueClass, bob.male?.class
654
+ assert_instance_of TrueClass, bob.male?
525
655
  assert_kind_of BigDecimal, bob.wealth
526
656
  end
527
657
 
528
- if current_adapter?(:MysqlAdapter)
658
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
529
659
  def test_unabstracted_database_dependent_types
530
660
  Person.delete_all
531
661
 
532
662
  ActiveRecord::Migration.add_column :people, :intelligence_quotient, :tinyint
533
663
  Person.reset_column_information
534
- assert_match /tinyint/, Person.columns_hash['intelligence_quotient'].sql_type
664
+ assert_match(/tinyint/, Person.columns_hash['intelligence_quotient'].sql_type)
535
665
  ensure
536
666
  ActiveRecord::Migration.remove_column :people, :intelligence_quotient rescue nil
537
667
  end
@@ -565,6 +695,53 @@ if ActiveRecord::Base.connection.supports_migrations?
565
695
  assert !Person.column_methods_hash.include?(:last_name)
566
696
  end
567
697
 
698
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
699
+ def testing_table_for_positioning
700
+ Person.connection.create_table :testings, :id => false do |t|
701
+ t.column :first, :integer
702
+ t.column :second, :integer
703
+ t.column :third, :integer
704
+ end
705
+
706
+ yield Person.connection
707
+ ensure
708
+ Person.connection.drop_table :testings rescue nil
709
+ end
710
+ protected :testing_table_for_positioning
711
+
712
+ def test_column_positioning
713
+ testing_table_for_positioning do |conn|
714
+ assert_equal %w(first second third), conn.columns(:testings).map {|c| c.name }
715
+ end
716
+ end
717
+
718
+ def test_add_column_with_positioning
719
+ testing_table_for_positioning do |conn|
720
+ conn.add_column :testings, :new_col, :integer
721
+ assert_equal %w(first second third new_col), conn.columns(:testings).map {|c| c.name }
722
+ end
723
+ testing_table_for_positioning do |conn|
724
+ conn.add_column :testings, :new_col, :integer, :first => true
725
+ assert_equal %w(new_col first second third), conn.columns(:testings).map {|c| c.name }
726
+ end
727
+ testing_table_for_positioning do |conn|
728
+ conn.add_column :testings, :new_col, :integer, :after => :first
729
+ assert_equal %w(first new_col second third), conn.columns(:testings).map {|c| c.name }
730
+ end
731
+ end
732
+
733
+ def test_change_column_with_positioning
734
+ testing_table_for_positioning do |conn|
735
+ conn.change_column :testings, :second, :integer, :first => true
736
+ assert_equal %w(second first third), conn.columns(:testings).map {|c| c.name }
737
+ end
738
+ testing_table_for_positioning do |conn|
739
+ conn.change_column :testings, :second, :integer, :after => :third
740
+ assert_equal %w(first third second), conn.columns(:testings).map {|c| c.name }
741
+ end
742
+ end
743
+ end
744
+
568
745
  if (!current_adapter?(:IBM_DBAdapter) || @ibm_db_rename_supported)
569
746
  def test_add_rename
570
747
  Person.delete_all
@@ -669,7 +846,20 @@ if ActiveRecord::Base.connection.supports_migrations?
669
846
  ensure
670
847
  ActiveRecord::Base.connection.drop_table(:hats)
671
848
  end
672
- end
849
+ end
850
+
851
+ def test_rename_column_with_an_index
852
+ ActiveRecord::Base.connection.create_table(:hats) do |table|
853
+ table.column :hat_name, :string, :limit => 100
854
+ table.column :hat_size, :integer
855
+ end
856
+ Person.connection.add_index :hats, :hat_name
857
+ assert_nothing_raised do
858
+ Person.connection.rename_column "hats", "hat_name", "name"
859
+ end
860
+ ensure
861
+ ActiveRecord::Base.connection.drop_table(:hats)
862
+ end
673
863
 
674
864
  def test_remove_column_with_index
675
865
  ActiveRecord::Base.connection.create_table(:hats) do |table|
@@ -710,6 +900,32 @@ if ActiveRecord::Base.connection.supports_migrations?
710
900
  end
711
901
  end
712
902
 
903
+ if current_adapter?(:SQLiteAdapter)
904
+ def test_rename_table_for_sqlite_should_work_with_reserved_words
905
+ begin
906
+ assert_nothing_raised do
907
+ ActiveRecord::Base.connection.rename_table :references, :old_references
908
+ ActiveRecord::Base.connection.create_table :octopuses do |t|
909
+ t.column :url, :string
910
+ end
911
+ end
912
+
913
+ assert_nothing_raised { ActiveRecord::Base.connection.rename_table :octopuses, :references }
914
+
915
+ # Using explicit id in insert for compatibility across all databases
916
+ con = ActiveRecord::Base.connection
917
+ assert_nothing_raised do
918
+ con.execute "INSERT INTO 'references' (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://rubyonrails.com')"
919
+ end
920
+ assert_equal 'http://rubyonrails.com', ActiveRecord::Base.connection.select_value("SELECT url FROM 'references' WHERE id=1")
921
+
922
+ ensure
923
+ ActiveRecord::Base.connection.drop_table :references
924
+ ActiveRecord::Base.connection.rename_table :old_references, :references
925
+ end
926
+ end
927
+ end
928
+
713
929
  def test_rename_table
714
930
  begin
715
931
  ActiveRecord::Base.connection.create_table :octopuses do |t|
@@ -772,19 +988,20 @@ if ActiveRecord::Base.connection.supports_migrations?
772
988
  unless current_adapter?(:IBM_DBAdapter) # incompatible types changes
773
989
  def test_change_column
774
990
  Person.connection.add_column 'people', 'age', :integer
775
- old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
991
+ label = "test_change_column Columns"
992
+ old_columns = Person.connection.columns(Person.table_name, label)
776
993
  assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
777
-
994
+
778
995
  assert_nothing_raised { Person.connection.change_column "people", "age", :string }
779
-
780
- new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
996
+
997
+ new_columns = Person.connection.columns(Person.table_name, label)
781
998
  assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
782
999
  assert new_columns.find { |c| c.name == 'age' and c.type == :string }
783
1000
 
784
- old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
1001
+ old_columns = Topic.connection.columns(Topic.table_name, label)
785
1002
  assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
786
1003
  assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
787
- new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
1004
+ new_columns = Topic.connection.columns(Topic.table_name, label)
788
1005
  assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
789
1006
  assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
790
1007
  assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
@@ -826,13 +1043,21 @@ if ActiveRecord::Base.connection.supports_migrations?
826
1043
 
827
1044
  def test_change_column_quotes_column_names
828
1045
  Person.connection.create_table :testings do |t|
829
- t.column :select, :string
1046
+ if current_adapter?(:IBM_DBAdapter)
1047
+ t.column :select, :string, :limit => 5
1048
+ else
1049
+ t.column :select, :string
1050
+ end
830
1051
  end
831
- unless current_adapter?(:IBM_DBAdapter) # altering a string column to smaller size
832
- assert_nothing_raised { Person.connection.change_column :testings, :select, :string, :limit => 10 }
833
- end
834
1052
 
835
- assert_nothing_raised { Person.connection.execute "insert into testings (#{Person.connection.quote_column_name('select')}) values ('7 chars')" }
1053
+ assert_nothing_raised { Person.connection.change_column :testings, :select, :string, :limit => 10 }
1054
+
1055
+ # Oracle needs primary key value from sequence
1056
+ if current_adapter?(:OracleAdapter)
1057
+ assert_nothing_raised { Person.connection.execute "insert into testings (id, #{Person.connection.quote_column_name('select')}) values (testings_seq.nextval, '7 chars')" }
1058
+ else
1059
+ assert_nothing_raised { Person.connection.execute "insert into testings (#{Person.connection.quote_column_name('select')}) values ('7 chars')" }
1060
+ end
836
1061
  ensure
837
1062
  Person.connection.drop_table :testings rescue nil
838
1063
  end
@@ -848,7 +1073,12 @@ if ActiveRecord::Base.connection.supports_migrations?
848
1073
  person_klass.reset_column_information
849
1074
  assert_equal 99, person_klass.columns_hash["wealth"].default
850
1075
  assert_equal false, person_klass.columns_hash["wealth"].null
851
- assert_nothing_raised {person_klass.connection.execute("insert into testings (title) values ('tester')")}
1076
+ # Oracle needs primary key value from sequence
1077
+ if current_adapter?(:OracleAdapter)
1078
+ assert_nothing_raised {person_klass.connection.execute("insert into testings (id, title) values (testings_seq.nextval, 'tester')")}
1079
+ else
1080
+ assert_nothing_raised {person_klass.connection.execute("insert into testings (title) values ('tester')")}
1081
+ end
852
1082
 
853
1083
  # change column default to see that column doesn't lose its not null definition
854
1084
  person_klass.connection.change_column_default "testings", "wealth", 100
@@ -913,15 +1143,50 @@ if ActiveRecord::Base.connection.supports_migrations?
913
1143
  end
914
1144
 
915
1145
  def test_change_column_default_to_null
916
- if current_adapter?(:IBM_DBAdapter)
917
- #Ensure that first_name had some default value else an error is thrown saying operation not valid
918
- Person.connection.change_column_default "people", "first_name", "foo"
919
- end
920
1146
  Person.connection.change_column_default "people", "first_name", nil
921
1147
  Person.reset_column_information
922
1148
  assert_nil Person.new.first_name
923
1149
  end
924
1150
 
1151
+ def test_column_exists
1152
+ Person.connection.create_table :testings do |t|
1153
+ t.column :foo, :string
1154
+ end
1155
+
1156
+ assert Person.connection.column_exists?(:testings, :foo)
1157
+ assert !Person.connection.column_exists?(:testings, :bar)
1158
+ ensure
1159
+ Person.connection.drop_table :testings rescue nil
1160
+ end
1161
+
1162
+ def test_column_exists_with_type
1163
+ Person.connection.create_table :testings do |t|
1164
+ t.column :foo, :string
1165
+ t.column :bar, :decimal, :precision => 8, :scale => 2
1166
+ end
1167
+
1168
+ assert Person.connection.column_exists?(:testings, :foo, :string)
1169
+ assert !Person.connection.column_exists?(:testings, :foo, :integer)
1170
+ assert Person.connection.column_exists?(:testings, :bar, :decimal)
1171
+ assert !Person.connection.column_exists?(:testings, :bar, :integer)
1172
+ ensure
1173
+ Person.connection.drop_table :testings rescue nil
1174
+ end
1175
+
1176
+ def test_column_exists_with_definition
1177
+ Person.connection.create_table :testings do |t|
1178
+ t.column :foo, :string, :limit => 100
1179
+ t.column :bar, :decimal, :precision => 8, :scale => 2
1180
+ end
1181
+
1182
+ assert Person.connection.column_exists?(:testings, :foo, :string, :limit => 100)
1183
+ assert !Person.connection.column_exists?(:testings, :foo, :string, :limit => 50)
1184
+ assert Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
1185
+ assert !Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
1186
+ ensure
1187
+ Person.connection.drop_table :testings rescue nil
1188
+ end
1189
+
925
1190
  def test_add_table
926
1191
  assert !Reminder.table_exists?
927
1192
 
@@ -979,7 +1244,7 @@ if ActiveRecord::Base.connection.supports_migrations?
979
1244
  # This one is fun. The 'value_of_e' field is defined as 'DECIMAL' with
980
1245
  # precision/scale explicitly left out. By the SQL standard, numbers
981
1246
  # assigned to this field should be truncated but that's seldom respected.
982
- if current_adapter?(:PostgreSQLAdapter, :SQLite2Adapter)
1247
+ if current_adapter?(:PostgreSQLAdapter)
983
1248
  # - PostgreSQL changes the SQL spec on columns declared simply as
984
1249
  # "decimal" to something more useful: instead of being given a scale
985
1250
  # of 0, they take on the compile-time limit for precision and scale,
@@ -992,7 +1257,7 @@ if ActiveRecord::Base.connection.supports_migrations?
992
1257
  elsif current_adapter?(:SQLiteAdapter)
993
1258
  # - SQLite3 stores a float, in violation of SQL
994
1259
  assert_kind_of BigDecimal, b.value_of_e
995
- assert_equal BigDecimal("2.71828182845905"), b.value_of_e
1260
+ assert_in_delta BigDecimal("2.71828182845905"), b.value_of_e, 0.00000000000001
996
1261
  else
997
1262
  # - SQL standard is an integer
998
1263
  unless current_adapter?(:IBM_DBAdapter) # incompatible types retrieved
@@ -1045,9 +1310,9 @@ if ActiveRecord::Base.connection.supports_migrations?
1045
1310
 
1046
1311
  def test_migrator_one_down
1047
1312
  ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid")
1048
-
1313
+
1049
1314
  ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", 1)
1050
-
1315
+
1051
1316
  Person.reset_column_information
1052
1317
  assert Person.column_methods_hash.include?(:last_name)
1053
1318
  assert !Reminder.table_exists?
@@ -1103,18 +1368,31 @@ if ActiveRecord::Base.connection.supports_migrations?
1103
1368
  def test_finds_pending_migrations
1104
1369
  ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1)
1105
1370
  migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations
1371
+
1106
1372
  assert_equal 1, migrations.size
1107
1373
  assert_equal migrations[0].version, 3
1108
1374
  assert_equal migrations[0].name, 'InnocentJointable'
1109
1375
  end
1110
1376
 
1377
+ def test_relative_migrations
1378
+ $".delete_if do |fname|
1379
+ fname == (MIGRATIONS_ROOT + "/valid/1_people_have_last_names.rb")
1380
+ end
1381
+ Object.send(:remove_const, :PeopleHaveLastNames)
1382
+
1383
+ Dir.chdir(MIGRATIONS_ROOT) do
1384
+ ActiveRecord::Migrator.up("valid/", 1)
1385
+ end
1386
+
1387
+ assert defined?(PeopleHaveLastNames)
1388
+ end
1389
+
1111
1390
  def test_only_loads_pending_migrations
1112
1391
  # migrate up to 1
1113
1392
  ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1)
1114
1393
 
1115
1394
  # now unload the migrations that have been defined
1116
- PeopleHaveLastNames.unloadable
1117
- ActiveSupport::Dependencies.remove_unloadable_constants!
1395
+ Object.send(:remove_const, :PeopleHaveLastNames)
1118
1396
 
1119
1397
  ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", nil)
1120
1398
 
@@ -1128,23 +1406,32 @@ if ActiveRecord::Base.connection.supports_migrations?
1128
1406
  load(MIGRATIONS_ROOT + "/valid/1_people_have_last_names.rb")
1129
1407
  end
1130
1408
 
1131
- def test_migrator_interleaved_migrations
1132
- ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_1")
1409
+ def test_target_version_zero_should_run_only_once
1410
+ # migrate up to 1
1411
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
1133
1412
 
1134
- assert_nothing_raised do
1135
- ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2")
1136
- end
1413
+ # migrate down to 0
1414
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 0)
1137
1415
 
1138
- Person.reset_column_information
1139
- assert Person.column_methods_hash.include?(:last_name)
1416
+ # now unload the migrations that have been defined
1417
+ PeopleHaveLastNames.unloadable
1418
+ ActiveSupport::Dependencies.remove_unloadable_constants!
1140
1419
 
1141
- assert_nothing_raised do
1142
- ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/interleaved/pass_3")
1143
- end
1420
+ # migrate down to 0 again
1421
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 0)
1422
+
1423
+ assert !defined? PeopleHaveLastNames
1424
+ ensure
1425
+ load(MIGRATIONS_ROOT + "/valid/1_people_have_last_names.rb")
1144
1426
  end
1145
1427
 
1146
1428
  def test_migrator_db_has_no_schema_migrations_table
1147
- ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations;")
1429
+ # Oracle adapter raises error if semicolon is present as last character
1430
+ if current_adapter?(:OracleAdapter)
1431
+ ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations")
1432
+ else
1433
+ ActiveRecord::Base.connection.execute("DROP TABLE schema_migrations;")
1434
+ end
1148
1435
  assert_nothing_raised do
1149
1436
  ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
1150
1437
  end
@@ -1182,24 +1469,49 @@ if ActiveRecord::Base.connection.supports_migrations?
1182
1469
  assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
1183
1470
  assert_equal "hello world", Reminder.find(:first).content
1184
1471
  end
1185
-
1472
+
1186
1473
  def test_migrator_rollback
1187
1474
  ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
1188
1475
  assert_equal(3, ActiveRecord::Migrator.current_version)
1189
-
1476
+
1190
1477
  ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1191
1478
  assert_equal(2, ActiveRecord::Migrator.current_version)
1192
-
1479
+
1193
1480
  ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1194
1481
  assert_equal(1, ActiveRecord::Migrator.current_version)
1195
-
1482
+
1196
1483
  ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1197
1484
  assert_equal(0, ActiveRecord::Migrator.current_version)
1198
-
1485
+
1199
1486
  ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1200
1487
  assert_equal(0, ActiveRecord::Migrator.current_version)
1201
1488
  end
1202
1489
 
1490
+ def test_migrator_forward
1491
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
1492
+ assert_equal(1, ActiveRecord::Migrator.current_version)
1493
+
1494
+ ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid", 2)
1495
+ assert_equal(3, ActiveRecord::Migrator.current_version)
1496
+
1497
+ ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid")
1498
+ assert_equal(3, ActiveRecord::Migrator.current_version)
1499
+ end
1500
+
1501
+ def test_get_all_versions
1502
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
1503
+ assert_equal([1,2,3], ActiveRecord::Migrator.get_all_versions)
1504
+
1505
+ ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1506
+ assert_equal([1,2], ActiveRecord::Migrator.get_all_versions)
1507
+
1508
+ ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1509
+ assert_equal([1], ActiveRecord::Migrator.get_all_versions)
1510
+
1511
+ ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
1512
+ assert_equal([], ActiveRecord::Migrator.get_all_versions)
1513
+ end
1514
+
1203
1515
  def test_schema_migrations_table_name
1204
1516
  ActiveRecord::Base.table_name_prefix = "prefix_"
1205
1517
  ActiveRecord::Base.table_name_suffix = "_suffix"
@@ -1274,7 +1586,7 @@ if ActiveRecord::Base.connection.supports_migrations?
1274
1586
  columns = Person.connection.columns(:binary_testings)
1275
1587
  data_column = columns.detect { |c| c.name == "data" }
1276
1588
 
1277
- if current_adapter?(:MysqlAdapter)
1589
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
1278
1590
  assert_equal '', data_column.default
1279
1591
  else
1280
1592
  assert_nil data_column.default
@@ -1305,8 +1617,8 @@ if ActiveRecord::Base.connection.supports_migrations?
1305
1617
  return unless current_adapter? :OracleAdapter
1306
1618
 
1307
1619
  # table name is 29 chars, the standard sequence name will
1308
- # be 33 chars and fail
1309
- assert_raise(ActiveRecord::StatementInvalid) do
1620
+ # be 33 chars and should be shortened
1621
+ assert_nothing_raised do
1310
1622
  begin
1311
1623
  Person.connection.create_table :table_with_name_thats_just_ok do |t|
1312
1624
  t.column :foo, :string, :null => false
@@ -1347,7 +1659,7 @@ if ActiveRecord::Base.connection.supports_migrations?
1347
1659
  end
1348
1660
 
1349
1661
  end
1350
-
1662
+
1351
1663
  class SexyMigrationsTest < ActiveRecord::TestCase
1352
1664
  def test_references_column_type_adds_id
1353
1665
  with_new_table do |t|
@@ -1372,48 +1684,113 @@ if ActiveRecord::Base.connection.supports_migrations?
1372
1684
  end
1373
1685
  end
1374
1686
 
1375
- def test_belongs_to_works_like_references
1376
- with_new_table do |t|
1377
- t.expects(:column).with('customer_id', :integer, {})
1378
- t.belongs_to :customer
1379
- end
1687
+ def test_belongs_to_works_like_references
1688
+ with_new_table do |t|
1689
+ t.expects(:column).with('customer_id', :integer, {})
1690
+ t.belongs_to :customer
1380
1691
  end
1692
+ end
1381
1693
 
1382
- def test_timestamps_creates_updated_at_and_created_at
1383
- with_new_table do |t|
1384
- t.expects(:column).with(:created_at, :datetime, kind_of(Hash))
1385
- t.expects(:column).with(:updated_at, :datetime, kind_of(Hash))
1386
- t.timestamps
1387
- end
1694
+ def test_timestamps_creates_updated_at_and_created_at
1695
+ with_new_table do |t|
1696
+ t.expects(:column).with(:created_at, :datetime, kind_of(Hash))
1697
+ t.expects(:column).with(:updated_at, :datetime, kind_of(Hash))
1698
+ t.timestamps
1388
1699
  end
1700
+ end
1389
1701
 
1390
- def test_integer_creates_integer_column
1391
- with_new_table do |t|
1392
- t.expects(:column).with(:foo, 'integer', {})
1393
- t.expects(:column).with(:bar, 'integer', {})
1394
- t.integer :foo, :bar
1395
- end
1702
+ def test_integer_creates_integer_column
1703
+ with_new_table do |t|
1704
+ t.expects(:column).with(:foo, 'integer', {})
1705
+ t.expects(:column).with(:bar, 'integer', {})
1706
+ t.integer :foo, :bar
1396
1707
  end
1708
+ end
1709
+
1710
+ def test_string_creates_string_column
1711
+ with_new_table do |t|
1712
+ t.expects(:column).with(:foo, 'string', {})
1713
+ t.expects(:column).with(:bar, 'string', {})
1714
+ t.string :foo, :bar
1715
+ end
1716
+ end
1397
1717
 
1398
- def test_string_creates_string_column
1718
+ if current_adapter?(:PostgreSQLAdapter)
1719
+ def test_xml_creates_xml_column
1399
1720
  with_new_table do |t|
1400
- t.expects(:column).with(:foo, 'string', {})
1401
- t.expects(:column).with(:bar, 'string', {})
1402
- t.string :foo, :bar
1721
+ t.expects(:column).with(:data, 'xml', {})
1722
+ t.xml :data
1403
1723
  end
1404
1724
  end
1725
+ end
1405
1726
 
1406
- protected
1407
- def with_new_table
1408
- Person.connection.create_table :delete_me, :force => true do |t|
1409
- yield t
1410
- end
1411
- ensure
1412
- Person.connection.drop_table :delete_me rescue nil
1727
+ protected
1728
+ def with_new_table
1729
+ Person.connection.create_table :delete_me, :force => true do |t|
1730
+ yield t
1413
1731
  end
1732
+ ensure
1733
+ Person.connection.drop_table :delete_me rescue nil
1734
+ end
1414
1735
 
1415
1736
  end # SexyMigrationsTest
1416
1737
 
1738
+ class MigrationLoggerTest < ActiveRecord::TestCase
1739
+ def setup
1740
+ Object.send(:remove_const, :InnocentJointable)
1741
+ end
1742
+
1743
+ def test_migration_should_be_run_without_logger
1744
+ previous_logger = ActiveRecord::Base.logger
1745
+ ActiveRecord::Base.logger = nil
1746
+ assert_nothing_raised do
1747
+ ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
1748
+ end
1749
+ ensure
1750
+ ActiveRecord::Base.logger = previous_logger
1751
+ end
1752
+ end
1753
+
1754
+ class InterleavedMigrationsTest < ActiveRecord::TestCase
1755
+ def setup
1756
+ Object.send(:remove_const, :PeopleHaveLastNames)
1757
+ end
1758
+
1759
+ def test_migrator_interleaved_migrations
1760
+ ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_1")
1761
+
1762
+ assert_nothing_raised do
1763
+ ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2")
1764
+ end
1765
+
1766
+ Person.reset_column_information
1767
+ assert Person.column_methods_hash.include?(:last_name)
1768
+
1769
+ Object.send(:remove_const, :PeopleHaveLastNames)
1770
+ Object.send(:remove_const, :InnocentJointable)
1771
+ assert_nothing_raised do
1772
+ ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/interleaved/pass_3")
1773
+ end
1774
+ end
1775
+ end
1776
+
1777
+ class ReservedWordsMigrationTest < ActiveRecord::TestCase
1778
+ def test_drop_index_from_table_named_values
1779
+ connection = Person.connection
1780
+ connection.create_table :values, :force => true do |t|
1781
+ t.integer :value
1782
+ end
1783
+
1784
+ assert_nothing_raised do
1785
+ connection.add_index :values, :value
1786
+ connection.remove_index :values, :column => :value
1787
+ end
1788
+
1789
+ connection.drop_table :values rescue nil
1790
+ end
1791
+ end
1792
+
1793
+
1417
1794
  class ChangeTableMigrationsTest < ActiveRecord::TestCase
1418
1795
  def setup
1419
1796
  @connection = Person.connection
@@ -1421,198 +1798,217 @@ if ActiveRecord::Base.connection.supports_migrations?
1421
1798
  end
1422
1799
  end
1423
1800
 
1424
- def teardown
1425
- Person.connection.drop_table :delete_me rescue nil
1801
+ def teardown
1802
+ Person.connection.drop_table :delete_me rescue nil
1803
+ end
1804
+
1805
+ def test_references_column_type_adds_id
1806
+ with_change_table do |t|
1807
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
1808
+ t.references :customer
1426
1809
  end
1810
+ end
1427
1811
 
1428
- def test_references_column_type_adds_id
1429
- with_change_table do |t|
1430
- @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
1431
- t.references :customer
1432
- end
1812
+ def test_remove_references_column_type_removes_id
1813
+ with_change_table do |t|
1814
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
1815
+ t.remove_references :customer
1433
1816
  end
1817
+ end
1434
1818
 
1435
- def test_remove_references_column_type_removes_id
1436
- with_change_table do |t|
1437
- @connection.expects(:remove_column).with(:delete_me, 'customer_id')
1438
- t.remove_references :customer
1439
- end
1819
+ def test_add_belongs_to_works_like_add_references
1820
+ with_change_table do |t|
1821
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
1822
+ t.belongs_to :customer
1440
1823
  end
1824
+ end
1441
1825
 
1442
- def test_add_belongs_to_works_like_add_references
1443
- with_change_table do |t|
1444
- @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
1445
- t.belongs_to :customer
1446
- end
1826
+ def test_remove_belongs_to_works_like_remove_references
1827
+ with_change_table do |t|
1828
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
1829
+ t.remove_belongs_to :customer
1447
1830
  end
1831
+ end
1448
1832
 
1449
- def test_remove_belongs_to_works_like_remove_references
1450
- with_change_table do |t|
1451
- @connection.expects(:remove_column).with(:delete_me, 'customer_id')
1452
- t.remove_belongs_to :customer
1453
- end
1833
+ def test_references_column_type_with_polymorphic_adds_type
1834
+ with_change_table do |t|
1835
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {})
1836
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {})
1837
+ t.references :taggable, :polymorphic => true
1454
1838
  end
1839
+ end
1455
1840
 
1456
- def test_references_column_type_with_polymorphic_adds_type
1457
- with_change_table do |t|
1458
- @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {})
1459
- @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {})
1460
- t.references :taggable, :polymorphic => true
1461
- end
1841
+ def test_remove_references_column_type_with_polymorphic_removes_type
1842
+ with_change_table do |t|
1843
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
1844
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
1845
+ t.remove_references :taggable, :polymorphic => true
1462
1846
  end
1847
+ end
1463
1848
 
1464
- def test_remove_references_column_type_with_polymorphic_removes_type
1465
- with_change_table do |t|
1466
- @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
1467
- @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
1468
- t.remove_references :taggable, :polymorphic => true
1469
- end
1849
+ def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
1850
+ with_change_table do |t|
1851
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false})
1852
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false})
1853
+ t.references :taggable, :polymorphic => true, :null => false
1470
1854
  end
1855
+ end
1471
1856
 
1472
- def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
1473
- with_change_table do |t|
1474
- @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false})
1475
- @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false})
1476
- t.references :taggable, :polymorphic => true, :null => false
1477
- end
1857
+ def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
1858
+ with_change_table do |t|
1859
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
1860
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
1861
+ t.remove_references :taggable, :polymorphic => true, :null => false
1478
1862
  end
1863
+ end
1479
1864
 
1480
- def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
1481
- with_change_table do |t|
1482
- @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
1483
- @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
1484
- t.remove_references :taggable, :polymorphic => true, :null => false
1485
- end
1865
+ def test_timestamps_creates_updated_at_and_created_at
1866
+ with_change_table do |t|
1867
+ @connection.expects(:add_timestamps).with(:delete_me)
1868
+ t.timestamps
1486
1869
  end
1870
+ end
1487
1871
 
1488
- def test_timestamps_creates_updated_at_and_created_at
1489
- with_change_table do |t|
1490
- @connection.expects(:add_timestamps).with(:delete_me)
1491
- t.timestamps
1492
- end
1872
+ def test_remove_timestamps_creates_updated_at_and_created_at
1873
+ with_change_table do |t|
1874
+ @connection.expects(:remove_timestamps).with(:delete_me)
1875
+ t.remove_timestamps
1493
1876
  end
1877
+ end
1494
1878
 
1495
- def test_remove_timestamps_creates_updated_at_and_created_at
1496
- with_change_table do |t|
1497
- @connection.expects(:remove_timestamps).with(:delete_me)
1498
- t.remove_timestamps
1499
- end
1879
+ def string_column
1880
+ if current_adapter?(:PostgreSQLAdapter)
1881
+ "character varying(255)"
1882
+ elsif current_adapter?(:OracleAdapter)
1883
+ 'VARCHAR2(255)'
1884
+ else
1885
+ 'varchar(255)'
1500
1886
  end
1887
+ end
1501
1888
 
1502
- def string_column
1503
- if current_adapter?(:PostgreSQLAdapter)
1504
- "character varying(255)"
1505
- else
1506
- 'varchar(255)'
1507
- end
1889
+ def integer_column
1890
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
1891
+ 'int(11)'
1892
+ elsif current_adapter?(:OracleAdapter)
1893
+ 'NUMBER(38)'
1894
+ else
1895
+ 'integer'
1508
1896
  end
1897
+ end
1509
1898
 
1510
- def integer_column
1511
- if current_adapter?(:MysqlAdapter)
1512
- 'int(11)'
1513
- else
1514
- 'integer'
1515
- end
1899
+ def test_integer_creates_integer_column
1900
+ with_change_table do |t|
1901
+ @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {})
1902
+ @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {})
1903
+ t.integer :foo, :bar
1516
1904
  end
1905
+ end
1517
1906
 
1518
- def test_integer_creates_integer_column
1519
- with_change_table do |t|
1520
- @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {})
1521
- @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {})
1522
- t.integer :foo, :bar
1523
- end
1907
+ def test_string_creates_string_column
1908
+ with_change_table do |t|
1909
+ @connection.expects(:add_column).with(:delete_me, :foo, string_column, {})
1910
+ @connection.expects(:add_column).with(:delete_me, :bar, string_column, {})
1911
+ t.string :foo, :bar
1524
1912
  end
1913
+ end
1525
1914
 
1526
- def test_string_creates_string_column
1527
- with_change_table do |t|
1528
- @connection.expects(:add_column).with(:delete_me, :foo, string_column, {})
1529
- @connection.expects(:add_column).with(:delete_me, :bar, string_column, {})
1530
- t.string :foo, :bar
1531
- end
1915
+ def test_column_creates_column
1916
+ with_change_table do |t|
1917
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {})
1918
+ t.column :bar, :integer
1532
1919
  end
1920
+ end
1533
1921
 
1534
- def test_column_creates_column
1535
- with_change_table do |t|
1536
- @connection.expects(:add_column).with(:delete_me, :bar, :integer, {})
1537
- t.column :bar, :integer
1538
- end
1922
+ def test_column_creates_column_with_options
1923
+ with_change_table do |t|
1924
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false})
1925
+ t.column :bar, :integer, :null => false
1539
1926
  end
1927
+ end
1540
1928
 
1541
- def test_column_creates_column_with_options
1542
- with_change_table do |t|
1543
- @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false})
1544
- t.column :bar, :integer, :null => false
1545
- end
1929
+ def test_index_creates_index
1930
+ with_change_table do |t|
1931
+ @connection.expects(:add_index).with(:delete_me, :bar, {})
1932
+ t.index :bar
1546
1933
  end
1934
+ end
1547
1935
 
1548
- def test_index_creates_index
1549
- with_change_table do |t|
1550
- @connection.expects(:add_index).with(:delete_me, :bar, {})
1551
- t.index :bar
1552
- end
1936
+ def test_index_creates_index_with_options
1937
+ with_change_table do |t|
1938
+ @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true})
1939
+ t.index :bar, :unique => true
1940
+ end
1941
+ end
1942
+
1943
+ def test_index_exists
1944
+ with_change_table do |t|
1945
+ @connection.expects(:index_exists?).with(:delete_me, :bar, {})
1946
+ t.index_exists?(:bar)
1553
1947
  end
1948
+ end
1554
1949
 
1555
- def test_index_creates_index_with_options
1556
- with_change_table do |t|
1557
- @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true})
1558
- t.index :bar, :unique => true
1559
- end
1950
+ def test_index_exists_with_options
1951
+ with_change_table do |t|
1952
+ @connection.expects(:index_exists?).with(:delete_me, :bar, {:unique => true})
1953
+ t.index_exists?(:bar, :unique => true)
1560
1954
  end
1955
+ end
1561
1956
 
1562
- def test_change_changes_column
1563
- with_change_table do |t|
1564
- @connection.expects(:change_column).with(:delete_me, :bar, :string, {})
1565
- t.change :bar, :string
1566
- end
1957
+ def test_change_changes_column
1958
+ with_change_table do |t|
1959
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {})
1960
+ t.change :bar, :string
1567
1961
  end
1962
+ end
1568
1963
 
1569
- def test_change_changes_column_with_options
1570
- with_change_table do |t|
1571
- @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true})
1572
- t.change :bar, :string, :null => true
1573
- end
1964
+ def test_change_changes_column_with_options
1965
+ with_change_table do |t|
1966
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true})
1967
+ t.change :bar, :string, :null => true
1574
1968
  end
1969
+ end
1575
1970
 
1576
- def test_change_default_changes_column
1577
- with_change_table do |t|
1578
- @connection.expects(:change_column_default).with(:delete_me, :bar, :string)
1579
- t.change_default :bar, :string
1580
- end
1971
+ def test_change_default_changes_column
1972
+ with_change_table do |t|
1973
+ @connection.expects(:change_column_default).with(:delete_me, :bar, :string)
1974
+ t.change_default :bar, :string
1581
1975
  end
1976
+ end
1582
1977
 
1583
- def test_remove_drops_single_column
1584
- with_change_table do |t|
1585
- @connection.expects(:remove_column).with(:delete_me, [:bar])
1586
- t.remove :bar
1587
- end
1978
+ def test_remove_drops_single_column
1979
+ with_change_table do |t|
1980
+ @connection.expects(:remove_column).with(:delete_me, [:bar])
1981
+ t.remove :bar
1588
1982
  end
1983
+ end
1589
1984
 
1590
- def test_remove_drops_multiple_columns
1591
- with_change_table do |t|
1592
- @connection.expects(:remove_column).with(:delete_me, [:bar, :baz])
1593
- t.remove :bar, :baz
1594
- end
1985
+ def test_remove_drops_multiple_columns
1986
+ with_change_table do |t|
1987
+ @connection.expects(:remove_column).with(:delete_me, [:bar, :baz])
1988
+ t.remove :bar, :baz
1595
1989
  end
1990
+ end
1596
1991
 
1597
- def test_remove_index_removes_index_with_options
1598
- with_change_table do |t|
1599
- @connection.expects(:remove_index).with(:delete_me, {:unique => true})
1600
- t.remove_index :unique => true
1601
- end
1992
+ def test_remove_index_removes_index_with_options
1993
+ with_change_table do |t|
1994
+ @connection.expects(:remove_index).with(:delete_me, {:unique => true})
1995
+ t.remove_index :unique => true
1602
1996
  end
1997
+ end
1603
1998
 
1604
- def test_rename_renames_column
1605
- with_change_table do |t|
1606
- @connection.expects(:rename_column).with(:delete_me, :bar, :baz)
1607
- t.rename :bar, :baz
1608
- end
1999
+ def test_rename_renames_column
2000
+ with_change_table do |t|
2001
+ @connection.expects(:rename_column).with(:delete_me, :bar, :baz)
2002
+ t.rename :bar, :baz
1609
2003
  end
2004
+ end
1610
2005
 
1611
- protected
1612
- def with_change_table
1613
- Person.connection.change_table :delete_me do |t|
1614
- yield t
1615
- end
2006
+ protected
2007
+ def with_change_table
2008
+ Person.connection.change_table :delete_me do |t|
2009
+ yield t
1616
2010
  end
1617
2011
  end
1618
2012
  end
2013
+ end
2014
+