Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ac384820
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 搜索 >>
提交
ac384820
编写于
3月 14, 2009
作者:
J
Jason King
提交者:
Pratik Naik
3月 14, 2009
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SQLite adapters now support DDL transactions [#2080 state:resolved]
Signed-off-by:
N
Pratik Naik
<
pratiknaik@gmail.com
>
上级
5b025a1d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
68 addition
and
28 deletion
+68
-28
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
...d/lib/active_record/connection_adapters/sqlite_adapter.rb
+33
-22
activerecord/test/cases/migration_test.rb
activerecord/test/cases/migration_test.rb
+25
-1
activerecord/test/cases/transactions_test.rb
activerecord/test/cases/transactions_test.rb
+10
-5
未找到文件。
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
浏览文件 @
ac384820
...
...
@@ -72,6 +72,18 @@ def binary_to_string(value)
#
# * <tt>:database</tt> - Path to the database file.
class
SQLiteAdapter
<
AbstractAdapter
class
Version
include
Comparable
def
initialize
(
version_string
)
@version
=
version_string
.
split
(
'.'
).
map
(
&
:to_i
)
end
def
<
=>
(
version_string
)
@version
<=>
version_string
.
split
(
'.'
).
map
(
&
:to_i
)
end
end
def
initialize
(
connection
,
logger
,
config
)
super
(
connection
,
logger
)
@config
=
config
...
...
@@ -81,6 +93,10 @@ def adapter_name #:nodoc:
'SQLite'
end
def
supports_ddl_transactions?
sqlite_version
>=
'2.0.0'
end
def
supports_migrations?
#:nodoc:
true
end
...
...
@@ -88,6 +104,10 @@ def supports_migrations? #:nodoc:
def
requires_reloading?
true
end
def
supports_add_column?
sqlite_version
>=
'3.1.6'
end
def
disconnect!
super
...
...
@@ -169,7 +189,6 @@ def rollback_db_transaction #:nodoc:
catch_schema_changes
{
@connection
.
rollback
}
end
# SELECT ... FOR UPDATE is redundant since the table is locked.
def
add_lock!
(
sql
,
options
)
#:nodoc:
sql
...
...
@@ -218,14 +237,20 @@ def rename_table(name, new_name)
execute
"ALTER TABLE
#{
name
}
RENAME TO
#{
new_name
}
"
end
# See: http://www.sqlite.org/lang_altertable.html
# SQLite has an additional restriction on the ALTER TABLE statement
def
valid_alter_table_options
(
type
,
options
)
type
.
to_sym
!=
:primary_key
end
def
add_column
(
table_name
,
column_name
,
type
,
options
=
{})
#:nodoc:
if
@connection
.
respond_to?
(
:transaction_active?
)
&&
@connection
.
transaction_active?
raise
StatementInvalid
,
'Cannot add columns to a SQLite database while inside a transaction'
if
supports_add_column?
&&
valid_alter_table_options
(
type
,
options
)
super
(
table_name
,
column_name
,
type
,
options
)
else
alter_table
(
table_name
)
do
|
definition
|
definition
.
column
(
column_name
,
type
,
options
)
end
end
super
(
table_name
,
column_name
,
type
,
options
)
# See last paragraph on http://www.sqlite.org/lang_altertable.html
execute
"VACUUM"
end
def
remove_column
(
table_name
,
*
column_names
)
#:nodoc:
...
...
@@ -385,7 +410,7 @@ def catch_schema_changes
end
def
sqlite_version
@sqlite_version
||=
select_value
(
'select sqlite_version(*)'
)
@sqlite_version
||=
SQLiteAdapter
::
Version
.
new
(
select_value
(
'select sqlite_version(*)'
)
)
end
def
default_primary_key_type
...
...
@@ -398,23 +423,9 @@ def default_primary_key_type
end
class
SQLite2Adapter
<
SQLiteAdapter
# :nodoc:
def
supports_count_distinct?
#:nodoc:
false
end
def
rename_table
(
name
,
new_name
)
move_table
(
name
,
new_name
)
end
def
add_column
(
table_name
,
column_name
,
type
,
options
=
{})
#:nodoc:
if
@connection
.
respond_to?
(
:transaction_active?
)
&&
@connection
.
transaction_active?
raise
StatementInvalid
,
'Cannot add columns to a SQLite database while inside a transaction'
end
alter_table
(
table_name
)
do
|
definition
|
definition
.
column
(
column_name
,
type
,
options
)
end
end
end
class
DeprecatedSQLiteAdapter
<
SQLite2Adapter
# :nodoc:
...
...
activerecord/test/cases/migration_test.rb
浏览文件 @
ac384820
...
...
@@ -93,6 +93,30 @@ def test_add_index
end
end
def
testing_table_with_only_foo_attribute
Person
.
connection
.
create_table
:testings
,
:id
=>
false
do
|
t
|
t
.
column
:foo
,
:string
end
yield
Person
.
connection
ensure
Person
.
connection
.
drop_table
:testings
rescue
nil
end
protected
:testing_table_with_only_foo_attribute
def
test_create_table_without_id
testing_table_with_only_foo_attribute
do
|
connection
|
assert_equal
connection
.
columns
(
:testings
).
size
,
1
end
end
def
test_add_column_with_primary_key_attribute
testing_table_with_only_foo_attribute
do
|
connection
|
assert_nothing_raised
{
connection
.
add_column
:testings
,
:id
,
:primary_key
}
assert_equal
connection
.
columns
(
:testings
).
size
,
2
end
end
def
test_create_table_adds_id
Person
.
connection
.
create_table
:testings
do
|
t
|
t
.
column
:foo
,
:string
...
...
@@ -928,7 +952,7 @@ def test_migrator_double_down
assert_equal
(
0
,
ActiveRecord
::
Migrator
.
current_version
)
end
if
current_adapter?
(
:PostgreSQLAdapter
)
if
ActiveRecord
::
Base
.
connection
.
supports_ddl_transactions?
def
test_migrator_one_up_with_exception_and_rollback
assert
!
Person
.
column_methods_hash
.
include?
(
:last_name
)
...
...
activerecord/test/cases/transactions_test.rb
浏览文件 @
ac384820
...
...
@@ -349,7 +349,7 @@ def test_open_transactions_count_is_reset_to_zero_if_no_transaction_active
end
end
def
test_sqlite_add_column_in_transaction
_raises_statement_invalid
def
test_sqlite_add_column_in_transaction
return
true
unless
current_adapter?
(
:SQLite3Adapter
,
:SQLiteAdapter
)
# Test first if column creation/deletion works correctly when no
...
...
@@ -368,10 +368,15 @@ def test_sqlite_add_column_in_transaction_raises_statement_invalid
assert
!
Topic
.
column_names
.
include?
(
'stuff'
)
end
# Test now inside a transaction: add_column should raise a StatementInvalid
Topic
.
transaction
do
assert_raise
(
ActiveRecord
::
StatementInvalid
)
{
Topic
.
connection
.
add_column
(
'topics'
,
'stuff'
,
:string
)
}
raise
ActiveRecord
::
Rollback
if
Topic
.
connection
.
supports_ddl_transactions?
assert_nothing_raised
do
Topic
.
transaction
{
Topic
.
connection
.
add_column
(
'topics'
,
'stuff'
,
:string
)
}
end
else
Topic
.
transaction
do
assert_raise
(
ActiveRecord
::
StatementInvalid
)
{
Topic
.
connection
.
add_column
(
'topics'
,
'stuff'
,
:string
)
}
raise
ActiveRecord
::
Rollback
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录