diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-20 08:54:42 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-20 08:54:42 +0000 |
commit | 19c99acfbc26c5fad02d968f37a958b1cbfa617b (patch) | |
tree | f8bbe5f6ec036d2b6fd9c0e67cc3aa86e7a3085a /activerecord/lib/active_record/connection_adapters | |
parent | 3fc4771996be40c7bd4c3128ead1b860b2d676f9 (diff) | |
download | rails-19c99acfbc26c5fad02d968f37a958b1cbfa617b.tar.gz rails-19c99acfbc26c5fad02d968f37a958b1cbfa617b.tar.bz2 rails-19c99acfbc26c5fad02d968f37a958b1cbfa617b.zip |
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. Closes #6156.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5586 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 6c75896233..574196e351 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'set' module MysqlCompat # add all_hashes method to standard mysql-c bindings or pure ruby version @@ -84,12 +85,30 @@ module ActiveRecord module ConnectionAdapters class MysqlColumn < Column #:nodoc: + TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:binary, :string, :text]) + + def initialize(name, default, sql_type = nil, null = true) + super + self.default = nil if missing_default_forged_as_empty_string? + end + private def simplified_type(field_type) return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)") return :string if field_type =~ /enum/i super end + + # MySQL misreports NOT NULL column default when none is given. + # We can't detect this for columns which may have a legitimate '' + # default (string, text, binary) but we can for others (integer, + # datetime, boolean, and the rest). + # + # Test whether the column has default '', is not null, and is not + # a type allowing default ''. + def missing_default_forged_as_empty_string? + !null && default == '' && !TYPES_ALLOWING_EMPTY_STRING_DEFAULT.include?(type) + end end # The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with |