aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2005-07-30 10:16:21 +0000
committerJamis Buck <jamis@37signals.com>2005-07-30 10:16:21 +0000
commitda874a4af817ebeb9421934b2a4e4e0032b1234d (patch)
tree29b3e248526352bb6697298e876579a8ee374cb5 /activerecord
parent0ea1375d7526cd1bbd74947f292d13b83a7617e6 (diff)
downloadrails-da874a4af817ebeb9421934b2a4e4e0032b1234d.tar.gz
rails-da874a4af817ebeb9421934b2a4e4e0032b1234d.tar.bz2
rails-da874a4af817ebeb9421934b2a4e4e0032b1234d.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb18
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb2
-rw-r--r--activerecord/test/migration_test.rb25
4 files changed, 40 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index b76c107cfe..83dddd1f4e 100644
--- a/activerecord/CHANGELOG
+++ b/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
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 532d582565..f5aa09c1f2 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -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
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 5e3ef53c82..af2de4f706 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -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
diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb
index 60bdf90bf4..f98986cdfa 100644
--- a/activerecord/test/migration_test.rb
+++ b/activerecord/test/migration_test.rb
@@ -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