Skip to content

Commit

Permalink
Fixed that create database statements would always include "DEFAULT N…
Browse files Browse the repository at this point in the history
…ULL" (Nick Sieger) [#334 status:committed]

Conflicts:

	activerecord/CHANGELOG
  • Loading branch information
tarmo authored and jeremy committed Jul 15, 2008
1 parent 0826384 commit 275c3ab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*2.1.1 (next release)*

* Fixed that create database statements would always include "DEFAULT NULL" (Nick Sieger) [#334]

* change_column_default preserves the not-null constraint. #617 [Tarmo Tänav]

* Add :tokenizer option to validates_length_of to specify how to split up the attribute string. #507. [David Lowenfels] Example :
Expand Down
Expand Up @@ -257,7 +257,10 @@ def sql_type

def to_sql
column_sql = "#{base.quote_column_name(name)} #{sql_type}"
add_column_options!(column_sql, :null => null, :default => default) unless type.to_sym == :primary_key
column_options = {}
column_options[:null] = null unless null.nil?
column_options[:default] = default unless default.nil?
add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
column_sql
end
alias to_s :to_sql
Expand Down
36 changes: 36 additions & 0 deletions activerecord/test/cases/column_definition_test.rb
@@ -0,0 +1,36 @@
require "cases/helper"

class ColumnDefinitionTest < ActiveRecord::TestCase
def setup
@adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(nil)
def @adapter.native_database_types
{:string => "varchar"}
end
end

# Avoid column definitions in create table statements like:
# `title` varchar(255) DEFAULT NULL NULL
def test_should_not_include_default_clause_when_default_is_null
column = ActiveRecord::ConnectionAdapters::Column.new("title", nil, "varchar(20)")
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
@adapter, column.name, "string",
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal "title varchar(20) NULL", column_def.to_sql
end

def test_should_include_default_clause_when_default_is_present
column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)")
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
@adapter, column.name, "string",
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NULL}, column_def.to_sql
end

def test_should_specify_not_null_if_null_option_is_false
column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)", false)
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
@adapter, column.name, "string",
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
end
end

0 comments on commit 275c3ab

Please sign in to comment.