PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/activerecord/test/migration_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 768 lines | 578 code | 141 blank | 49 comment | 38 complexity | 935d0883b2022baab495b46be4bdd1f8 MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'abstract_unit'
  2. require 'bigdecimal/util'
  3. require 'fixtures/person'
  4. require 'fixtures/topic'
  5. require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
  6. require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
  7. require File.dirname(__FILE__) + '/fixtures/migrations_with_decimal/1_give_me_big_numbers'
  8. if ActiveRecord::Base.connection.supports_migrations?
  9. class BigNumber < ActiveRecord::Base; end
  10. class Reminder < ActiveRecord::Base; end
  11. class ActiveRecord::Migration
  12. class <<self
  13. attr_accessor :message_count
  14. def puts(text="")
  15. self.message_count ||= 0
  16. self.message_count += 1
  17. end
  18. end
  19. end
  20. class MigrationTest < Test::Unit::TestCase
  21. self.use_transactional_fixtures = false
  22. def setup
  23. ActiveRecord::Migration.verbose = true
  24. PeopleHaveLastNames.message_count = 0
  25. end
  26. def teardown
  27. ActiveRecord::Base.connection.initialize_schema_information
  28. ActiveRecord::Base.connection.update "UPDATE #{ActiveRecord::Migrator.schema_info_table_name} SET version = 0"
  29. %w(reminders people_reminders prefix_reminders_suffix).each do |table|
  30. Reminder.connection.drop_table(table) rescue nil
  31. end
  32. Reminder.reset_column_information
  33. %w(last_name key bio age height wealth birthday favorite_day
  34. male administrator).each do |column|
  35. Person.connection.remove_column('people', column) rescue nil
  36. end
  37. Person.connection.remove_column("people", "first_name") rescue nil
  38. Person.connection.remove_column("people", "middle_name") rescue nil
  39. Person.connection.add_column("people", "first_name", :string, :limit => 40)
  40. Person.reset_column_information
  41. end
  42. def test_add_index
  43. # Limit size of last_name and key columns to support Firebird index limitations
  44. Person.connection.add_column "people", "last_name", :string, :limit => 100
  45. Person.connection.add_column "people", "key", :string, :limit => 100
  46. Person.connection.add_column "people", "administrator", :boolean
  47. assert_nothing_raised { Person.connection.add_index("people", "last_name") }
  48. assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
  49. # Orcl nds shrt indx nms. Sybs 2.
  50. unless current_adapter?(:OracleAdapter, :SybaseAdapter)
  51. assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  52. assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
  53. assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  54. assert_nothing_raised { Person.connection.remove_index("people", :name => "index_people_on_last_name_and_first_name") }
  55. assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  56. assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") }
  57. assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  58. assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
  59. end
  60. # quoting
  61. # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
  62. assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) }
  63. assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) }
  64. # Sybase adapter does not support indexes on :boolean columns
  65. unless current_adapter?(:SybaseAdapter)
  66. assert_nothing_raised { Person.connection.add_index("people", %w(last_name first_name administrator), :name => "named_admin") }
  67. assert_nothing_raised { Person.connection.remove_index("people", :name => "named_admin") }
  68. end
  69. end
  70. def test_create_table_adds_id
  71. Person.connection.create_table :testings do |t|
  72. t.column :foo, :string
  73. end
  74. assert_equal %w(foo id),
  75. Person.connection.columns(:testings).map { |c| c.name }.sort
  76. ensure
  77. Person.connection.drop_table :testings rescue nil
  78. end
  79. def test_create_table_with_not_null_column
  80. assert_nothing_raised do
  81. Person.connection.create_table :testings do |t|
  82. t.column :foo, :string, :null => false
  83. end
  84. end
  85. assert_raises(ActiveRecord::StatementInvalid) do
  86. Person.connection.execute "insert into testings (foo) values (NULL)"
  87. end
  88. ensure
  89. Person.connection.drop_table :testings rescue nil
  90. end
  91. def test_create_table_with_defaults
  92. Person.connection.create_table :testings do |t|
  93. t.column :one, :string, :default => "hello"
  94. t.column :two, :boolean, :default => true
  95. t.column :three, :boolean, :default => false
  96. t.column :four, :integer, :default => 1
  97. end
  98. columns = Person.connection.columns(:testings)
  99. one = columns.detect { |c| c.name == "one" }
  100. two = columns.detect { |c| c.name == "two" }
  101. three = columns.detect { |c| c.name == "three" }
  102. four = columns.detect { |c| c.name == "four" }
  103. assert_equal "hello", one.default
  104. assert_equal true, two.default
  105. assert_equal false, three.default
  106. assert_equal 1, four.default
  107. ensure
  108. Person.connection.drop_table :testings rescue nil
  109. end
  110. def test_create_table_with_limits
  111. assert_nothing_raised do
  112. Person.connection.create_table :testings do |t|
  113. t.column :foo, :string, :limit => 255
  114. t.column :default_int, :integer
  115. t.column :one_int, :integer, :limit => 1
  116. t.column :four_int, :integer, :limit => 4
  117. t.column :eight_int, :integer, :limit => 8
  118. end
  119. end
  120. columns = Person.connection.columns(:testings)
  121. foo = columns.detect { |c| c.name == "foo" }
  122. assert_equal 255, foo.limit
  123. default = columns.detect { |c| c.name == "default_int" }
  124. one = columns.detect { |c| c.name == "one_int" }
  125. four = columns.detect { |c| c.name == "four_int" }
  126. eight = columns.detect { |c| c.name == "eight_int" }
  127. if current_adapter?(:PostgreSQLAdapter)
  128. assert_equal 'integer', default.sql_type
  129. assert_equal 'smallint', one.sql_type
  130. assert_equal 'integer', four.sql_type
  131. assert_equal 'bigint', eight.sql_type
  132. elsif current_adapter?(:OracleAdapter)
  133. assert_equal 'NUMBER(38)', default.sql_type
  134. assert_equal 'NUMBER(1)', one.sql_type
  135. assert_equal 'NUMBER(4)', four.sql_type
  136. assert_equal 'NUMBER(8)', eight.sql_type
  137. end
  138. ensure
  139. Person.connection.drop_table :testings rescue nil
  140. end
  141. # SQL Server and Sybase will not allow you to add a NOT NULL column
  142. # to a table without specifying a default value, so the
  143. # following test must be skipped
  144. unless current_adapter?(:SQLServerAdapter, :SybaseAdapter)
  145. def test_add_column_not_null_without_default
  146. Person.connection.create_table :testings do |t|
  147. t.column :foo, :string
  148. end
  149. Person.connection.add_column :testings, :bar, :string, :null => false
  150. assert_raises(ActiveRecord::StatementInvalid) do
  151. Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
  152. end
  153. ensure
  154. Person.connection.drop_table :testings rescue nil
  155. end
  156. end
  157. def test_add_column_not_null_with_default
  158. Person.connection.create_table :testings do |t|
  159. t.column :foo, :string
  160. end
  161. con = Person.connection
  162. Person.connection.enable_identity_insert("testings", true) if current_adapter?(:SybaseAdapter)
  163. Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}) values (1, 'hello')"
  164. Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
  165. assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
  166. assert_raises(ActiveRecord::StatementInvalid) do
  167. 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)"
  168. end
  169. ensure
  170. Person.connection.drop_table :testings rescue nil
  171. end
  172. # We specifically do a manual INSERT here, and then test only the SELECT
  173. # functionality. This allows us to more easily catch INSERT being broken,
  174. # but SELECT actually working fine.
  175. def test_native_decimal_insert_manual_vs_automatic
  176. # SQLite3 always uses float in violation of SQL
  177. # 16 decimal places
  178. correct_value = (current_adapter?(:SQLiteAdapter) ? '0.123456789012346E20' : '0012345678901234567890.0123456789').to_d
  179. Person.delete_all
  180. Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
  181. Person.reset_column_information
  182. # Do a manual insertion
  183. if current_adapter?(:OracleAdapter)
  184. Person.connection.execute "insert into people (id, wealth) values (people_seq.nextval, 12345678901234567890.0123456789)"
  185. else
  186. Person.connection.execute "insert into people (wealth) values (12345678901234567890.0123456789)"
  187. end
  188. # SELECT
  189. row = Person.find(:first)
  190. assert_kind_of BigDecimal, row.wealth
  191. # If this assert fails, that means the SELECT is broken!
  192. assert_equal correct_value, row.wealth
  193. # Reset to old state
  194. Person.delete_all
  195. # Now use the Rails insertion
  196. assert_nothing_raised { Person.create :wealth => BigDecimal.new("12345678901234567890.0123456789") }
  197. # SELECT
  198. row = Person.find(:first)
  199. assert_kind_of BigDecimal, row.wealth
  200. # If these asserts fail, that means the INSERT (create function, or cast to SQL) is broken!
  201. assert_equal correct_value, row.wealth
  202. # Reset to old state
  203. Person.connection.del_column "people", "wealth" rescue nil
  204. Person.reset_column_information
  205. end
  206. def test_native_types
  207. Person.delete_all
  208. Person.connection.add_column "people", "last_name", :string
  209. Person.connection.add_column "people", "bio", :text
  210. Person.connection.add_column "people", "age", :integer
  211. Person.connection.add_column "people", "height", :float
  212. Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
  213. Person.connection.add_column "people", "birthday", :datetime
  214. Person.connection.add_column "people", "favorite_day", :date
  215. Person.connection.add_column "people", "male", :boolean
  216. 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 }
  217. bob = Person.find(:first)
  218. assert_equal 'bob', bob.first_name
  219. assert_equal 'bobsen', bob.last_name
  220. assert_equal "I was born ....", bob.bio
  221. assert_equal 18, bob.age
  222. # Test for 30 significent digits (beyond the 16 of float), 10 of them
  223. # after the decimal place.
  224. if current_adapter?(:SQLiteAdapter)
  225. # SQLite3 uses float in violation of SQL. Test for 16 decimal places.
  226. assert_equal BigDecimal.new('0.123456789012346E20'), bob.wealth
  227. else
  228. assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth
  229. end
  230. assert_equal true, bob.male?
  231. assert_equal String, bob.first_name.class
  232. assert_equal String, bob.last_name.class
  233. assert_equal String, bob.bio.class
  234. assert_equal Fixnum, bob.age.class
  235. assert_equal Time, bob.birthday.class
  236. if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter)
  237. # Sybase, and Oracle don't differentiate between date/time
  238. assert_equal Time, bob.favorite_day.class
  239. else
  240. assert_equal Date, bob.favorite_day.class
  241. end
  242. assert_equal TrueClass, bob.male?.class
  243. assert_kind_of BigDecimal, bob.wealth
  244. end
  245. def test_add_remove_single_field_using_string_arguments
  246. assert !Person.column_methods_hash.include?(:last_name)
  247. ActiveRecord::Migration.add_column 'people', 'last_name', :string
  248. Person.reset_column_information
  249. assert Person.column_methods_hash.include?(:last_name)
  250. ActiveRecord::Migration.remove_column 'people', 'last_name'
  251. Person.reset_column_information
  252. assert !Person.column_methods_hash.include?(:last_name)
  253. end
  254. def test_add_remove_single_field_using_symbol_arguments
  255. assert !Person.column_methods_hash.include?(:last_name)
  256. ActiveRecord::Migration.add_column :people, :last_name, :string
  257. Person.reset_column_information
  258. assert Person.column_methods_hash.include?(:last_name)
  259. ActiveRecord::Migration.remove_column :people, :last_name
  260. Person.reset_column_information
  261. assert !Person.column_methods_hash.include?(:last_name)
  262. end
  263. def test_add_rename
  264. Person.delete_all
  265. begin
  266. Person.connection.add_column "people", "girlfriend", :string
  267. Person.create :girlfriend => 'bobette'
  268. Person.connection.rename_column "people", "girlfriend", "exgirlfriend"
  269. Person.reset_column_information
  270. bob = Person.find(:first)
  271. assert_equal "bobette", bob.exgirlfriend
  272. ensure
  273. Person.connection.remove_column("people", "girlfriend") rescue nil
  274. Person.connection.remove_column("people", "exgirlfriend") rescue nil
  275. end
  276. end
  277. def test_rename_column_using_symbol_arguments
  278. begin
  279. Person.connection.rename_column :people, :first_name, :nick_name
  280. Person.reset_column_information
  281. assert Person.column_names.include?("nick_name")
  282. ensure
  283. Person.connection.remove_column("people","nick_name")
  284. Person.connection.add_column("people","first_name", :string)
  285. end
  286. end
  287. def test_rename_column
  288. begin
  289. Person.connection.rename_column "people", "first_name", "nick_name"
  290. Person.reset_column_information
  291. assert Person.column_names.include?("nick_name")
  292. ensure
  293. Person.connection.remove_column("people","nick_name")
  294. Person.connection.add_column("people","first_name", :string)
  295. end
  296. end
  297. def test_rename_table
  298. begin
  299. ActiveRecord::Base.connection.create_table :octopuses do |t|
  300. t.column :url, :string
  301. end
  302. ActiveRecord::Base.connection.rename_table :octopuses, :octopi
  303. # Using explicit id in insert for compatibility across all databases
  304. con = ActiveRecord::Base.connection
  305. con.enable_identity_insert("octopi", true) if current_adapter?(:SybaseAdapter)
  306. assert_nothing_raised { con.execute "INSERT INTO octopi (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" }
  307. con.enable_identity_insert("octopi", false) if current_adapter?(:SybaseAdapter)
  308. assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
  309. ensure
  310. ActiveRecord::Base.connection.drop_table :octopuses rescue nil
  311. ActiveRecord::Base.connection.drop_table :octopi rescue nil
  312. end
  313. end
  314. def test_rename_table_with_an_index
  315. begin
  316. ActiveRecord::Base.connection.create_table :octopuses do |t|
  317. t.column :url, :string
  318. end
  319. ActiveRecord::Base.connection.add_index :octopuses, :url
  320. ActiveRecord::Base.connection.rename_table :octopuses, :octopi
  321. # Using explicit id in insert for compatibility across all databases
  322. con = ActiveRecord::Base.connection
  323. con.enable_identity_insert("octopi", true) if current_adapter?(:SybaseAdapter)
  324. assert_nothing_raised { con.execute "INSERT INTO octopi (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" }
  325. con.enable_identity_insert("octopi", false) if current_adapter?(:SybaseAdapter)
  326. assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
  327. assert ActiveRecord::Base.connection.indexes(:octopi).first.columns.include?("url")
  328. ensure
  329. ActiveRecord::Base.connection.drop_table :octopuses rescue nil
  330. ActiveRecord::Base.connection.drop_table :octopi rescue nil
  331. end
  332. end
  333. def test_change_column
  334. Person.connection.add_column 'people', 'age', :integer
  335. old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
  336. assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
  337. assert_nothing_raised { Person.connection.change_column "people", "age", :string }
  338. new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
  339. assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
  340. assert new_columns.find { |c| c.name == 'age' and c.type == :string }
  341. old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
  342. assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
  343. assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
  344. new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
  345. assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
  346. assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
  347. assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
  348. end
  349. def test_change_column_with_nil_default
  350. Person.connection.add_column "people", "contributor", :boolean, :default => true
  351. Person.reset_column_information
  352. assert Person.new.contributor?
  353. assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil }
  354. Person.reset_column_information
  355. assert !Person.new.contributor?
  356. assert_nil Person.new.contributor
  357. end
  358. def test_change_column_with_new_default
  359. Person.connection.add_column "people", "administrator", :boolean, :default => true
  360. Person.reset_column_information
  361. assert Person.new.administrator?
  362. assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
  363. Person.reset_column_information
  364. assert !Person.new.administrator?
  365. end
  366. def test_change_column_default
  367. Person.connection.change_column_default "people", "first_name", "Tester"
  368. Person.reset_column_information
  369. assert_equal "Tester", Person.new.first_name
  370. end
  371. def test_change_column_default_to_null
  372. Person.connection.change_column_default "people", "first_name", nil
  373. Person.reset_column_information
  374. assert_nil Person.new.first_name
  375. end
  376. def test_add_table
  377. assert !Reminder.table_exists?
  378. WeNeedReminders.up
  379. assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
  380. assert_equal "hello world", Reminder.find(:first).content
  381. WeNeedReminders.down
  382. assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
  383. end
  384. def test_add_table_with_decimals
  385. Person.connection.drop_table :big_numbers rescue nil
  386. assert !BigNumber.table_exists?
  387. GiveMeBigNumbers.up
  388. assert BigNumber.create(
  389. :bank_balance => 1586.43,
  390. :big_bank_balance => BigDecimal("1000234000567.95"),
  391. :world_population => 6000000000,
  392. :my_house_population => 3,
  393. :value_of_e => BigDecimal("2.7182818284590452353602875")
  394. )
  395. b = BigNumber.find(:first)
  396. assert_not_nil b
  397. assert_not_nil b.bank_balance
  398. assert_not_nil b.big_bank_balance
  399. assert_not_nil b.world_population
  400. assert_not_nil b.my_house_population
  401. assert_not_nil b.value_of_e
  402. # TODO: set world_population >= 2**62 to cover 64-bit platforms and test
  403. # is_a?(Bignum)
  404. assert_kind_of Integer, b.world_population
  405. assert_equal 6000000000, b.world_population
  406. assert_kind_of Fixnum, b.my_house_population
  407. assert_equal 3, b.my_house_population
  408. assert_kind_of BigDecimal, b.bank_balance
  409. assert_equal BigDecimal("1586.43"), b.bank_balance
  410. assert_kind_of BigDecimal, b.big_bank_balance
  411. assert_equal BigDecimal("1000234000567.95"), b.big_bank_balance
  412. # This one is fun. The 'value_of_e' field is defined as 'DECIMAL' with
  413. # precision/scale explictly left out. By the SQL standard, numbers
  414. # assigned to this field should be truncated but that's seldom respected.
  415. if current_adapter?(:PostgreSQLAdapter, :SQLite2Adapter)
  416. # - PostgreSQL changes the SQL spec on columns declared simply as
  417. # "decimal" to something more useful: instead of being given a scale
  418. # of 0, they take on the compile-time limit for precision and scale,
  419. # so the following should succeed unless you have used really wacky
  420. # compilation options
  421. # - SQLite2 has the default behavior of preserving all data sent in,
  422. # so this happens there too
  423. assert_kind_of BigDecimal, b.value_of_e
  424. assert_equal BigDecimal("2.7182818284590452353602875"), b.value_of_e
  425. elsif current_adapter?(:SQLiteAdapter)
  426. # - SQLite3 stores a float, in violation of SQL
  427. assert_kind_of BigDecimal, b.value_of_e
  428. assert_equal BigDecimal("2.71828182845905"), b.value_of_e
  429. elsif current_adapter?(:SQLServer)
  430. # - SQL Server rounds instead of truncating
  431. assert_kind_of Fixnum, b.value_of_e
  432. assert_equal 3, b.value_of_e
  433. else
  434. # - SQL standard is an integer
  435. assert_kind_of Fixnum, b.value_of_e
  436. assert_equal 2, b.value_of_e
  437. end
  438. GiveMeBigNumbers.down
  439. assert_raises(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
  440. end
  441. def test_migrator
  442. assert !Person.column_methods_hash.include?(:last_name)
  443. assert !Reminder.table_exists?
  444. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
  445. assert_equal 3, ActiveRecord::Migrator.current_version
  446. Person.reset_column_information
  447. assert Person.column_methods_hash.include?(:last_name)
  448. assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
  449. assert_equal "hello world", Reminder.find(:first).content
  450. ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
  451. assert_equal 0, ActiveRecord::Migrator.current_version
  452. Person.reset_column_information
  453. assert !Person.column_methods_hash.include?(:last_name)
  454. assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
  455. end
  456. def test_migrator_one_up
  457. assert !Person.column_methods_hash.include?(:last_name)
  458. assert !Reminder.table_exists?
  459. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  460. Person.reset_column_information
  461. assert Person.column_methods_hash.include?(:last_name)
  462. assert !Reminder.table_exists?
  463. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2)
  464. assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
  465. assert_equal "hello world", Reminder.find(:first).content
  466. end
  467. def test_migrator_one_down
  468. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
  469. ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  470. Person.reset_column_information
  471. assert Person.column_methods_hash.include?(:last_name)
  472. assert !Reminder.table_exists?
  473. end
  474. def test_migrator_one_up_one_down
  475. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  476. ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
  477. assert !Person.column_methods_hash.include?(:last_name)
  478. assert !Reminder.table_exists?
  479. end
  480. def test_migrator_verbosity
  481. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  482. assert PeopleHaveLastNames.message_count > 0
  483. PeopleHaveLastNames.message_count = 0
  484. ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
  485. assert PeopleHaveLastNames.message_count > 0
  486. PeopleHaveLastNames.message_count = 0
  487. end
  488. def test_migrator_verbosity_off
  489. PeopleHaveLastNames.verbose = false
  490. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  491. assert PeopleHaveLastNames.message_count.zero?
  492. ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
  493. assert PeopleHaveLastNames.message_count.zero?
  494. end
  495. def test_migrator_going_down_due_to_version_target
  496. ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
  497. ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
  498. assert !Person.column_methods_hash.include?(:last_name)
  499. assert !Reminder.table_exists?
  500. ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/')
  501. Person.reset_column_information
  502. assert Person.column_methods_hash.include?(:last_name)
  503. assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
  504. assert_equal "hello world", Reminder.find(:first).content
  505. end
  506. def test_schema_info_table_name
  507. ActiveRecord::Base.table_name_prefix = "prefix_"
  508. ActiveRecord::Base.table_name_suffix = "_suffix"
  509. Reminder.reset_table_name
  510. assert_equal "prefix_schema_info_suffix", ActiveRecord::Migrator.schema_info_table_name
  511. ActiveRecord::Base.table_name_prefix = ""
  512. ActiveRecord::Base.table_name_suffix = ""
  513. Reminder.reset_table_name
  514. assert_equal "schema_info", ActiveRecord::Migrator.schema_info_table_name
  515. ensure
  516. ActiveRecord::Base.table_name_prefix = ""
  517. ActiveRecord::Base.table_name_suffix = ""
  518. end
  519. def test_proper_table_name
  520. assert_equal "table", ActiveRecord::Migrator.proper_table_name('table')
  521. assert_equal "table", ActiveRecord::Migrator.proper_table_name(:table)
  522. assert_equal "reminders", ActiveRecord::Migrator.proper_table_name(Reminder)
  523. Reminder.reset_table_name
  524. assert_equal Reminder.table_name, ActiveRecord::Migrator.proper_table_name(Reminder)
  525. # Use the model's own prefix/suffix if a model is given
  526. ActiveRecord::Base.table_name_prefix = "ARprefix_"
  527. ActiveRecord::Base.table_name_suffix = "_ARsuffix"
  528. Reminder.table_name_prefix = 'prefix_'
  529. Reminder.table_name_suffix = '_suffix'
  530. Reminder.reset_table_name
  531. assert_equal "prefix_reminders_suffix", ActiveRecord::Migrator.proper_table_name(Reminder)
  532. Reminder.table_name_prefix = ''
  533. Reminder.table_name_suffix = ''
  534. Reminder.reset_table_name
  535. # Use AR::Base's prefix/suffix if string or symbol is given
  536. ActiveRecord::Base.table_name_prefix = "prefix_"
  537. ActiveRecord::Base.table_name_suffix = "_suffix"
  538. Reminder.reset_table_name
  539. assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name('table')
  540. assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name(:table)
  541. ActiveRecord::Base.table_name_prefix = ""
  542. ActiveRecord::Base.table_name_suffix = ""
  543. Reminder.reset_table_name
  544. end
  545. def test_add_drop_table_with_prefix_and_suffix
  546. assert !Reminder.table_exists?
  547. ActiveRecord::Base.table_name_prefix = 'prefix_'
  548. ActiveRecord::Base.table_name_suffix = '_suffix'
  549. Reminder.reset_table_name
  550. Reminder.reset_sequence_name
  551. WeNeedReminders.up
  552. assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
  553. assert_equal "hello world", Reminder.find(:first).content
  554. WeNeedReminders.down
  555. assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
  556. ensure
  557. ActiveRecord::Base.table_name_prefix = ''
  558. ActiveRecord::Base.table_name_suffix = ''
  559. Reminder.reset_table_name
  560. Reminder.reset_sequence_name
  561. end
  562. # FrontBase does not support default values on BLOB/CLOB columns
  563. unless current_adapter?(:FrontBaseAdapter)
  564. def test_create_table_with_binary_column
  565. Person.connection.drop_table :binary_testings rescue nil
  566. assert_nothing_raised {
  567. Person.connection.create_table :binary_testings do |t|
  568. t.column "data", :binary, :default => "", :null => false
  569. end
  570. }
  571. columns = Person.connection.columns(:binary_testings)
  572. data_column = columns.detect { |c| c.name == "data" }
  573. if current_adapter?(:OracleAdapter)
  574. assert_equal "empty_blob()", data_column.default
  575. else
  576. assert_equal "", data_column.default
  577. end
  578. Person.connection.drop_table :binary_testings rescue nil
  579. end
  580. end
  581. def test_migrator_with_duplicates
  582. assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
  583. ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_duplicate/', nil)
  584. end
  585. end
  586. def test_migrator_with_missing_version_numbers
  587. ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 500)
  588. assert !Person.column_methods_hash.include?(:middle_name)
  589. assert_equal 4, ActiveRecord::Migrator.current_version
  590. ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2)
  591. assert !Reminder.table_exists?
  592. assert Person.column_methods_hash.include?(:last_name)
  593. assert_equal 2, ActiveRecord::Migrator.current_version
  594. end
  595. def test_create_table_with_custom_sequence_name
  596. return unless current_adapter? :OracleAdapter
  597. # table name is 29 chars, the standard sequence name will
  598. # be 33 chars and fail
  599. assert_raises(ActiveRecord::StatementInvalid) do
  600. begin
  601. Person.connection.create_table :table_with_name_thats_just_ok do |t|
  602. t.column :foo, :string, :null => false
  603. end
  604. ensure
  605. Person.connection.drop_table :table_with_name_thats_just_ok rescue nil
  606. end
  607. end
  608. # should be all good w/ a custom sequence name
  609. assert_nothing_raised do
  610. begin
  611. Person.connection.create_table :table_with_name_thats_just_ok,
  612. :sequence_name => 'suitably_short_seq' do |t|
  613. t.column :foo, :string, :null => false
  614. end
  615. Person.connection.execute("select suitably_short_seq.nextval from dual")
  616. ensure
  617. Person.connection.drop_table :table_with_name_thats_just_ok,
  618. :sequence_name => 'suitably_short_seq' rescue nil
  619. end
  620. end
  621. # confirm the custom sequence got dropped
  622. assert_raises(ActiveRecord::StatementInvalid) do
  623. Person.connection.execute("select suitably_short_seq.nextval from dual")
  624. end
  625. end
  626. end
  627. end