2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2008-01-18 02:30:42 -05:00
|
|
|
if ActiveRecord::Base.connection.supports_migrations?
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ActiveRecordSchemaTest < ActiveRecord::TestCase
|
2005-09-29 20:57:27 -04:00
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
2014-09-08 05:46:36 -04:00
|
|
|
setup do
|
|
|
|
@original_verbose = ActiveRecord::Migration.verbose
|
2014-09-06 12:14:08 -04:00
|
|
|
ActiveRecord::Migration.verbose = false
|
2005-09-23 09:29:33 -04:00
|
|
|
@connection = ActiveRecord::Base.connection
|
2013-01-06 15:42:08 -05:00
|
|
|
ActiveRecord::SchemaMigration.drop_table
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2005-09-23 09:29:33 -04:00
|
|
|
@connection.drop_table :fruits rescue nil
|
2013-05-11 22:32:33 -04:00
|
|
|
@connection.drop_table :nep_fruits rescue nil
|
|
|
|
@connection.drop_table :nep_schema_migrations rescue nil
|
2014-08-12 16:23:14 -04:00
|
|
|
@connection.drop_table :has_timestamps rescue nil
|
2013-01-21 15:19:14 -05:00
|
|
|
ActiveRecord::SchemaMigration.delete_all rescue nil
|
2014-09-08 05:46:36 -04:00
|
|
|
ActiveRecord::Migration.verbose = @original_verbose
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
|
|
|
|
2014-06-06 09:53:33 -04:00
|
|
|
def test_has_no_primary_key
|
|
|
|
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
|
|
|
|
assert_nil ActiveRecord::SchemaMigration.primary_key
|
|
|
|
|
|
|
|
ActiveRecord::SchemaMigration.create_table
|
|
|
|
assert_difference "ActiveRecord::SchemaMigration.count", 1 do
|
|
|
|
ActiveRecord::SchemaMigration.create version: 12
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActiveRecord::SchemaMigration.drop_table
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
|
|
|
|
end
|
|
|
|
|
2005-09-23 09:29:33 -04:00
|
|
|
def test_schema_define
|
|
|
|
ActiveRecord::Schema.define(:version => 7) do
|
|
|
|
create_table :fruits do |t|
|
|
|
|
t.column :color, :string
|
2005-10-29 14:40:49 -04:00
|
|
|
t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle
|
2005-09-23 09:29:33 -04:00
|
|
|
t.column :texture, :string
|
|
|
|
t.column :flavor, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" }
|
2008-04-09 12:20:15 -04:00
|
|
|
assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" }
|
|
|
|
assert_equal 7, ActiveRecord::Migrator::current_version
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2010-02-28 05:53:48 -05:00
|
|
|
|
2013-05-11 22:32:33 -04:00
|
|
|
def test_schema_define_w_table_name_prefix
|
|
|
|
table_name = ActiveRecord::SchemaMigration.table_name
|
2014-06-06 09:47:57 -04:00
|
|
|
old_table_name_prefix = ActiveRecord::Base.table_name_prefix
|
2013-05-11 22:32:33 -04:00
|
|
|
ActiveRecord::Base.table_name_prefix = "nep_"
|
|
|
|
ActiveRecord::SchemaMigration.table_name = "nep_#{table_name}"
|
|
|
|
ActiveRecord::Schema.define(:version => 7) do
|
|
|
|
create_table :fruits do |t|
|
|
|
|
t.column :color, :string
|
|
|
|
t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle
|
|
|
|
t.column :texture, :string
|
|
|
|
t.column :flavor, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 7, ActiveRecord::Migrator::current_version
|
|
|
|
ensure
|
2014-06-06 09:47:57 -04:00
|
|
|
ActiveRecord::Base.table_name_prefix = old_table_name_prefix
|
2013-05-11 22:32:33 -04:00
|
|
|
ActiveRecord::SchemaMigration.table_name = table_name
|
|
|
|
end
|
|
|
|
|
2010-07-16 22:19:29 -04:00
|
|
|
def test_schema_raises_an_error_for_invalid_column_type
|
2010-02-28 05:53:48 -05:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
ActiveRecord::Schema.define(:version => 8) do
|
|
|
|
create_table :vegetables do |t|
|
|
|
|
t.unknown :color
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-05-16 18:08:17 -04:00
|
|
|
|
|
|
|
def test_schema_subclass
|
|
|
|
Class.new(ActiveRecord::Schema).define(:version => 9) do
|
|
|
|
create_table :fruits
|
|
|
|
end
|
|
|
|
assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" }
|
|
|
|
end
|
2014-06-06 06:16:48 -04:00
|
|
|
|
|
|
|
def test_normalize_version
|
|
|
|
assert_equal "118", ActiveRecord::SchemaMigration.normalize_migration_number("0000118")
|
|
|
|
assert_equal "002", ActiveRecord::SchemaMigration.normalize_migration_number("2")
|
|
|
|
assert_equal "017", ActiveRecord::SchemaMigration.normalize_migration_number("0017")
|
|
|
|
assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947")
|
|
|
|
end
|
2014-08-12 16:23:14 -04:00
|
|
|
|
|
|
|
def test_timestamps_without_null_is_deprecated_on_create_table
|
|
|
|
assert_deprecated do
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
create_table :has_timestamps do |t|
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_timestamps_without_null_is_deprecated_on_change_table
|
|
|
|
assert_deprecated do
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
create_table :has_timestamps
|
|
|
|
|
|
|
|
change_table :has_timestamps do |t|
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_deprecation_warning_from_timestamps_on_create_table
|
|
|
|
assert_not_deprecated do
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
create_table :has_timestamps do |t|
|
|
|
|
t.timestamps null: true
|
|
|
|
end
|
|
|
|
|
|
|
|
drop_table :has_timestamps
|
|
|
|
|
|
|
|
create_table :has_timestamps do |t|
|
|
|
|
t.timestamps null: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_deprecation_warning_from_timestamps_on_change_table
|
|
|
|
assert_not_deprecated do
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
create_table :has_timestamps
|
|
|
|
change_table :has_timestamps do |t|
|
|
|
|
t.timestamps null: true
|
|
|
|
end
|
|
|
|
|
|
|
|
drop_table :has_timestamps
|
|
|
|
|
|
|
|
create_table :has_timestamps
|
|
|
|
change_table :has_timestamps do |t|
|
|
|
|
t.timestamps null: false, default: Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
|
|
|
end
|