|
1 | 1 | require "cases/helper"
|
2 | 2 |
|
3 |
| -if ActiveRecord::Base.connection.supports_migrations? |
| 3 | +class ActiveRecordSchemaTest < ActiveRecord::TestCase |
| 4 | + self.use_transactional_tests = false |
| 5 | + |
| 6 | + setup do |
| 7 | + @original_verbose = ActiveRecord::Migration.verbose |
| 8 | + ActiveRecord::Migration.verbose = false |
| 9 | + @connection = ActiveRecord::Base.connection |
| 10 | + ActiveRecord::SchemaMigration.drop_table |
| 11 | + end |
4 | 12 |
|
5 |
| - class ActiveRecordSchemaTest < ActiveRecord::TestCase |
6 |
| - self.use_transactional_tests = false |
| 13 | + teardown do |
| 14 | + @connection.drop_table :fruits rescue nil |
| 15 | + @connection.drop_table :nep_fruits rescue nil |
| 16 | + @connection.drop_table :nep_schema_migrations rescue nil |
| 17 | + @connection.drop_table :has_timestamps rescue nil |
| 18 | + @connection.drop_table :multiple_indexes rescue nil |
| 19 | + ActiveRecord::SchemaMigration.delete_all rescue nil |
| 20 | + ActiveRecord::Migration.verbose = @original_verbose |
| 21 | + end |
7 | 22 |
|
8 |
| - setup do |
9 |
| - @original_verbose = ActiveRecord::Migration.verbose |
10 |
| - ActiveRecord::Migration.verbose = false |
11 |
| - @connection = ActiveRecord::Base.connection |
12 |
| - ActiveRecord::SchemaMigration.drop_table |
13 |
| - end |
| 23 | + def test_has_primary_key |
| 24 | + old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type |
| 25 | + ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore |
| 26 | + assert_equal "version", ActiveRecord::SchemaMigration.primary_key |
14 | 27 |
|
15 |
| - teardown do |
16 |
| - @connection.drop_table :fruits rescue nil |
17 |
| - @connection.drop_table :nep_fruits rescue nil |
18 |
| - @connection.drop_table :nep_schema_migrations rescue nil |
19 |
| - @connection.drop_table :has_timestamps rescue nil |
20 |
| - @connection.drop_table :multiple_indexes rescue nil |
21 |
| - ActiveRecord::SchemaMigration.delete_all rescue nil |
22 |
| - ActiveRecord::Migration.verbose = @original_verbose |
| 28 | + ActiveRecord::SchemaMigration.create_table |
| 29 | + assert_difference "ActiveRecord::SchemaMigration.count", 1 do |
| 30 | + ActiveRecord::SchemaMigration.create version: 12 |
23 | 31 | end
|
| 32 | + ensure |
| 33 | + ActiveRecord::SchemaMigration.drop_table |
| 34 | + ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type |
| 35 | + end |
24 | 36 |
|
25 |
| - def test_has_primary_key |
26 |
| - old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type |
27 |
| - ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore |
28 |
| - assert_equal "version", ActiveRecord::SchemaMigration.primary_key |
29 |
| - |
30 |
| - ActiveRecord::SchemaMigration.create_table |
31 |
| - assert_difference "ActiveRecord::SchemaMigration.count", 1 do |
32 |
| - ActiveRecord::SchemaMigration.create version: 12 |
| 37 | + def test_schema_define |
| 38 | + ActiveRecord::Schema.define(version: 7) do |
| 39 | + create_table :fruits do |t| |
| 40 | + t.column :color, :string |
| 41 | + t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle |
| 42 | + t.column :texture, :string |
| 43 | + t.column :flavor, :string |
33 | 44 | end
|
34 |
| - ensure |
35 |
| - ActiveRecord::SchemaMigration.drop_table |
36 |
| - ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type |
37 | 45 | end
|
38 | 46 |
|
39 |
| - def test_schema_define |
40 |
| - ActiveRecord::Schema.define(version: 7) do |
41 |
| - create_table :fruits do |t| |
42 |
| - t.column :color, :string |
43 |
| - t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle |
44 |
| - t.column :texture, :string |
45 |
| - t.column :flavor, :string |
46 |
| - end |
47 |
| - end |
48 |
| - |
49 |
| - assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } |
50 |
| - assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" } |
51 |
| - assert_equal 7, ActiveRecord::Migrator::current_version |
52 |
| - end |
| 47 | + assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } |
| 48 | + assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" } |
| 49 | + assert_equal 7, ActiveRecord::Migrator::current_version |
| 50 | + end |
53 | 51 |
|
54 |
| - def test_schema_define_w_table_name_prefix |
55 |
| - table_name = ActiveRecord::SchemaMigration.table_name |
56 |
| - old_table_name_prefix = ActiveRecord::Base.table_name_prefix |
57 |
| - ActiveRecord::Base.table_name_prefix = "nep_" |
58 |
| - ActiveRecord::SchemaMigration.table_name = "nep_#{table_name}" |
59 |
| - ActiveRecord::Schema.define(version: 7) do |
60 |
| - create_table :fruits do |t| |
61 |
| - t.column :color, :string |
62 |
| - t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle |
63 |
| - t.column :texture, :string |
64 |
| - t.column :flavor, :string |
65 |
| - end |
| 52 | + def test_schema_define_w_table_name_prefix |
| 53 | + table_name = ActiveRecord::SchemaMigration.table_name |
| 54 | + old_table_name_prefix = ActiveRecord::Base.table_name_prefix |
| 55 | + ActiveRecord::Base.table_name_prefix = "nep_" |
| 56 | + ActiveRecord::SchemaMigration.table_name = "nep_#{table_name}" |
| 57 | + ActiveRecord::Schema.define(version: 7) do |
| 58 | + create_table :fruits do |t| |
| 59 | + t.column :color, :string |
| 60 | + t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle |
| 61 | + t.column :texture, :string |
| 62 | + t.column :flavor, :string |
66 | 63 | end
|
67 |
| - assert_equal 7, ActiveRecord::Migrator::current_version |
68 |
| - ensure |
69 |
| - ActiveRecord::Base.table_name_prefix = old_table_name_prefix |
70 |
| - ActiveRecord::SchemaMigration.table_name = table_name |
71 | 64 | end
|
| 65 | + assert_equal 7, ActiveRecord::Migrator::current_version |
| 66 | + ensure |
| 67 | + ActiveRecord::Base.table_name_prefix = old_table_name_prefix |
| 68 | + ActiveRecord::SchemaMigration.table_name = table_name |
| 69 | + end |
72 | 70 |
|
73 |
| - def test_schema_raises_an_error_for_invalid_column_type |
74 |
| - assert_raise NoMethodError do |
75 |
| - ActiveRecord::Schema.define(version: 8) do |
76 |
| - create_table :vegetables do |t| |
77 |
| - t.unknown :color |
78 |
| - end |
| 71 | + def test_schema_raises_an_error_for_invalid_column_type |
| 72 | + assert_raise NoMethodError do |
| 73 | + ActiveRecord::Schema.define(version: 8) do |
| 74 | + create_table :vegetables do |t| |
| 75 | + t.unknown :color |
79 | 76 | end
|
80 | 77 | end
|
81 | 78 | end
|
| 79 | + end |
82 | 80 |
|
83 |
| - def test_schema_subclass |
84 |
| - Class.new(ActiveRecord::Schema).define(version: 9) do |
85 |
| - create_table :fruits |
86 |
| - end |
87 |
| - assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } |
| 81 | + def test_schema_subclass |
| 82 | + Class.new(ActiveRecord::Schema).define(version: 9) do |
| 83 | + create_table :fruits |
88 | 84 | end
|
| 85 | + assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } |
| 86 | + end |
89 | 87 |
|
90 |
| - def test_normalize_version |
91 |
| - assert_equal "118", ActiveRecord::SchemaMigration.normalize_migration_number("0000118") |
92 |
| - assert_equal "002", ActiveRecord::SchemaMigration.normalize_migration_number("2") |
93 |
| - assert_equal "017", ActiveRecord::SchemaMigration.normalize_migration_number("0017") |
94 |
| - assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947") |
95 |
| - end |
| 88 | + def test_normalize_version |
| 89 | + assert_equal "118", ActiveRecord::SchemaMigration.normalize_migration_number("0000118") |
| 90 | + assert_equal "002", ActiveRecord::SchemaMigration.normalize_migration_number("2") |
| 91 | + assert_equal "017", ActiveRecord::SchemaMigration.normalize_migration_number("0017") |
| 92 | + assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947") |
| 93 | + end |
96 | 94 |
|
97 |
| - def test_schema_load_with_multiple_indexes_for_column_of_different_names |
98 |
| - ActiveRecord::Schema.define do |
99 |
| - create_table :multiple_indexes do |t| |
100 |
| - t.string "foo" |
101 |
| - t.index ["foo"], name: "multiple_indexes_foo_1" |
102 |
| - t.index ["foo"], name: "multiple_indexes_foo_2" |
103 |
| - end |
| 95 | + def test_schema_load_with_multiple_indexes_for_column_of_different_names |
| 96 | + ActiveRecord::Schema.define do |
| 97 | + create_table :multiple_indexes do |t| |
| 98 | + t.string "foo" |
| 99 | + t.index ["foo"], name: "multiple_indexes_foo_1" |
| 100 | + t.index ["foo"], name: "multiple_indexes_foo_2" |
104 | 101 | end
|
| 102 | + end |
105 | 103 |
|
106 |
| - indexes = @connection.indexes("multiple_indexes") |
| 104 | + indexes = @connection.indexes("multiple_indexes") |
107 | 105 |
|
108 |
| - assert_equal 2, indexes.length |
109 |
| - assert_equal ["multiple_indexes_foo_1", "multiple_indexes_foo_2"], indexes.collect(&:name).sort |
110 |
| - end |
| 106 | + assert_equal 2, indexes.length |
| 107 | + assert_equal ["multiple_indexes_foo_1", "multiple_indexes_foo_2"], indexes.collect(&:name).sort |
| 108 | + end |
111 | 109 |
|
112 |
| - def test_timestamps_without_null_set_null_to_false_on_create_table |
113 |
| - ActiveRecord::Schema.define do |
114 |
| - create_table :has_timestamps do |t| |
115 |
| - t.timestamps |
116 |
| - end |
| 110 | + def test_timestamps_without_null_set_null_to_false_on_create_table |
| 111 | + ActiveRecord::Schema.define do |
| 112 | + create_table :has_timestamps do |t| |
| 113 | + t.timestamps |
117 | 114 | end
|
118 |
| - |
119 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
120 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
121 | 115 | end
|
122 | 116 |
|
123 |
| - def test_timestamps_without_null_set_null_to_false_on_change_table |
124 |
| - ActiveRecord::Schema.define do |
125 |
| - create_table :has_timestamps |
| 117 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
| 118 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
| 119 | + end |
126 | 120 |
|
127 |
| - change_table :has_timestamps do |t| |
128 |
| - t.timestamps default: Time.now |
129 |
| - end |
130 |
| - end |
| 121 | + def test_timestamps_without_null_set_null_to_false_on_change_table |
| 122 | + ActiveRecord::Schema.define do |
| 123 | + create_table :has_timestamps |
131 | 124 |
|
132 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
133 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
| 125 | + change_table :has_timestamps do |t| |
| 126 | + t.timestamps default: Time.now |
| 127 | + end |
134 | 128 | end
|
135 | 129 |
|
136 |
| - def test_timestamps_without_null_set_null_to_false_on_add_timestamps |
137 |
| - ActiveRecord::Schema.define do |
138 |
| - create_table :has_timestamps |
139 |
| - add_timestamps :has_timestamps, default: Time.now |
140 |
| - end |
| 130 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
| 131 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
| 132 | + end |
141 | 133 |
|
142 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
143 |
| - assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
| 134 | + def test_timestamps_without_null_set_null_to_false_on_add_timestamps |
| 135 | + ActiveRecord::Schema.define do |
| 136 | + create_table :has_timestamps |
| 137 | + add_timestamps :has_timestamps, default: Time.now |
144 | 138 | end
|
| 139 | + |
| 140 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "created_at" }.null |
| 141 | + assert !@connection.columns(:has_timestamps).find { |c| c.name == "updated_at" }.null |
145 | 142 | end
|
146 | 143 | end
|
0 commit comments