Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b485b8a0
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,体验更适合开发者的 AI 搜索 >>
提交
b485b8a0
编写于
9月 07, 2012
作者:
K
Konstantin Shabanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Dump schema using new style hash
上级
f4154756
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
63 addition
and
61 deletion
+63
-61
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+2
-0
activerecord/lib/active_record/schema.rb
activerecord/lib/active_record/schema.rb
+4
-4
activerecord/lib/active_record/schema_dumper.rb
activerecord/lib/active_record/schema_dumper.rb
+10
-10
activerecord/test/cases/schema_dumper_test.rb
activerecord/test/cases/schema_dumper_test.rb
+44
-44
guides/source/migrations.textile
guides/source/migrations.textile
+3
-3
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
b485b8a0
## Rails 4.0.0 (unreleased) ##
*
`ActiveRecord::SchemaDumper`
use the Ruby 1.9 style hash.
*Konstantin Shabanov*
*
Map interval with precision to string datatype in PostgreSQL. Fixes #7518.
*Yves Senn*
*
Fix eagerly loading associations without primary keys. Fixes #4976.
*Kelley Reynolds*
...
...
activerecord/lib/active_record/schema.rb
浏览文件 @
b485b8a0
...
...
@@ -11,16 +11,16 @@ module ActiveRecord
#
# ActiveRecord::Schema.define do
# create_table :authors do |t|
# t.string :name,
:null =>
false
# t.string :name,
null:
false
# end
#
# add_index :authors, :name, :unique
#
# create_table :posts do |t|
# t.integer :author_id,
:null =>
false
# t.integer :author_id,
null:
false
# t.string :subject
# t.text :body
# t.boolean :private,
:default =>
false
# t.boolean :private,
default:
false
# end
#
# add_index :posts, :author_id
...
...
@@ -50,7 +50,7 @@ def define(info, &block)
# The +info+ hash is optional, and if given is used to define metadata
# about the current schema (currently, only the schema's version):
#
# ActiveRecord::Schema.define(
:version =>
20380119000001) do
# ActiveRecord::Schema.define(
version:
20380119000001) do
# ...
# end
def
self
.
define
(
info
=
{},
&
block
)
...
...
activerecord/lib/active_record/schema_dumper.rb
浏览文件 @
b485b8a0
...
...
@@ -38,7 +38,7 @@ def initialize(connection)
end
def
header
(
stream
)
define_params
=
@version
?
"
:version =>
#{
@version
}
"
:
""
define_params
=
@version
?
"
version:
#{
@version
}
"
:
""
if
stream
.
respond_to?
(
:external_encoding
)
&&
stream
.
external_encoding
stream
.
puts
"# encoding:
#{
stream
.
external_encoding
.
name
}
"
...
...
@@ -95,12 +95,12 @@ def table(table, stream)
tbl
.
print
" create_table
#{
remove_prefix_and_suffix
(
table
).
inspect
}
"
if
columns
.
detect
{
|
c
|
c
.
name
==
pk
}
if
pk
!=
'id'
tbl
.
print
%Q(,
:primary_key =>
"
#{
pk
}
")
tbl
.
print
%Q(,
primary_key:
"
#{
pk
}
")
end
else
tbl
.
print
",
:id =>
false"
tbl
.
print
",
id:
false"
end
tbl
.
print
",
:force =>
true"
tbl
.
print
",
force:
true"
tbl
.
puts
" do |t|"
# then dump all non-primary key columns
...
...
@@ -122,7 +122,7 @@ def table(table, stream)
spec
[
:scale
]
=
column
.
scale
.
inspect
if
column
.
scale
spec
[
:null
]
=
'false'
unless
column
.
null
spec
[
:default
]
=
default_string
(
column
.
default
)
if
column
.
has_default?
(
spec
.
keys
-
[
:name
,
:type
]).
each
{
|
k
|
spec
[
k
].
insert
(
0
,
"
#{
k
.
inspect
}
=>
"
)}
(
spec
.
keys
-
[
:name
,
:type
]).
each
{
|
k
|
spec
[
k
].
insert
(
0
,
"
#{
k
.
to_s
}
:
"
)}
spec
end
.
compact
...
...
@@ -187,17 +187,17 @@ def indexes(table, stream)
statement_parts
=
[
(
'add_index '
+
remove_prefix_and_suffix
(
index
.
table
).
inspect
),
index
.
columns
.
inspect
,
(
'
:name =>
'
+
index
.
name
.
inspect
),
(
'
name:
'
+
index
.
name
.
inspect
),
]
statement_parts
<<
'
:unique =>
true'
if
index
.
unique
statement_parts
<<
'
unique:
true'
if
index
.
unique
index_lengths
=
(
index
.
lengths
||
[]).
compact
statement_parts
<<
(
'
:length =>
'
+
Hash
[
index
.
columns
.
zip
(
index
.
lengths
)].
inspect
)
unless
index_lengths
.
empty?
statement_parts
<<
(
'
length:
'
+
Hash
[
index
.
columns
.
zip
(
index
.
lengths
)].
inspect
)
unless
index_lengths
.
empty?
index_orders
=
(
index
.
orders
||
{})
statement_parts
<<
(
'
:order =>
'
+
index
.
orders
.
inspect
)
unless
index_orders
.
empty?
statement_parts
<<
(
'
order:
'
+
index
.
orders
.
inspect
)
unless
index_orders
.
empty?
statement_parts
<<
(
'
:where =>
'
+
index
.
where
.
inspect
)
if
index
.
where
statement_parts
<<
(
'
where:
'
+
index
.
where
.
inspect
)
if
index
.
where
' '
+
statement_parts
.
join
(
', '
)
end
...
...
activerecord/test/cases/schema_dumper_test.rb
浏览文件 @
b485b8a0
...
...
@@ -96,7 +96,7 @@ def test_schema_dump_includes_not_null_columns
ActiveRecord
::
SchemaDumper
.
ignore_tables
=
[
/^[^r]/
]
ActiveRecord
::
SchemaDumper
.
dump
(
ActiveRecord
::
Base
.
connection
,
stream
)
output
=
stream
.
string
assert_match
%r{
:null =>
false}
,
output
assert_match
%r{
null:
false}
,
output
end
def
test_schema_dump_includes_limit_constraint_for_integer_columns
...
...
@@ -107,46 +107,46 @@ def test_schema_dump_includes_limit_constraint_for_integer_columns
output
=
stream
.
string
if
current_adapter?
(
:PostgreSQLAdapter
)
assert_match
%r{c_int_1.*
:limit =>
2}
,
output
assert_match
%r{c_int_2.*
:limit =>
2}
,
output
assert_match
%r{c_int_1.*
limit:
2}
,
output
assert_match
%r{c_int_2.*
limit:
2}
,
output
# int 3 is 4 bytes in postgresql
assert_match
%r{c_int_3.*}
,
output
assert_no_match
%r{c_int_3.*
:limit
}
,
output
assert_no_match
%r{c_int_3.*
limit:
}
,
output
assert_match
%r{c_int_4.*}
,
output
assert_no_match
%r{c_int_4.*
:limit
}
,
output
assert_no_match
%r{c_int_4.*
limit:
}
,
output
elsif
current_adapter?
(
:MysqlAdapter
)
or
current_adapter?
(
:Mysql2Adapter
)
assert_match
%r{c_int_1.*
:limit =>
1}
,
output
assert_match
%r{c_int_2.*
:limit =>
2}
,
output
assert_match
%r{c_int_3.*
:limit =>
3}
,
output
assert_match
%r{c_int_1.*
limit:
1}
,
output
assert_match
%r{c_int_2.*
limit:
2}
,
output
assert_match
%r{c_int_3.*
limit:
3}
,
output
assert_match
%r{c_int_4.*}
,
output
assert_no_match
%r{c_int_4.*:limit}
,
output
elsif
current_adapter?
(
:SQLite3Adapter
)
assert_match
%r{c_int_1.*
:limit =>
1}
,
output
assert_match
%r{c_int_2.*
:limit =>
2}
,
output
assert_match
%r{c_int_3.*
:limit =>
3}
,
output
assert_match
%r{c_int_4.*
:limit =>
4}
,
output
assert_match
%r{c_int_1.*
limit:
1}
,
output
assert_match
%r{c_int_2.*
limit:
2}
,
output
assert_match
%r{c_int_3.*
limit:
3}
,
output
assert_match
%r{c_int_4.*
limit:
4}
,
output
end
assert_match
%r{c_int_without_limit.*}
,
output
assert_no_match
%r{c_int_without_limit.*
:limit
}
,
output
assert_no_match
%r{c_int_without_limit.*
limit:
}
,
output
if
current_adapter?
(
:SQLite3Adapter
)
assert_match
%r{c_int_5.*
:limit =>
5}
,
output
assert_match
%r{c_int_6.*
:limit =>
6}
,
output
assert_match
%r{c_int_7.*
:limit =>
7}
,
output
assert_match
%r{c_int_8.*
:limit =>
8}
,
output
assert_match
%r{c_int_5.*
limit:
5}
,
output
assert_match
%r{c_int_6.*
limit:
6}
,
output
assert_match
%r{c_int_7.*
limit:
7}
,
output
assert_match
%r{c_int_8.*
limit:
8}
,
output
elsif
current_adapter?
(
:OracleAdapter
)
assert_match
%r{c_int_5.*
:limit =>
5}
,
output
assert_match
%r{c_int_6.*
:limit =>
6}
,
output
assert_match
%r{c_int_7.*
:limit =>
7}
,
output
assert_match
%r{c_int_8.*
:limit =>
8}
,
output
assert_match
%r{c_int_5.*
limit:
5}
,
output
assert_match
%r{c_int_6.*
limit:
6}
,
output
assert_match
%r{c_int_7.*
limit:
7}
,
output
assert_match
%r{c_int_8.*
limit:
8}
,
output
else
assert_match
%r{c_int_5.*
:limit =>
8}
,
output
assert_match
%r{c_int_6.*
:limit =>
8}
,
output
assert_match
%r{c_int_7.*
:limit =>
8}
,
output
assert_match
%r{c_int_8.*
:limit =>
8}
,
output
assert_match
%r{c_int_5.*
limit:
8}
,
output
assert_match
%r{c_int_6.*
limit:
8}
,
output
assert_match
%r{c_int_7.*
limit:
8}
,
output
assert_match
%r{c_int_8.*
limit:
8}
,
output
end
end
...
...
@@ -182,15 +182,15 @@ def test_schema_dump_illegal_ignored_table_value
def
test_schema_dumps_index_columns_in_right_order
index_definition
=
standard_dump
.
split
(
/\n/
).
grep
(
/add_index.*companies/
).
first
.
strip
assert_equal
'add_index "companies", ["firm_id", "type", "rating"],
:name =>
"company_index"'
,
index_definition
assert_equal
'add_index "companies", ["firm_id", "type", "rating"],
name:
"company_index"'
,
index_definition
end
def
test_schema_dumps_partial_indices
index_definition
=
standard_dump
.
split
(
/\n/
).
grep
(
/add_index.*company_partial_index/
).
first
.
strip
if
current_adapter?
(
:PostgreSQLAdapter
)
assert_equal
'add_index "companies", ["firm_id", "type"],
:name => "company_partial_index", :where =>
"(rating > 10)"'
,
index_definition
assert_equal
'add_index "companies", ["firm_id", "type"],
name: "company_partial_index", where:
"(rating > 10)"'
,
index_definition
else
assert_equal
'add_index "companies", ["firm_id", "type"],
:name =>
"company_partial_index"'
,
index_definition
assert_equal
'add_index "companies", ["firm_id", "type"],
name:
"company_partial_index"'
,
index_definition
end
end
...
...
@@ -198,25 +198,25 @@ def test_schema_dump_should_honor_nonstandard_primary_keys
output
=
standard_dump
match
=
output
.
match
(
%r{create_table "movies"(.*)do}
)
assert_not_nil
(
match
,
"nonstandardpk table not found"
)
assert_match
%r(
:primary_key =>
"movieid")
,
match
[
1
],
"non-standard primary key not preserved"
assert_match
%r(
primary_key:
"movieid")
,
match
[
1
],
"non-standard primary key not preserved"
end
if
current_adapter?
(
:MysqlAdapter
)
or
current_adapter?
(
:Mysql2Adapter
)
def
test_schema_dump_should_not_add_default_value_for_mysql_text_field
output
=
standard_dump
assert_match
%r{t.text
\s
+"body",
\s
+
:null =>
false$}
,
output
assert_match
%r{t.text
\s
+"body",
\s
+
null:
false$}
,
output
end
def
test_schema_dump_includes_length_for_mysql_blob_and_text_fields
output
=
standard_dump
assert_match
%r{t.binary
\s
+"tiny_blob",
\s
+
:limit =>
255$}
,
output
assert_match
%r{t.binary
\s
+"tiny_blob",
\s
+
limit:
255$}
,
output
assert_match
%r{t.binary
\s
+"normal_blob"$}
,
output
assert_match
%r{t.binary
\s
+"medium_blob",
\s
+
:limit =>
16777215$}
,
output
assert_match
%r{t.binary
\s
+"long_blob",
\s
+
:limit =>
2147483647$}
,
output
assert_match
%r{t.text
\s
+"tiny_text",
\s
+
:limit =>
255$}
,
output
assert_match
%r{t.binary
\s
+"medium_blob",
\s
+
limit:
16777215$}
,
output
assert_match
%r{t.binary
\s
+"long_blob",
\s
+
limit:
2147483647$}
,
output
assert_match
%r{t.text
\s
+"tiny_text",
\s
+
limit:
255$}
,
output
assert_match
%r{t.text
\s
+"normal_text"$}
,
output
assert_match
%r{t.text
\s
+"medium_text",
\s
+
:limit =>
16777215$}
,
output
assert_match
%r{t.text
\s
+"long_text",
\s
+
:limit =>
2147483647$}
,
output
assert_match
%r{t.text
\s
+"medium_text",
\s
+
limit:
16777215$}
,
output
assert_match
%r{t.text
\s
+"long_text",
\s
+
limit:
2147483647$}
,
output
end
end
...
...
@@ -225,7 +225,7 @@ def test_schema_dump_includes_decimal_options
ActiveRecord
::
SchemaDumper
.
ignore_tables
=
[
/^[^n]/
]
ActiveRecord
::
SchemaDumper
.
dump
(
ActiveRecord
::
Base
.
connection
,
stream
)
output
=
stream
.
string
assert_match
%r{
:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default =>
2.78}
,
output
assert_match
%r{
precision: 3,[[:space:]]+scale: 2,[[:space:]]+default:
2.78}
,
output
end
if
current_adapter?
(
:PostgreSQLAdapter
)
...
...
@@ -239,7 +239,7 @@ def test_schema_dump_includes_xml_shorthand_definition
def
test_schema_dump_includes_json_shorthand_definition
output
=
standard_dump
if
%r{create_table "postgresql_json_data_type"}
=~
output
assert_match
%r|t.json "json_data",
:default =>
{}|
,
output
assert_match
%r|t.json "json_data",
default:
{}|
,
output
end
end
...
...
@@ -274,7 +274,7 @@ def test_schema_dump_includes_uuid_shorthand_definition
def
test_schema_dump_includes_hstores_shorthand_definition
output
=
standard_dump
if
%r{create_table "postgresql_hstores"}
=~
output
assert_match
%r[t.hstore "hash_store",
:default =>
{}]
,
output
assert_match
%r[t.hstore "hash_store",
default:
{}]
,
output
end
end
...
...
@@ -290,9 +290,9 @@ def test_schema_dump_keeps_large_precision_integer_columns_as_decimal
output
=
standard_dump
# Oracle supports precision up to 38 and it identifies decimals with scale 0 as integers
if
current_adapter?
(
:OracleAdapter
)
assert_match
%r{t.integer
\s
+"atoms_in_universe",
\s
+
:precision => 38,
\s
+:scale =>
0}
,
output
assert_match
%r{t.integer
\s
+"atoms_in_universe",
\s
+
precision: 38,
\s
+scale:
0}
,
output
else
assert_match
%r{t.decimal
\s
+"atoms_in_universe",
\s
+
:precision => 55,
\s
+:scale =>
0}
,
output
assert_match
%r{t.decimal
\s
+"atoms_in_universe",
\s
+
precision: 55,
\s
+scale:
0}
,
output
end
end
...
...
@@ -300,13 +300,13 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
output
=
standard_dump
match
=
output
.
match
(
%r{create_table "goofy_string_id"(.*)do.*
\n
(.*)
\n
}
)
assert_not_nil
(
match
,
"goofy_string_id table not found"
)
assert_match
%r(
:id =>
false)
,
match
[
1
],
"no table id not preserved"
assert_match
%r{t.string[[:space:]]+"id",[[:space:]]+
:null =>
false$}
,
match
[
2
],
"non-primary key id column not preserved"
assert_match
%r(
id:
false)
,
match
[
1
],
"no table id not preserved"
assert_match
%r{t.string[[:space:]]+"id",[[:space:]]+
null:
false$}
,
match
[
2
],
"non-primary key id column not preserved"
end
def
test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
output
=
standard_dump
assert_match
%r{create_table "subscribers",
:id =>
false}
,
output
assert_match
%r{create_table "subscribers",
id:
false}
,
output
end
class
CreateDogMigration
<
ActiveRecord
::
Migration
...
...
guides/source/migrations.textile
浏览文件 @
b485b8a0
...
...
@@ -912,14 +912,14 @@ If +:ruby+ is selected then the schema is stored in +db/schema.rb+. If you look
at this file you'll find that it looks an awful lot like one very big migration:
<ruby>
ActiveRecord::Schema.define(
:version =>
20080906171750) do
create_table "authors",
:force =>
true do |t|
ActiveRecord::Schema.define(
version:
20080906171750) do
create_table "authors",
force:
true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products",
:force =>
true do |t|
create_table "products",
force:
true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录