Extract MySQL::Column
class to connection_adapters/mysql/column.rb
This commit is contained in:
parent
efa445c7b5
commit
83026cb15d
@ -1,4 +1,5 @@
|
||||
require 'active_record/connection_adapters/abstract_adapter'
|
||||
require 'active_record/connection_adapters/mysql/column'
|
||||
require 'active_record/connection_adapters/mysql/schema_creation'
|
||||
require 'active_record/connection_adapters/mysql/schema_definitions'
|
||||
require 'active_record/connection_adapters/mysql/schema_dumper'
|
||||
@ -19,51 +20,6 @@ def schema_creation
|
||||
MySQL::SchemaCreation.new(self)
|
||||
end
|
||||
|
||||
class Column < ConnectionAdapters::Column # :nodoc:
|
||||
delegate :strict, :extra, to: :sql_type_metadata, allow_nil: true
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
assert_valid_default(default)
|
||||
extract_default
|
||||
end
|
||||
|
||||
def extract_default
|
||||
if blob_or_text_column?
|
||||
@default = null || strict ? nil : ''
|
||||
end
|
||||
end
|
||||
|
||||
def has_default?
|
||||
return false if blob_or_text_column? # MySQL forbids defaults on blob and text columns
|
||||
super
|
||||
end
|
||||
|
||||
def blob_or_text_column?
|
||||
/\A(?:tiny|medium|long)?blob\b/ === sql_type || type == :text
|
||||
end
|
||||
|
||||
def unsigned?
|
||||
/\bunsigned\z/ === sql_type
|
||||
end
|
||||
|
||||
def case_sensitive?
|
||||
collation && !collation.match(/_ci$/)
|
||||
end
|
||||
|
||||
def auto_increment?
|
||||
extra == 'auto_increment'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_valid_default(default)
|
||||
if blob_or_text_column? && default.present?
|
||||
raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MysqlTypeMetadata < DelegateClass(SqlTypeMetadata) # :nodoc:
|
||||
attr_reader :extra, :strict
|
||||
|
||||
@ -234,7 +190,7 @@ def each_hash(result) # :nodoc:
|
||||
end
|
||||
|
||||
def new_column(field, default, sql_type_metadata = nil, null = true, default_function = nil, collation = nil) # :nodoc:
|
||||
Column.new(field, default, sql_type_metadata, null, default_function, collation)
|
||||
MySQL::Column.new(field, default, sql_type_metadata, null, default_function, collation)
|
||||
end
|
||||
|
||||
# Must return the MySQL error number from the exception, if the exception has an
|
||||
|
@ -0,0 +1,50 @@
|
||||
module ActiveRecord
|
||||
module ConnectionAdapters
|
||||
module MySQL
|
||||
class Column < ConnectionAdapters::Column # :nodoc:
|
||||
delegate :strict, :extra, to: :sql_type_metadata, allow_nil: true
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
assert_valid_default
|
||||
extract_default
|
||||
end
|
||||
|
||||
def has_default?
|
||||
return false if blob_or_text_column? # MySQL forbids defaults on blob and text columns
|
||||
super
|
||||
end
|
||||
|
||||
def blob_or_text_column?
|
||||
/\A(?:tiny|medium|long)?blob\b/ === sql_type || type == :text
|
||||
end
|
||||
|
||||
def unsigned?
|
||||
/\bunsigned\z/ === sql_type
|
||||
end
|
||||
|
||||
def case_sensitive?
|
||||
collation && collation !~ /_ci\z/
|
||||
end
|
||||
|
||||
def auto_increment?
|
||||
extra == 'auto_increment'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_default
|
||||
if blob_or_text_column?
|
||||
@default = null || strict ? nil : ''
|
||||
end
|
||||
end
|
||||
|
||||
def assert_valid_default
|
||||
if blob_or_text_column? && default.present?
|
||||
raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -41,49 +41,49 @@ def test_should_specify_not_null_if_null_option_is_false
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
def test_should_set_default_for_mysql_binary_data_types
|
||||
type = SqlTypeMetadata.new(type: :binary, sql_type: "binary(1)")
|
||||
binary_column = AbstractMysqlAdapter::Column.new("title", "a", type)
|
||||
binary_column = MySQL::Column.new("title", "a", type)
|
||||
assert_equal "a", binary_column.default
|
||||
|
||||
type = SqlTypeMetadata.new(type: :binary, sql_type: "varbinary")
|
||||
varbinary_column = AbstractMysqlAdapter::Column.new("title", "a", type)
|
||||
varbinary_column = MySQL::Column.new("title", "a", type)
|
||||
assert_equal "a", varbinary_column.default
|
||||
end
|
||||
|
||||
def test_should_be_empty_string_default_for_mysql_binary_data_types
|
||||
type = SqlTypeMetadata.new(type: :binary, sql_type: "binary(1)")
|
||||
binary_column = AbstractMysqlAdapter::Column.new("title", "", type, false)
|
||||
binary_column = MySQL::Column.new("title", "", type, false)
|
||||
assert_equal "", binary_column.default
|
||||
|
||||
type = SqlTypeMetadata.new(type: :binary, sql_type: "varbinary")
|
||||
varbinary_column = AbstractMysqlAdapter::Column.new("title", "", type, false)
|
||||
varbinary_column = MySQL::Column.new("title", "", type, false)
|
||||
assert_equal "", varbinary_column.default
|
||||
end
|
||||
|
||||
def test_should_not_set_default_for_blob_and_text_data_types
|
||||
assert_raise ArgumentError do
|
||||
AbstractMysqlAdapter::Column.new("title", "a", SqlTypeMetadata.new(sql_type: "blob"))
|
||||
MySQL::Column.new("title", "a", SqlTypeMetadata.new(sql_type: "blob"))
|
||||
end
|
||||
|
||||
text_type = AbstractMysqlAdapter::MysqlTypeMetadata.new(
|
||||
SqlTypeMetadata.new(type: :text))
|
||||
assert_raise ArgumentError do
|
||||
AbstractMysqlAdapter::Column.new("title", "Hello", text_type)
|
||||
MySQL::Column.new("title", "Hello", text_type)
|
||||
end
|
||||
|
||||
text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type)
|
||||
text_column = MySQL::Column.new("title", nil, text_type)
|
||||
assert_equal nil, text_column.default
|
||||
|
||||
not_null_text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type, false)
|
||||
not_null_text_column = MySQL::Column.new("title", nil, text_type, false)
|
||||
assert_equal "", not_null_text_column.default
|
||||
end
|
||||
|
||||
def test_has_default_should_return_false_for_blob_and_text_data_types
|
||||
binary_type = SqlTypeMetadata.new(sql_type: "blob")
|
||||
blob_column = AbstractMysqlAdapter::Column.new("title", nil, binary_type)
|
||||
blob_column = MySQL::Column.new("title", nil, binary_type)
|
||||
assert !blob_column.has_default?
|
||||
|
||||
text_type = SqlTypeMetadata.new(type: :text)
|
||||
text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type)
|
||||
text_column = MySQL::Column.new("title", nil, text_type)
|
||||
assert !text_column.has_default?
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user