| | 424 | def test_rename_column_preserves_default_and_not_null_state |
|---|
| | 425 | begin |
|---|
| | 426 | Person.connection.add_column "people", "contibutions", :integer, :default => 10, :null => false |
|---|
| | 427 | Person.reset_column_information |
|---|
| | 428 | columns = Person.connection.columns(:people) |
|---|
| | 429 | column = columns.detect { |c| c.name == "contibutions" } |
|---|
| | 430 | assert_equal 10, column.default |
|---|
| | 431 | assert !column.null |
|---|
| | 432 | |
|---|
| | 433 | Person.connection.rename_column("people", "contibutions", "contributions") |
|---|
| | 434 | Person.reset_column_information |
|---|
| | 435 | columns = Person.connection.columns(:people) |
|---|
| | 436 | column = columns.detect { |c| c.name == "contributions" } |
|---|
| | 437 | assert_equal 10, column.default |
|---|
| | 438 | assert !column.null |
|---|
| | 439 | ensure |
|---|
| | 440 | Person.connection.remove_column("people", "contibutions") rescue ActiveRecord::StatementInvalid |
|---|
| | 441 | Person.connection.remove_column("people", "contributions") rescue ActiveRecord::StatementInvalid |
|---|
| | 442 | end |
|---|
| | 443 | end |
|---|
| | 444 | |
|---|
| | 445 | if current_adapter?(:MysqlAdapter) |
|---|
| | 446 | def test_rename_column_preserves_auto_increment |
|---|
| | 447 | begin |
|---|
| | 448 | ActiveRecord::Base.connection.create_table(:legacy) {} # We just need the id column |
|---|
| | 449 | legacy = Class.new(ActiveRecord::Base) |
|---|
| | 450 | legacy.send(:define_attr_method, :table_name, "legacy") |
|---|
| | 451 | options = legacy.connection.select_one("SHOW COLUMNS FROM legacy LIKE 'id'") |
|---|
| | 452 | assert_equal 'auto_increment', options['Extra'] |
|---|
| | 453 | |
|---|
| | 454 | legacy.connection.rename_column("legacy", "id", "orig_id") |
|---|
| | 455 | options = legacy.connection.select_one("SHOW COLUMNS FROM legacy LIKE 'orig_id'") |
|---|
| | 456 | assert_equal 'auto_increment', options['Extra'] |
|---|
| | 457 | ensure |
|---|
| | 458 | ActiveRecord::Base.connection.drop_table :legacy rescue nil |
|---|
| | 459 | end |
|---|
| | 460 | end |
|---|
| | 461 | |
|---|
| | 462 | def test_rename_column_does_handle_mysql_misreport_of_not_null_column_default |
|---|
| | 463 | begin |
|---|
| | 464 | Person.connection.add_column "people", "contibutions", :integer, :null => false |
|---|
| | 465 | Person.reset_column_information |
|---|
| | 466 | columns = Person.connection.columns(:people) |
|---|
| | 467 | column = columns.detect { |c| c.name == "contibutions" } |
|---|
| | 468 | assert_equal nil, column.default |
|---|
| | 469 | assert !column.null |
|---|
| | 470 | |
|---|
| | 471 | Person.connection.rename_column("people", "contibutions", "contributions") |
|---|
| | 472 | Person.reset_column_information |
|---|
| | 473 | columns = Person.connection.columns(:people) |
|---|
| | 474 | column = columns.detect { |c| c.name == "contributions" } |
|---|
| | 475 | assert_equal nil, column.default |
|---|
| | 476 | assert !column.null |
|---|
| | 477 | ensure |
|---|
| | 478 | Person.connection.remove_column("people", "contibutions") rescue ActiveRecord::StatementInvalid |
|---|
| | 479 | Person.connection.remove_column("people", "contributions") rescue ActiveRecord::StatementInvalid |
|---|
| | 480 | end |
|---|
| | 481 | end |
|---|
| | 482 | |
|---|
| | 483 | def test_rename_column_keeps_empty_default |
|---|
| | 484 | begin |
|---|
| | 485 | Person.connection.add_column "people", "state", :string, :default => "" |
|---|
| | 486 | Person.reset_column_information |
|---|
| | 487 | columns = Person.connection.columns(:people) |
|---|
| | 488 | column = columns.detect { |c| c.name == "state" } |
|---|
| | 489 | assert_equal '', column.default |
|---|
| | 490 | assert column.null |
|---|
| | 491 | |
|---|
| | 492 | Person.connection.rename_column("people", "state", "mood") |
|---|
| | 493 | Person.reset_column_information |
|---|
| | 494 | columns = Person.connection.columns(:people) |
|---|
| | 495 | column = columns.detect { |c| c.name == "mood" } |
|---|
| | 496 | assert_equal '', column.default |
|---|
| | 497 | assert column.null |
|---|
| | 498 | ensure |
|---|
| | 499 | Person.connection.remove_column("people", "state") rescue ActiveRecord::StatementInvalid |
|---|
| | 500 | Person.connection.remove_column("people", "mood") rescue ActiveRecord::StatementInvalid |
|---|
| | 501 | end |
|---|
| | 502 | end |
|---|
| | 503 | end |
|---|
| | 504 | |
|---|