Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
594c7dc2
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,发现更多精彩内容 >>
提交
594c7dc2
编写于
2月 22, 2017
作者:
R
Rafael França
提交者:
GitHub
2月 22, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23553 from kamipo/correctly_dump_native_timestamp_types_for_mysql
Correctly dump native timestamp types for MySQL
上级
45468b87
50552633
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
80 addition
and
5 deletion
+80
-5
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+33
-0
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+1
-1
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+2
-1
activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
...ctive_record/connection_adapters/mysql/schema_creation.rb
+8
-0
activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
...ve_record/connection_adapters/mysql/schema_definitions.rb
+5
-0
activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb
.../active_record/connection_adapters/mysql/schema_dumper.rb
+5
-2
activerecord/test/cases/defaults_test.rb
activerecord/test/cases/defaults_test.rb
+11
-1
activerecord/test/cases/migration/change_schema_test.rb
activerecord/test/cases/migration/change_schema_test.rb
+2
-0
activerecord/test/cases/primary_keys_test.rb
activerecord/test/cases/primary_keys_test.rb
+8
-0
activerecord/test/schema/mysql2_specific_schema.rb
activerecord/test/schema/mysql2_specific_schema.rb
+5
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
594c7dc2
*
Correctly dump native timestamp types for MySQL.
The native timestamp type in MySQL is different from datetime type.
Internal representation of the timestamp type is UNIX time, This means
that timestamp columns are affected by time zone.
> SET time_zone = '+00:00';
Query OK, 0 rows affected (0.00 sec)
> INSERT INTO time_with_zone(ts,dt) VALUES (NOW(),NOW());
Query OK, 1 row affected (0.02 sec)
> SELECT * FROM time_with_zone;
+---------------------+---------------------+
| ts | dt |
+---------------------+---------------------+
| 2016-02-07 22:11:44 | 2016-02-07 22:11:44 |
+---------------------+---------------------+
1 row in set (0.00 sec)
> SET time_zone = '-08:00';
Query OK, 0 rows affected (0.00 sec)
> SELECT * FROM time_with_zone;
+---------------------+---------------------+
| ts | dt |
+---------------------+---------------------+
| 2016-02-07 14:11:44 | 2016-02-07 22:11:44 |
+---------------------+---------------------+
1 row in set (0.00 sec)
*Ryuta Kamizono*
*
All integer-like PKs are autoincrement unless they have an explicit default.
*Matthew Draper*
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
594c7dc2
...
...
@@ -1071,7 +1071,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **) # :nodoc:
raise
ArgumentError
,
"Error adding decimal column: precision cannot be empty if scale is specified"
end
elsif
[
:datetime
,
:time
,
:interval
].
include?
(
type
)
&&
precision
||=
native
[
:precision
]
elsif
[
:datetime
,
:time
stamp
,
:time
,
:interval
].
include?
(
type
)
&&
precision
||=
native
[
:precision
]
if
(
0
..
6
)
===
precision
column_type_sql
<<
"(
#{
precision
}
)"
else
...
...
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
594c7dc2
...
...
@@ -46,6 +46,7 @@ def arel_visitor # :nodoc:
float:
{
name:
"float"
},
decimal:
{
name:
"decimal"
},
datetime:
{
name:
"datetime"
},
timestamp:
{
name:
"timestamp"
},
time:
{
name:
"time"
},
date:
{
name:
"date"
},
binary:
{
name:
"blob"
,
limit:
65535
},
...
...
@@ -708,7 +709,7 @@ def register_integer_type(mapping, key, options)
end
def
extract_precision
(
sql_type
)
if
/
time
/
.
match?
(
sql_type
)
if
/
\A(?:date)?time(?:stamp)?\b
/
.
match?
(
sql_type
)
super
||
0
else
super
...
...
activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
浏览文件 @
594c7dc2
...
...
@@ -25,6 +25,14 @@ def add_table_options!(create_sql, options)
end
def
add_column_options!
(
sql
,
options
)
# By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values,
# and assigning NULL assigns the current timestamp. To permit a TIMESTAMP
# column to contain NULL, explicitly declare it with the NULL attribute.
# See http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
if
/\Atimestamp\b/
.
match?
(
options
[
:column
].
sql_type
)
&&
!
options
[
:primary_key
]
sql
<<
" NULL"
unless
options
[
:null
]
==
false
||
options_include_default?
(
options
)
end
if
charset
=
options
[
:charset
]
sql
<<
" CHARACTER SET
#{
charset
}
"
end
...
...
activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
浏览文件 @
594c7dc2
...
...
@@ -75,6 +75,11 @@ def new_column_definition(name, type, **options) # :nodoc:
super
end
private
def
aliased_types
(
name
,
fallback
)
fallback
end
end
class
Table
<
ActiveRecord
::
ConnectionAdapters
::
Table
...
...
activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb
浏览文件 @
594c7dc2
...
...
@@ -30,7 +30,10 @@ def explicit_primary_key_default?(column)
end
def
schema_type
(
column
)
if
column
.
sql_type
==
"tinyblob"
case
column
.
sql_type
when
/\Atimestamp\b/
:timestamp
when
"tinyblob"
:blob
else
super
...
...
@@ -38,7 +41,7 @@ def schema_type(column)
end
def
schema_precision
(
column
)
super
unless
/
time
/
.
match?
(
column
.
sql_type
)
&&
column
.
precision
==
0
super
unless
/
\A(?:date)?time(?:stamp)?\b
/
.
match?
(
column
.
sql_type
)
&&
column
.
precision
==
0
end
def
schema_collation
(
column
)
...
...
activerecord/test/cases/defaults_test.rb
浏览文件 @
594c7dc2
...
...
@@ -100,11 +100,21 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase
include
SchemaDumpingHelper
if
ActiveRecord
::
Base
.
connection
.
version
>=
"5.6.0"
test
"schema dump includes default expression"
do
test
"schema dump
datetime
includes default expression"
do
output
=
dump_table_schema
(
"datetime_defaults"
)
assert_match
%r/t
\.
datetime
\s
+"modified_datetime",
\s
+default: -> { "CURRENT_TIMESTAMP" }/
,
output
end
end
test
"schema dump timestamp includes default expression"
do
output
=
dump_table_schema
(
"timestamp_defaults"
)
assert_match
%r/t
\.
timestamp
\s
+"modified_timestamp",
\s
+default: -> { "CURRENT_TIMESTAMP" }/
,
output
end
test
"schema dump timestamp without default expression"
do
output
=
dump_table_schema
(
"timestamp_defaults"
)
assert_match
%r/t
\.
timestamp
\s
+"nullable_timestamp"$/
,
output
end
end
class
DefaultsTestWithoutTransactionalFixtures
<
ActiveRecord
::
TestCase
...
...
activerecord/test/cases/migration/change_schema_test.rb
浏览文件 @
594c7dc2
...
...
@@ -269,6 +269,8 @@ def test_add_column_with_timestamp_type
if
current_adapter?
(
:PostgreSQLAdapter
)
assert_equal
"timestamp without time zone"
,
klass
.
columns_hash
[
"foo"
].
sql_type
elsif
current_adapter?
(
:Mysql2Adapter
)
assert_equal
"timestamp"
,
klass
.
columns_hash
[
"foo"
].
sql_type
else
assert_equal
klass
.
connection
.
type_to_sql
(
"datetime"
),
klass
.
columns_hash
[
"foo"
].
sql_type
end
...
...
activerecord/test/cases/primary_keys_test.rb
浏览文件 @
594c7dc2
...
...
@@ -291,6 +291,14 @@ def test_any_type_primary_key
schema
=
dump_table_schema
"barcodes"
assert_match
%r{create_table "barcodes", primary_key: "code", id: :string, limit: 42}
,
schema
end
if
current_adapter?
(
:Mysql2Adapter
)
&&
subsecond_precision_supported?
test
"schema typed primary key column"
do
@connection
.
create_table
(
:scheduled_logs
,
id: :timestamp
,
precision:
6
,
force:
true
)
schema
=
dump_table_schema
(
"scheduled_logs"
)
assert_match
%r/create_table "scheduled_logs", id: :timestamp, precision: 6/
,
schema
end
end
end
class
CompositePrimaryKeyTest
<
ActiveRecord
::
TestCase
...
...
activerecord/test/schema/mysql2_specific_schema.rb
浏览文件 @
594c7dc2
...
...
@@ -6,6 +6,11 @@
end
end
create_table
:timestamp_defaults
,
force:
true
do
|
t
|
t
.
timestamp
:nullable_timestamp
t
.
timestamp
:modified_timestamp
,
default:
->
{
"CURRENT_TIMESTAMP"
}
end
create_table
:binary_fields
,
force:
true
do
|
t
|
t
.
binary
:var_binary
,
limit:
255
t
.
binary
:var_binary_large
,
limit:
4095
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录