1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow add_column and create_table to specify NOT NULL #1712 [emptysands@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1955 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2005-07-30 10:16:21 +00:00
parent 0ea1375d75
commit da874a4af8
4 changed files with 40 additions and 7 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Allow add_column and create_table to specify NOT NULL #1712 [emptysands@gmail.com]
* Fix create_table so that id column is implicitly added [Rick Olson]
* Default sequence names for Oracle changed to #{table_name}_seq, which is the most commonly used standard. In addition, a new method ActiveRecord::Base#set_sequence_name allows the developer to set the sequence name per model. This is a non-backwards-compatible change -- anyone using the old-style "rails_sequence" will need to either create new sequences, or set: ActiveRecord::Base.set_sequence_name = "rails_sequence" #1798

View file

@ -437,6 +437,11 @@ module ActiveRecord
column_type_sql << "(#{limit})" if limit
column_type_sql
end
def add_column_options!(sql, options)
sql << " NOT NULL" if options[:null] == false
sql << " DEFAULT '#{options[:default]}'" unless options[:default].nil?
end
protected
def log(sql, name)
@ -488,16 +493,12 @@ module ActiveRecord
"%s %s" % [message, dump]
end
end
def add_column_options!(sql, options)
sql << " DEFAULT '#{options[:default]}'" unless options[:default].nil?
end
end
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default)
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default, :null)
def to_sql
column_sql = "#{name} #{type_to_sql(type.to_sym, limit)}"
column_sql << " DEFAULT '#{default}'" if default
add_column_options!(column_sql, :null => null, :default => default)
column_sql
end
alias to_s :to_sql
@ -506,6 +507,10 @@ module ActiveRecord
def type_to_sql(name, limit)
base.type_to_sql(name, limit) rescue name
end
def add_column_options!(sql, options)
base.add_column_options!(sql, options)
end
end
class TableDefinition
@ -528,6 +533,7 @@ module ActiveRecord
column = self[name] || ColumnDefinition.new(@base, name, type)
column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym]
column.default = options[:default]
column.null = options[:null]
@columns << column unless @columns.include? column
self
end

View file

@ -239,7 +239,7 @@ module ActiveRecord
transaction do
move_table(table_name, altered_table_name,
options.merge(:temporary => true), &caller)
options.merge(:temporary => true))
move_table(altered_table_name, table_name, &caller)
end
end

View file

@ -50,6 +50,31 @@ if ActiveRecord::Base.connection.supports_migrations?
ensure
Person.connection.drop_table :testings rescue nil
end
def test_create_table_with_not_null_column
Person.connection.create_table :testings do |t|
t.column :foo, :string, :null => false
end
assert_raises(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo) values (NULL)"
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_add_column_not_null
Person.connection.create_table :testings do |t|
t.column :foo, :string
end
Person.connection.add_column :testings, :bar, :string, :null => false
assert_raises(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
end
ensure
Person.connection.drop_table :testings rescue nil
end
def test_native_types
Person.delete_all