Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f20b2f4e
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 搜索 >>
提交
f20b2f4e
编写于
3月 16, 2013
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
push alter table add column sql in to the schema modification visitor
上级
f84cf418
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
14 deletion
+51
-14
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
...record/connection_adapters/abstract/schema_definitions.rb
+23
-6
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+7
-3
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+21
-5
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
浏览文件 @
f20b2f4e
...
...
@@ -262,12 +262,7 @@ def references(*args)
end
alias
:belongs_to
:references
private
def
create_column_definition
(
name
,
type
)
ColumnDefinition
.
new
name
,
type
end
def
new_column_definition
(
name
,
type
,
options
)
def
new_column_definition
(
name
,
type
,
options
)
# :nodoc:
column
=
create_column_definition
name
,
type
limit
=
options
.
fetch
(
:limit
)
do
native
[
type
][
:limit
]
if
native
[
type
].
is_a?
(
Hash
)
...
...
@@ -281,6 +276,11 @@ def new_column_definition(name, type, options)
column
end
private
def
create_column_definition
(
name
,
type
)
ColumnDefinition
.
new
name
,
type
end
def
primary_key_column_name
primary_key_column
=
columns
.
detect
{
|
c
|
c
.
primary_key?
}
primary_key_column
&&
primary_key_column
.
name
...
...
@@ -291,6 +291,23 @@ def native
end
end
class
AlterTable
# :nodoc:
attr_reader
:add
def
initialize
(
td
)
@td
=
td
@add
=
nil
end
def
name
;
@td
.
name
;
end
def
add_column
(
name
,
type
,
options
)
name
=
name
.
to_s
type
=
type
.
to_sym
@add
=
@td
.
new_column_definition
(
name
,
type
,
options
)
end
end
# Represents an SQL table in an abstract way for updating a table.
# Also see TableDefinition and SchemaStatements#create_table
#
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
f20b2f4e
...
...
@@ -355,9 +355,9 @@ def drop_table(table_name, options = {})
# Adds a new column to the named table.
# See TableDefinition#column for details of the options you can use.
def
add_column
(
table_name
,
column_name
,
type
,
options
=
{})
a
dd_column_sql
=
"ALTER TABLE
#{
quote_table_name
(
table_name
)
}
ADD
#{
quote_column_name
(
column_name
)
}
#{
type_to_sql
(
type
,
options
[
:limit
],
options
[
:precision
],
options
[
:scale
])
}
"
a
dd_column_options!
(
add_column_sql
,
options
)
execute
(
add_column_sql
)
a
t
=
create_alter_table
table_name
a
t
.
add_column
(
column_name
,
type
,
options
)
execute
schema_creation
.
accept
at
end
# Removes the given columns from the table definition.
...
...
@@ -829,6 +829,10 @@ def create_table_definition(name, temporary, options)
TableDefinition
.
new
native_database_types
,
name
,
temporary
,
options
end
def
create_alter_table
(
name
)
AlterTable
.
new
create_table_definition
(
name
,
false
,
{})
end
def
update_table_definition
(
table_name
,
base
)
Table
.
new
(
table_name
,
base
)
end
...
...
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
f20b2f4e
...
...
@@ -113,14 +113,22 @@ def accept(o)
private
def
visit_AlterTable
(
o
)
sql
=
"ALTER TABLE
#{
quote_table_name
(
o
.
name
)
}
"
if
col
=
o
.
add
sql_type
=
type_to_sql
(
col
.
type
.
to_sym
,
col
.
limit
,
col
.
precision
,
col
.
scale
)
sql
<<
"ADD
#{
quote_column_name
(
col
.
name
)
}
#{
sql_type
}
"
add_column_options!
(
sql
,
column_options
(
col
))
end
sql
end
def
visit_ColumnDefinition
(
o
)
sql_type
=
type_to_sql
(
o
.
type
.
to_sym
,
o
.
limit
,
o
.
precision
,
o
.
scale
)
column_sql
=
"
#{
quote_column_name
(
o
.
name
)
}
#{
sql_type
}
"
column_options
=
{}
column_options
[
:null
]
=
o
.
null
unless
o
.
null
.
nil?
column_options
[
:default
]
=
o
.
default
unless
o
.
default
.
nil?
column_options
[
:column
]
=
o
add_column_options!
(
column_sql
,
column_options
)
unless
o
.
primary_key?
add_column_options!
(
column_sql
,
column_options
(
o
))
unless
o
.
primary_key?
column_sql
end
...
...
@@ -132,6 +140,14 @@ def visit_TableDefinition(o)
create_sql
end
def
column_options
(
o
)
column_options
=
{}
column_options
[
:null
]
=
o
.
null
unless
o
.
null
.
nil?
column_options
[
:default
]
=
o
.
default
unless
o
.
default
.
nil?
column_options
[
:column
]
=
o
column_options
end
def
quote_column_name
(
name
)
@conn
.
quote_column_name
name
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录