Skip to content

Commit

Permalink
Allow add_column and create_table to specify NOT NULL #1712 [emptysan…
Browse files Browse the repository at this point in the history
…ds@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1955 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jamis committed Jul 30, 2005
1 parent 0ea1375 commit da874a4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -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
Expand Down
Expand Up @@ -437,6 +437,11 @@ def type_to_sql(type, limit = nil)
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)
Expand Down Expand Up @@ -488,16 +493,12 @@ def format_log_entry(message, dump = nil)
"%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
Expand All @@ -506,6 +507,10 @@ def to_sql
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
Expand All @@ -528,6 +533,7 @@ def column(name, type, options = {})
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
Expand Down
Expand Up @@ -239,7 +239,7 @@ def alter_table(table_name, options = {}) #:nodoc:

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
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/migration_test.rb
Expand Up @@ -50,6 +50,31 @@ def test_create_table_adds_id
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
Expand Down

0 comments on commit da874a4

Please sign in to comment.