提交 7661065c 编写于 作者: A Aaron Patterson

refactoring migration test

上级 f82e244a
require 'cases/helper'
module ActiveRecord
class Migration
class ChangeSchemaTest < ActiveRecord::TestCase
attr_reader :connection, :table_name
def setup
super
@connection = ActiveRecord::Base.connection
@table_name = :testings
end
def teardown
super
connection.drop_table :testings rescue nil
ActiveRecord::Base.primary_key_prefix_type = nil
end
def test_index_exists
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :string, :limit => 100
end
connection.add_index :testings, :foo
assert connection.index_exists?(:testings, :foo)
assert !connection.index_exists?(:testings, :bar)
end
def test_index_exists_on_multiple_columns
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :string, :limit => 100
end
connection.add_index :testings, [:foo, :bar]
assert connection.index_exists?(:testings, [:foo, :bar])
end
def test_unique_index_exists
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
end
connection.add_index :testings, :foo, :unique => true
assert connection.index_exists?(:testings, :foo, :unique => true)
end
def test_named_index_exists
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
end
connection.add_index :testings, :foo, :name => "custom_index_name"
assert connection.index_exists?(:testings, :foo, :name => "custom_index_name")
end
def test_create_table_without_id
testing_table_with_only_foo_attribute do
assert_equal connection.columns(:testings).size, 1
end
end
def test_add_column_with_primary_key_attribute
testing_table_with_only_foo_attribute do
connection.add_column :testings, :id, :primary_key
assert_equal connection.columns(:testings).size, 2
end
end
def test_create_table_adds_id
connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo id), connection.columns(:testings).map(&:name).sort
end
def test_create_table_with_not_null_column
connection.create_table :testings do |t|
t.column :foo, :string, :null => false
end
assert_raises(ActiveRecord::StatementInvalid) do
connection.execute "insert into testings (foo) values (NULL)"
end
end
def test_create_table_with_defaults
# MySQL doesn't allow defaults on TEXT or BLOB columns.
mysql = current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter)
connection.create_table :testings do |t|
t.column :one, :string, :default => "hello"
t.column :two, :boolean, :default => true
t.column :three, :boolean, :default => false
t.column :four, :integer, :default => 1
t.column :five, :text, :default => "hello" unless mysql
end
columns = connection.columns(:testings)
one = columns.detect { |c| c.name == "one" }
two = columns.detect { |c| c.name == "two" }
three = columns.detect { |c| c.name == "three" }
four = columns.detect { |c| c.name == "four" }
five = columns.detect { |c| c.name == "five" } unless mysql
assert_equal "hello", one.default
assert_equal true, two.default
assert_equal false, three.default
assert_equal 1, four.default
assert_equal "hello", five.default unless mysql
end
def test_create_table_with_limits
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 255
t.column :default_int, :integer
t.column :one_int, :integer, :limit => 1
t.column :four_int, :integer, :limit => 4
t.column :eight_int, :integer, :limit => 8
t.column :eleven_int, :integer, :limit => 11
end
columns = connection.columns(:testings)
foo = columns.detect { |c| c.name == "foo" }
assert_equal 255, foo.limit
default = columns.detect { |c| c.name == "default_int" }
one = columns.detect { |c| c.name == "one_int" }
four = columns.detect { |c| c.name == "four_int" }
eight = columns.detect { |c| c.name == "eight_int" }
eleven = columns.detect { |c| c.name == "eleven_int" }
if current_adapter?(:PostgreSQLAdapter)
assert_equal 'integer', default.sql_type
assert_equal 'smallint', one.sql_type
assert_equal 'integer', four.sql_type
assert_equal 'bigint', eight.sql_type
assert_equal 'integer', eleven.sql_type
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
assert_match 'int(11)', default.sql_type
assert_match 'tinyint', one.sql_type
assert_match 'int', four.sql_type
assert_match 'bigint', eight.sql_type
assert_match 'int(11)', eleven.sql_type
elsif current_adapter?(:OracleAdapter)
assert_equal 'NUMBER(38)', default.sql_type
assert_equal 'NUMBER(1)', one.sql_type
assert_equal 'NUMBER(4)', four.sql_type
assert_equal 'NUMBER(8)', eight.sql_type
end
end
def test_create_table_with_primary_key_prefix_as_table_name_with_underscore
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo testing_id), connection.columns(:testings).map(&:name).sort
end
def test_create_table_with_primary_key_prefix_as_table_name
ActiveRecord::Base.primary_key_prefix_type = :table_name
connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo testingid), connection.columns(:testings).map(&:name).sort
end
def test_create_table_with_timestamps_should_create_datetime_columns
connection.create_table table_name do |t|
t.timestamps
end
created_columns = connection.columns(table_name)
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
assert !created_at_column.null
assert !updated_at_column.null
end
def test_create_table_with_timestamps_should_create_datetime_columns_with_options
connection.create_table table_name do |t|
t.timestamps :null => false
end
created_columns = connection.columns(table_name)
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
assert !created_at_column.null
assert !updated_at_column.null
end
def test_create_table_without_a_block
connection.create_table table_name
end
def test_add_column_not_null_without_default
# Sybase, and SQLite3 will not allow you to add a NOT NULL
# column to a table without a default value.
if current_adapter?(:SybaseAdapter, :SQLite3Adapter)
skip "not supported on #{connection.class}"
end
connection.create_table :testings do |t|
t.column :foo, :string
end
connection.add_column :testings, :bar, :string, :null => false
assert_raise(ActiveRecord::StatementInvalid) do
connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
end
end
def test_add_column_not_null_with_default
connection.create_table :testings do |t|
t.column :foo, :string
end
con = connection
connection.enable_identity_insert("testings", true) if current_adapter?(:SybaseAdapter)
connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}) values (1, 'hello')"
connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
assert_nothing_raised {connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
assert_raises(ActiveRecord::StatementInvalid) do
unless current_adapter?(:OpenBaseAdapter)
connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
else
connection.insert("INSERT INTO testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) VALUES (2, 'hello', NULL)",
"Testing Insert","id",2)
end
end
end
def test_column_positioning
testing_table_for_positioning do |conn|
assert_equal %w(first second third), conn.columns(:testings).map {|c| c.name }
end
end
def test_add_column_with_positioning
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer
assert_equal %w(first second third new_col), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer, :first => true
assert_equal %w(new_col first second third), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer, :after => :first
assert_equal %w(first new_col second third), conn.columns(:testings).map {|c| c.name }
end
end
def test_change_column_with_positioning
testing_table_for_positioning do |conn|
conn.change_column :testings, :second, :integer, :first => true
assert_equal %w(second first third), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.change_column :testings, :second, :integer, :after => :third
assert_equal %w(first third second), conn.columns(:testings).map {|c| c.name }
end
end
def test_change_column_quotes_column_names
connection.create_table :testings do |t|
t.column :select, :string
end
connection.change_column :testings, :select, :string, :limit => 10
# Oracle needs primary key value from sequence
if current_adapter?(:OracleAdapter)
connection.execute "insert into testings (id, #{connection.quote_column_name('select')}) values (testings_seq.nextval, '7 chars')"
else
connection.execute "insert into testings (#{connection.quote_column_name('select')}) values ('7 chars')"
end
end
def test_keeping_default_and_notnull_constaint_on_change
connection.create_table :testings do |t|
t.column :title, :string
end
person_klass = Class.new(ActiveRecord::Base)
person_klass.table_name = 'testings'
person_klass.connection.add_column "testings", "wealth", :integer, :null => false, :default => 99
person_klass.reset_column_information
assert_equal 99, person_klass.columns_hash["wealth"].default
assert_equal false, person_klass.columns_hash["wealth"].null
# Oracle needs primary key value from sequence
if current_adapter?(:OracleAdapter)
assert_nothing_raised {person_klass.connection.execute("insert into testings (id, title) values (testings_seq.nextval, 'tester')")}
else
assert_nothing_raised {person_klass.connection.execute("insert into testings (title) values ('tester')")}
end
# change column default to see that column doesn't lose its not null definition
person_klass.connection.change_column_default "testings", "wealth", 100
person_klass.reset_column_information
assert_equal 100, person_klass.columns_hash["wealth"].default
assert_equal false, person_klass.columns_hash["wealth"].null
# rename column to see that column doesn't lose its not null and/or default definition
person_klass.connection.rename_column "testings", "wealth", "money"
person_klass.reset_column_information
assert_nil person_klass.columns_hash["wealth"]
assert_equal 100, person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
# change column
person_klass.connection.change_column "testings", "money", :integer, :null => false, :default => 1000
person_klass.reset_column_information
assert_equal 1000, person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
# change column, make it nullable and clear default
person_klass.connection.change_column "testings", "money", :integer, :null => true, :default => nil
person_klass.reset_column_information
assert_nil person_klass.columns_hash["money"].default
assert_equal true, person_klass.columns_hash["money"].null
# change_column_null, make it not nullable and set null values to a default value
person_klass.connection.execute('UPDATE testings SET money = NULL')
person_klass.connection.change_column_null "testings", "money", false, 2000
person_klass.reset_column_information
assert_nil person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
assert_equal 2000, connection.select_values("SELECT money FROM testings").first.to_i
end
def test_column_exists
connection.create_table :testings do |t|
t.column :foo, :string
end
assert connection.column_exists?(:testings, :foo)
refute connection.column_exists?(:testings, :bar)
end
def test_column_exists_with_type
connection.create_table :testings do |t|
t.column :foo, :string
t.column :bar, :decimal, :precision => 8, :scale => 2
end
assert connection.column_exists?(:testings, :foo, :string)
refute connection.column_exists?(:testings, :foo, :integer)
assert connection.column_exists?(:testings, :bar, :decimal)
refute connection.column_exists?(:testings, :bar, :integer)
end
def test_column_exists_with_definition
connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :decimal, :precision => 8, :scale => 2
end
assert connection.column_exists?(:testings, :foo, :string, :limit => 100)
refute connection.column_exists?(:testings, :foo, :string, :limit => 50)
assert connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
refute connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
end
def test_column_exists_on_table_with_no_options_parameter_supplied
connection.create_table :testings do |t|
t.string :foo
end
connection.change_table :testings do |t|
assert t.column_exists?(:foo)
assert !(t.column_exists?(:bar))
end
end
private
def testing_table_with_only_foo_attribute
connection.create_table :testings, :id => false do |t|
t.column :foo, :string
end
yield
end
def testing_table_for_positioning
unless current_adapter?(:MysqlAdapter, :Mysql2Adapter)
skip "not supported on #{connection.class}"
end
connection.create_table :testings, :id => false do |t|
t.column :first, :integer
t.column :second, :integer
t.column :third, :integer
end
yield connection
end
end
end
end
......@@ -53,6 +53,7 @@ class MigrationTest < ActiveRecord::TestCase
fixtures :people
def setup
super
ActiveRecord::Migration.verbose = true
ActiveRecord::Migration.message_count = 0
end
......@@ -186,203 +187,6 @@ def test_double_add_index
end
end
def test_index_exists
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :string, :limit => 100
end
Person.connection.add_index :testings, :foo
assert Person.connection.index_exists?(:testings, :foo)
assert !Person.connection.index_exists?(:testings, :bar)
ensure
Person.connection.drop_table :testings rescue nil
end
def test_index_exists_on_multiple_columns
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :string, :limit => 100
end
Person.connection.add_index :testings, [:foo, :bar]
assert Person.connection.index_exists?(:testings, [:foo, :bar])
ensure
Person.connection.drop_table :testings rescue nil
end
def test_unique_index_exists
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
end
Person.connection.add_index :testings, :foo, :unique => true
assert Person.connection.index_exists?(:testings, :foo, :unique => true)
ensure
Person.connection.drop_table :testings rescue nil
end
def test_named_index_exists
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
end
Person.connection.add_index :testings, :foo, :name => "custom_index_name"
assert Person.connection.index_exists?(:testings, :foo, :name => "custom_index_name")
ensure
Person.connection.drop_table :testings rescue nil
end
def testing_table_with_only_foo_attribute
Person.connection.create_table :testings, :id => false do |t|
t.column :foo, :string
end
yield Person.connection
ensure
Person.connection.drop_table :testings rescue nil
end
protected :testing_table_with_only_foo_attribute
def test_create_table_without_id
testing_table_with_only_foo_attribute do |connection|
assert_equal connection.columns(:testings).size, 1
end
end
def test_add_column_with_primary_key_attribute
testing_table_with_only_foo_attribute do |connection|
assert_nothing_raised { connection.add_column :testings, :id, :primary_key }
assert_equal connection.columns(:testings).size, 2
end
end
def test_create_table_adds_id
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo id),
Person.connection.columns(:testings).map { |c| c.name }.sort
ensure
Person.connection.drop_table :testings rescue nil
end
def test_create_table_with_not_null_column
assert_nothing_raised do
Person.connection.create_table :testings do |t|
t.column :foo, :string, :null => false
end
end
assert_raise(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo) values (NULL)"
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_create_table_with_defaults
# MySQL doesn't allow defaults on TEXT or BLOB columns.
mysql = current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter)
Person.connection.create_table :testings do |t|
t.column :one, :string, :default => "hello"
t.column :two, :boolean, :default => true
t.column :three, :boolean, :default => false
t.column :four, :integer, :default => 1
t.column :five, :text, :default => "hello" unless mysql
end
columns = Person.connection.columns(:testings)
one = columns.detect { |c| c.name == "one" }
two = columns.detect { |c| c.name == "two" }
three = columns.detect { |c| c.name == "three" }
four = columns.detect { |c| c.name == "four" }
five = columns.detect { |c| c.name == "five" } unless mysql
assert_equal "hello", one.default
assert_equal true, two.default
assert_equal false, three.default
assert_equal 1, four.default
assert_equal "hello", five.default unless mysql
ensure
Person.connection.drop_table :testings rescue nil
end
def test_create_table_with_limits
assert_nothing_raised do
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 255
t.column :default_int, :integer
t.column :one_int, :integer, :limit => 1
t.column :four_int, :integer, :limit => 4
t.column :eight_int, :integer, :limit => 8
t.column :eleven_int, :integer, :limit => 11
end
end
columns = Person.connection.columns(:testings)
foo = columns.detect { |c| c.name == "foo" }
assert_equal 255, foo.limit
default = columns.detect { |c| c.name == "default_int" }
one = columns.detect { |c| c.name == "one_int" }
four = columns.detect { |c| c.name == "four_int" }
eight = columns.detect { |c| c.name == "eight_int" }
eleven = columns.detect { |c| c.name == "eleven_int" }
if current_adapter?(:PostgreSQLAdapter)
assert_equal 'integer', default.sql_type
assert_equal 'smallint', one.sql_type
assert_equal 'integer', four.sql_type
assert_equal 'bigint', eight.sql_type
assert_equal 'integer', eleven.sql_type
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
assert_match 'int(11)', default.sql_type
assert_match 'tinyint', one.sql_type
assert_match 'int', four.sql_type
assert_match 'bigint', eight.sql_type
assert_match 'int(11)', eleven.sql_type
elsif current_adapter?(:OracleAdapter)
assert_equal 'NUMBER(38)', default.sql_type
assert_equal 'NUMBER(1)', one.sql_type
assert_equal 'NUMBER(4)', four.sql_type
assert_equal 'NUMBER(8)', eight.sql_type
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_create_table_with_primary_key_prefix_as_table_name_with_underscore
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo testing_id), Person.connection.columns(:testings).map { |c| c.name }.sort
ensure
Person.connection.drop_table :testings rescue nil
ActiveRecord::Base.primary_key_prefix_type = nil
end
def test_create_table_with_primary_key_prefix_as_table_name
ActiveRecord::Base.primary_key_prefix_type = :table_name
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
assert_equal %w(foo testingid), Person.connection.columns(:testings).map { |c| c.name }.sort
ensure
Person.connection.drop_table :testings rescue nil
ActiveRecord::Base.primary_key_prefix_type = nil
end
def test_create_table_with_force_true_does_not_drop_nonexisting_table
if Person.connection.table_exists?(:testings2)
Person.connection.drop_table :testings2
......@@ -399,87 +203,6 @@ def test_create_table_with_force_true_does_not_drop_nonexisting_table
Person.connection.drop_table :testings2 rescue nil
end
def test_create_table_with_timestamps_should_create_datetime_columns
table_name = :testings
Person.connection.create_table table_name do |t|
t.timestamps
end
created_columns = Person.connection.columns(table_name)
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
assert !created_at_column.null
assert !updated_at_column.null
ensure
Person.connection.drop_table table_name rescue nil
end
def test_create_table_with_timestamps_should_create_datetime_columns_with_options
table_name = :testings
Person.connection.create_table table_name do |t|
t.timestamps :null => false
end
created_columns = Person.connection.columns(table_name)
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
assert !created_at_column.null
assert !updated_at_column.null
ensure
Person.connection.drop_table table_name rescue nil
end
def test_create_table_without_a_block
table_name = :testings
Person.connection.create_table table_name
ensure
Person.connection.drop_table table_name rescue nil
end
# Sybase, and SQLite3 will not allow you to add a NOT NULL
# column to a table without a default value.
unless current_adapter?(:SybaseAdapter, :SQLite3Adapter)
def test_add_column_not_null_without_default
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
Person.connection.add_column :testings, :bar, :string, :null => false
assert_raise(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
end
ensure
Person.connection.drop_table :testings rescue nil
end
end
def test_add_column_not_null_with_default
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
con = Person.connection
Person.connection.enable_identity_insert("testings", true) if current_adapter?(:SybaseAdapter)
Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}) values (1, 'hello')"
Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
assert_raise(ActiveRecord::StatementInvalid) do
unless current_adapter?(:OpenBaseAdapter)
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)"
else
Person.connection.insert("INSERT INTO testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) VALUES (2, 'hello', NULL)",
"Testing Insert","id",2)
end
end
ensure
Person.connection.drop_table :testings rescue nil
end
# We specifically do a manual INSERT here, and then test only the SELECT
# functionality. This allows us to more easily catch INSERT being broken,
# but SELECT actually working fine.
......@@ -688,53 +411,6 @@ def test_add_remove_single_field_using_symbol_arguments
assert !Person.column_methods_hash.include?(:last_name)
end
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
def testing_table_for_positioning
Person.connection.create_table :testings, :id => false do |t|
t.column :first, :integer
t.column :second, :integer
t.column :third, :integer
end
yield Person.connection
ensure
Person.connection.drop_table :testings rescue nil
end
protected :testing_table_for_positioning
def test_column_positioning
testing_table_for_positioning do |conn|
assert_equal %w(first second third), conn.columns(:testings).map {|c| c.name }
end
end
def test_add_column_with_positioning
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer
assert_equal %w(first second third new_col), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer, :first => true
assert_equal %w(new_col first second third), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.add_column :testings, :new_col, :integer, :after => :first
assert_equal %w(first new_col second third), conn.columns(:testings).map {|c| c.name }
end
end
def test_change_column_with_positioning
testing_table_for_positioning do |conn|
conn.change_column :testings, :second, :integer, :first => true
assert_equal %w(second first third), conn.columns(:testings).map {|c| c.name }
end
testing_table_for_positioning do |conn|
conn.change_column :testings, :second, :integer, :after => :third
assert_equal %w(first third second), conn.columns(:testings).map {|c| c.name }
end
end
end
def test_add_rename
Person.delete_all
......@@ -1014,134 +690,12 @@ def test_change_column_default
assert_equal "Tester", Person.new.first_name
end
def test_change_column_quotes_column_names
Person.connection.create_table :testings do |t|
t.column :select, :string
end
assert_nothing_raised { Person.connection.change_column :testings, :select, :string, :limit => 10 }
# Oracle needs primary key value from sequence
if current_adapter?(:OracleAdapter)
assert_nothing_raised { Person.connection.execute "insert into testings (id, #{Person.connection.quote_column_name('select')}) values (testings_seq.nextval, '7 chars')" }
else
assert_nothing_raised { Person.connection.execute "insert into testings (#{Person.connection.quote_column_name('select')}) values ('7 chars')" }
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_keeping_default_and_notnull_constaint_on_change
Person.connection.create_table :testings do |t|
t.column :title, :string
end
person_klass = Class.new(Person)
person_klass.table_name = 'testings'
person_klass.connection.add_column "testings", "wealth", :integer, :null => false, :default => 99
person_klass.reset_column_information
assert_equal 99, person_klass.columns_hash["wealth"].default
assert_equal false, person_klass.columns_hash["wealth"].null
# Oracle needs primary key value from sequence
if current_adapter?(:OracleAdapter)
assert_nothing_raised {person_klass.connection.execute("insert into testings (id, title) values (testings_seq.nextval, 'tester')")}
else
assert_nothing_raised {person_klass.connection.execute("insert into testings (title) values ('tester')")}
end
# change column default to see that column doesn't lose its not null definition
person_klass.connection.change_column_default "testings", "wealth", 100
person_klass.reset_column_information
assert_equal 100, person_klass.columns_hash["wealth"].default
assert_equal false, person_klass.columns_hash["wealth"].null
# rename column to see that column doesn't lose its not null and/or default definition
person_klass.connection.rename_column "testings", "wealth", "money"
person_klass.reset_column_information
assert_nil person_klass.columns_hash["wealth"]
assert_equal 100, person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
# change column
person_klass.connection.change_column "testings", "money", :integer, :null => false, :default => 1000
person_klass.reset_column_information
assert_equal 1000, person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
# change column, make it nullable and clear default
person_klass.connection.change_column "testings", "money", :integer, :null => true, :default => nil
person_klass.reset_column_information
assert_nil person_klass.columns_hash["money"].default
assert_equal true, person_klass.columns_hash["money"].null
# change_column_null, make it not nullable and set null values to a default value
person_klass.connection.execute('UPDATE testings SET money = NULL')
person_klass.connection.change_column_null "testings", "money", false, 2000
person_klass.reset_column_information
assert_nil person_klass.columns_hash["money"].default
assert_equal false, person_klass.columns_hash["money"].null
assert_equal [2000], Person.connection.select_values("SELECT money FROM testings").map { |s| s.to_i }.sort
ensure
Person.connection.drop_table :testings rescue nil
end
def test_change_column_default_to_null
Person.connection.change_column_default "people", "first_name", nil
Person.reset_column_information
assert_nil Person.new.first_name
end
def test_column_exists
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
assert Person.connection.column_exists?(:testings, :foo)
assert !Person.connection.column_exists?(:testings, :bar)
ensure
Person.connection.drop_table :testings rescue nil
end
def test_column_exists_with_type
Person.connection.create_table :testings do |t|
t.column :foo, :string
t.column :bar, :decimal, :precision => 8, :scale => 2
end
assert Person.connection.column_exists?(:testings, :foo, :string)
assert !Person.connection.column_exists?(:testings, :foo, :integer)
assert Person.connection.column_exists?(:testings, :bar, :decimal)
assert !Person.connection.column_exists?(:testings, :bar, :integer)
ensure
Person.connection.drop_table :testings rescue nil
end
def test_column_exists_with_definition
Person.connection.create_table :testings do |t|
t.column :foo, :string, :limit => 100
t.column :bar, :decimal, :precision => 8, :scale => 2
end
assert Person.connection.column_exists?(:testings, :foo, :string, :limit => 100)
assert !Person.connection.column_exists?(:testings, :foo, :string, :limit => 50)
assert Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
assert !Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
ensure
Person.connection.drop_table :testings rescue nil
end
def test_column_exists_on_table_with_no_options_parameter_supplied
Person.connection.create_table :testings do |t|
t.string :foo
end
Person.connection.change_table :testings do |t|
assert t.column_exists?(:foo)
assert !(t.column_exists?(:bar))
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_add_table
assert !Reminder.table_exists?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册