Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
83026cb1
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
83026cb1
编写于
1月 11, 2016
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract `MySQL::Column` class to `connection_adapters/mysql/column.rb`
上级
efa445c7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
62 addition
and
56 deletion
+62
-56
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+2
-46
activerecord/lib/active_record/connection_adapters/mysql/column.rb
...ord/lib/active_record/connection_adapters/mysql/column.rb
+50
-0
activerecord/test/cases/column_definition_test.rb
activerecord/test/cases/column_definition_test.rb
+10
-10
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
83026cb1
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
...
...
activerecord/lib/active_record/connection_adapters/mysql/column.rb
0 → 100644
浏览文件 @
83026cb1
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
activerecord/test/cases/column_definition_test.rb
浏览文件 @
83026cb1
...
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录