public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Merge [5586], [5596] from trunk.

git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5599 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
jeremy (author)
Mon Nov 20 04:20:16 -0800 2006
commit  2043513f4cfaa682ede34c3b051dc66c9b2591c9
tree    941ca0869205239a88517feebf52bd6256ed6412
parent  ef6c3c4289eba6ac5bef5fafcba1c5b067bb4833
...
2
3
4
 
 
5
6
7
...
2
3
4
5
6
7
8
9
0
@@ -2,6 +2,8 @@
0
 
0
 * Quote ActiveSupport::Multibyte::Chars. #6653 [Julian Tarkhanov]
0
 
0
+* MySQL: detect when a NOT NULL column without a default value is misreported as default ''. Can't detect for string, text, and binary columns since '' is a legitimate default. #6156 [simon@redhillconsulting.com.au, obrie, Jonathan Viney, Jeremy Kemper]
0
+
0
 * validates_numericality_of uses \A \Z to ensure the entire string matches rather than ^ $ which may match one valid line of a multiline string. #5716 [Andreas Schwarz]
0
 
0
 * Oracle: automatically detect the primary key. #6594 [vesaria, Michael Schoen]
...
1
 
2
3
4
...
84
85
86
 
 
 
 
 
 
 
 
87
88
89
90
91
92
 
 
 
 
 
 
 
 
 
 
 
93
94
95
...
1
2
3
4
5
...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
0
@@ -1,4 +1,5 @@
0
 require 'active_record/connection_adapters/abstract_adapter'
0
+require 'set'
0
 
0
 module MysqlCompat
0
   # add all_hashes method to standard mysql-c bindings or pure ruby version
0
@@ -84,12 +85,31 @@ module ActiveRecord
0
 
0
   module ConnectionAdapters
0
     class MysqlColumn < Column #:nodoc:
0
+ TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:binary, :string, :text])
0
+
0
+ def initialize(name, default, sql_type = nil, null = true)
0
+ @original_default = default
0
+ super
0
+ @default = nil if missing_default_forged_as_empty_string?
0
+ end
0
+
0
       private
0
         def simplified_type(field_type)
0
           return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)")
0
           return :string if field_type =~ /enum/i
0
           super
0
         end
0
+
0
+ # MySQL misreports NOT NULL column default when none is given.
0
+ # We can't detect this for columns which may have a legitimate ''
0
+ # default (string, text, binary) but we can for others (integer,
0
+ # datetime, boolean, and the rest).
0
+ #
0
+ # Test whether the column has default '', is not null, and is not
0
+ # a type allowing default ''.
0
+ def missing_default_forged_as_empty_string?
0
+ !null && @original_default == '' && !TYPES_ALLOWING_EMPTY_STRING_DEFAULT.include?(type)
0
+ end
0
     end
0
 
0
     # The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with
...
1
2
 
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
8
...
1
2
3
4
 
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -1,8 +1,24 @@
0
 require 'abstract_unit'
0
 require 'fixtures/default'
0
+require 'fixtures/entrant'
0
 
0
-if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter)
0
- class DefaultsTest < Test::Unit::TestCase
0
+class DefaultTest < Test::Unit::TestCase
0
+ def test_nil_defaults_for_not_null_columns
0
+ column_defaults =
0
+ if current_adapter?(:MysqlAdapter)
0
+ { 'id' => nil, 'name' => '', 'course_id' => nil }
0
+ else
0
+ { 'id' => nil, 'name' => nil, 'course_id' => nil }
0
+ end
0
+
0
+ column_defaults.each do |name, default|
0
+ column = Entrant.columns_hash[name]
0
+ assert !column.null, "#{name} column should be NOT NULL"
0
+ assert_equal default, column.default, "#{name} column should be DEFAULT #{default.inspect}"
0
+ end
0
+ end
0
+
0
+ if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter)
0
     def test_default_integers
0
       default = Default.new
0
       assert_instance_of Fixnum, default.positive_integer
...
130
131
132
133
134
 
 
135
136
137
...
130
131
132
 
 
133
134
135
136
137
0
@@ -130,8 +130,8 @@ CREATE TABLE auto_id_tests (
0
 
0
 CREATE TABLE entrants (
0
   id serial,
0
- name text,
0
- course_id integer
0
+ name text not null,
0
+ course_id integer not null
0
 );
0
 
0
 CREATE TABLE colnametests (

Comments

    No one has commented yet.