Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2c845f6b
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 搜索 >>
提交
2c845f6b
编写于
10月 24, 2017
作者:
R
Rafael França
提交者:
GitHub
10月 24, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #30976 from yahonda/supports_extensions_always_true
`supports_extensions?` return always true since PostgreSQL 9.1
上级
9a55eb05
5cd6f179
变更
6
展开全部
隐藏空白更改
内联
并排
Showing
6 changed file
with
466 addition
and
488 deletion
+466
-488
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+3
-9
activerecord/test/cases/adapters/postgresql/citext_test.rb
activerecord/test/cases/adapters/postgresql/citext_test.rb
+54
-56
activerecord/test/cases/adapters/postgresql/extension_migration_test.rb
...est/cases/adapters/postgresql/extension_migration_test.rb
+0
-4
activerecord/test/cases/adapters/postgresql/hstore_test.rb
activerecord/test/cases/adapters/postgresql/hstore_test.rb
+294
-296
activerecord/test/cases/adapters/postgresql/uuid_test.rb
activerecord/test/cases/adapters/postgresql/uuid_test.rb
+92
-98
activerecord/test/cases/schema_dumper_test.rb
activerecord/test/cases/schema_dumper_test.rb
+23
-25
未找到文件。
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
2c845f6b
...
...
@@ -337,18 +337,12 @@ def disable_extension(name)
end
def
extension_enabled?
(
name
)
if
supports_extensions?
res
=
exec_query
(
"SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '
#{
name
}
' AND installed_version IS NOT NULL) as enabled"
,
"SCHEMA"
)
res
.
cast_values
.
first
end
res
=
exec_query
(
"SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '
#{
name
}
' AND installed_version IS NOT NULL) as enabled"
,
"SCHEMA"
)
res
.
cast_values
.
first
end
def
extensions
if
supports_extensions?
exec_query
(
"SELECT extname FROM pg_extension"
,
"SCHEMA"
).
cast_values
else
super
end
exec_query
(
"SELECT extname FROM pg_extension"
,
"SCHEMA"
).
cast_values
end
# Returns the configured supported identifier length supported by PostgreSQL
...
...
activerecord/test/cases/adapters/postgresql/citext_test.rb
浏览文件 @
2c845f6b
...
...
@@ -3,78 +3,76 @@
require
"cases/helper"
require
"support/schema_dumping_helper"
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
class
PostgresqlCitextTest
<
ActiveRecord
::
PostgreSQLTestCase
include
SchemaDumpingHelper
class
Citext
<
ActiveRecord
::
Base
self
.
table_name
=
"citexts"
end
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
class
PostgresqlCitextTest
<
ActiveRecord
::
PostgreSQLTestCase
include
SchemaDumpingHelper
class
Citext
<
ActiveRecord
::
Base
self
.
table_name
=
"citexts"
end
enable_extension!
(
"citext"
,
@connection
)
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
create_table
(
"citexts"
)
do
|
t
|
t
.
citext
"cival"
end
end
enable_extension!
(
"citext"
,
@connection
)
teardown
do
@connection
.
drop_table
"citexts"
,
if_exists:
true
disable_extension!
(
"citext"
,
@connection
)
@connection
.
create_table
(
"citexts"
)
do
|
t
|
t
.
citext
"cival"
end
end
def
test_citext_enabled
assert
@connection
.
extension_enabled?
(
"citext"
)
end
teardown
do
@connection
.
drop_table
"citexts"
,
if_exists:
true
disable_extension!
(
"citext"
,
@connection
)
end
def
test_column
column
=
Citext
.
columns_hash
[
"cival"
]
assert_equal
:citext
,
column
.
type
assert_equal
"citext"
,
column
.
sql_type
assert_not
column
.
array?
def
test_citext_enabled
assert
@connection
.
extension_enabled?
(
"citext"
)
end
type
=
Citext
.
type_for_attribute
(
"cival"
)
assert_not
type
.
binary?
end
def
test_column
column
=
Citext
.
columns_hash
[
"cival"
]
assert_equal
:citext
,
column
.
type
assert_equal
"citext"
,
column
.
sql_type
assert_not
column
.
array?
def
test_change_table_supports_json
@connection
.
transaction
do
@connection
.
change_table
(
"citexts"
)
do
|
t
|
t
.
citext
"username"
end
Citext
.
reset_column_information
column
=
Citext
.
columns_hash
[
"username"
]
assert_equal
:citext
,
column
.
type
type
=
Citext
.
type_for_attribute
(
"cival"
)
assert_not
type
.
binary?
end
raise
ActiveRecord
::
Rollback
# reset the schema change
def
test_change_table_supports_json
@connection
.
transaction
do
@connection
.
change_table
(
"citexts"
)
do
|
t
|
t
.
citext
"username"
end
ensure
Citext
.
reset_column_information
column
=
Citext
.
columns_hash
[
"username"
]
assert_equal
:citext
,
column
.
type
raise
ActiveRecord
::
Rollback
# reset the schema change
end
ensure
Citext
.
reset_column_information
end
def
test_write
x
=
Citext
.
new
(
cival:
"Some CI Text"
)
x
.
save!
citext
=
Citext
.
first
assert_equal
"Some CI Text"
,
citext
.
cival
def
test_write
x
=
Citext
.
new
(
cival:
"Some CI Text"
)
x
.
save!
citext
=
Citext
.
first
assert_equal
"Some CI Text"
,
citext
.
cival
citext
.
cival
=
"Some NEW CI Text"
citext
.
save!
citext
.
cival
=
"Some NEW CI Text"
citext
.
save!
assert_equal
"Some NEW CI Text"
,
citext
.
reload
.
cival
end
assert_equal
"Some NEW CI Text"
,
citext
.
reload
.
cival
end
def
test_select_case_insensitive
@connection
.
execute
"insert into citexts (cival) values('Cased Text')"
x
=
Citext
.
where
(
cival:
"cased text"
).
first
assert_equal
"Cased Text"
,
x
.
cival
end
def
test_select_case_insensitive
@connection
.
execute
"insert into citexts (cival) values('Cased Text')"
x
=
Citext
.
where
(
cival:
"cased text"
).
first
assert_equal
"Cased Text"
,
x
.
cival
end
def
test_schema_dump_with_shorthand
output
=
dump_table_schema
(
"citexts"
)
assert_match
%r[t
\.
citext "cival"]
,
output
end
def
test_schema_dump_with_shorthand
output
=
dump_table_schema
(
"citexts"
)
assert_match
%r[t
\.
citext "cival"]
,
output
end
end
activerecord/test/cases/adapters/postgresql/extension_migration_test.rb
浏览文件 @
2c845f6b
...
...
@@ -22,10 +22,6 @@ def setup
@connection
=
ActiveRecord
::
Base
.
connection
unless
@connection
.
supports_extensions?
return
skip
(
"no extension support"
)
end
@old_schema_migration_table_name
=
ActiveRecord
::
SchemaMigration
.
table_name
@old_table_name_prefix
=
ActiveRecord
::
Base
.
table_name_prefix
@old_table_name_suffix
=
ActiveRecord
::
Base
.
table_name_suffix
...
...
activerecord/test/cases/adapters/postgresql/hstore_test.rb
浏览文件 @
2c845f6b
此差异已折叠。
点击以展开。
activerecord/test/cases/adapters/postgresql/uuid_test.rb
浏览文件 @
2c845f6b
...
...
@@ -222,68 +222,66 @@ class UUID < ActiveRecord::Base
connection
.
execute
"DROP FUNCTION IF EXISTS my_uuid_generator();"
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
def
test_id_is_uuid
assert_equal
:uuid
,
UUID
.
columns_hash
[
"id"
].
type
assert
UUID
.
primary_key
end
def
test_id_is_uuid
assert_equal
:uuid
,
UUID
.
columns_hash
[
"id"
].
type
assert
UUID
.
primary_key
end
def
test_id_has_a_default
u
=
UUID
.
create
assert_not_nil
u
.
id
end
def
test_id_has_a_default
u
=
UUID
.
create
assert_not_nil
u
.
id
end
def
test_auto_create_uuid
u
=
UUID
.
create
u
.
reload
assert_not_nil
u
.
other_uuid
end
def
test_auto_create_uuid
u
=
UUID
.
create
u
.
reload
assert_not_nil
u
.
other_uuid
end
def
test_pk_and_sequence_for_uuid_primary_key
pk
,
seq
=
connection
.
pk_and_sequence_for
(
"pg_uuids"
)
assert_equal
"id"
,
pk
assert_nil
seq
end
def
test_pk_and_sequence_for_uuid_primary_key
pk
,
seq
=
connection
.
pk_and_sequence_for
(
"pg_uuids"
)
assert_equal
"id"
,
pk
assert_nil
seq
end
def
test_schema_dumper_for_uuid_primary_key
schema
=
dump_table_schema
"pg_uuids"
assert_match
(
/\bcreate_table "pg_uuids", id: :uuid, default: -> { "uuid_generate_v1\(\)" }/
,
schema
)
assert_match
(
/t\.uuid "other_uuid", default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
end
def
test_schema_dumper_for_uuid_primary_key
schema
=
dump_table_schema
"pg_uuids"
assert_match
(
/\bcreate_table "pg_uuids", id: :uuid, default: -> { "uuid_generate_v1\(\)" }/
,
schema
)
assert_match
(
/t\.uuid "other_uuid", default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
end
def
test_schema_dumper_for_uuid_primary_key_with_custom_default
schema
=
dump_table_schema
"pg_uuids_2"
assert_match
(
/\bcreate_table "pg_uuids_2", id: :uuid, default: -> { "my_uuid_generator\(\)" }/
,
schema
)
assert_match
(
/t\.uuid "other_uuid_2", default: -> { "my_uuid_generator\(\)" }/
,
schema
)
end
def
test_schema_dumper_for_uuid_primary_key_with_custom_default
schema
=
dump_table_schema
"pg_uuids_2"
assert_match
(
/\bcreate_table "pg_uuids_2", id: :uuid, default: -> { "my_uuid_generator\(\)" }/
,
schema
)
assert_match
(
/t\.uuid "other_uuid_2", default: -> { "my_uuid_generator\(\)" }/
,
schema
)
def
test_schema_dumper_for_uuid_primary_key_default
schema
=
dump_table_schema
"pg_uuids_3"
if
connection
.
supports_pgcrypto_uuid?
assert_match
(
/\bcreate_table "pg_uuids_3", id: :uuid, default: -> { "gen_random_uuid\(\)" }/
,
schema
)
else
assert_match
(
/\bcreate_table "pg_uuids_3", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
end
end
def
test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
@verbose_was
=
ActiveRecord
::
Migration
.
verbose
ActiveRecord
::
Migration
.
verbose
=
false
def
test_schema_dumper_for_uuid_primary_key_default
schema
=
dump_table_schema
"pg_uuids_3"
if
connection
.
supports_pgcrypto_uuid?
assert_match
(
/\bcreate_table "pg_uuids_3", id: :uuid, default: -> { "gen_random_uuid\(\)" }/
,
schema
)
else
assert_match
(
/\bcreate_table "pg_uuids_3", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
migration
=
Class
.
new
(
ActiveRecord
::
Migration
[
5.0
])
do
def
version
;
101
end
def
migrate
(
x
)
create_table
(
"pg_uuids_4"
,
id: :uuid
)
end
end
end
.
new
ActiveRecord
::
Migrator
.
new
(
:up
,
[
migration
]).
migrate
def
test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
@verbose_was
=
ActiveRecord
::
Migration
.
verbose
ActiveRecord
::
Migration
.
verbose
=
false
migration
=
Class
.
new
(
ActiveRecord
::
Migration
[
5.0
])
do
def
version
;
101
end
def
migrate
(
x
)
create_table
(
"pg_uuids_4"
,
id: :uuid
)
end
end
.
new
ActiveRecord
::
Migrator
.
new
(
:up
,
[
migration
]).
migrate
schema
=
dump_table_schema
"pg_uuids_4"
assert_match
(
/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
ensure
drop_table
"pg_uuids_4"
ActiveRecord
::
Migration
.
verbose
=
@verbose_was
end
schema
=
dump_table_schema
"pg_uuids_4"
assert_match
(
/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/
,
schema
)
ensure
drop_table
"pg_uuids_4"
ActiveRecord
::
Migration
.
verbose
=
@verbose_was
end
end
...
...
@@ -302,38 +300,36 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::PostgreSQLTestCase
drop_table
"pg_uuids"
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
def
test_id_allows_default_override_via_nil
col_desc
=
connection
.
execute
(
"SELECT pg_get_expr(d.adbin, d.adrelid) as default
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attname='id' AND a.attrelid = 'pg_uuids'::regclass"
).
first
assert_nil
col_desc
[
"default"
]
end
def
test_id_allows_default_override_via_nil
col_desc
=
connection
.
execute
(
"SELECT pg_get_expr(d.adbin, d.adrelid) as default
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attname='id' AND a.attrelid = 'pg_uuids'::regclass"
).
first
assert_nil
col_desc
[
"default"
]
end
def
test_schema_dumper_for_uuid_primary_key_with_default_override_via_nil
schema
=
dump_table_schema
"pg_uuids"
assert_match
(
/\bcreate_table "pg_uuids", id: :uuid, default: nil/
,
schema
)
end
def
test_schema_dumper_for_uuid_primary_key_with_default_override_via_nil
schema
=
dump_table_schema
"pg_uuids"
assert_match
(
/\bcreate_table "pg_uuids", id: :uuid, default: nil/
,
schema
)
end
def
test_schema_dumper_for_uuid_primary_key_with_default_nil_in_legacy_migration
@verbose_was
=
ActiveRecord
::
Migration
.
verbose
ActiveRecord
::
Migration
.
verbose
=
false
migration
=
Class
.
new
(
ActiveRecord
::
Migration
[
5.0
])
do
def
version
;
101
end
def
migrate
(
x
)
create_table
(
"pg_uuids_4"
,
id: :uuid
,
default:
nil
)
end
end
.
new
ActiveRecord
::
Migrator
.
new
(
:up
,
[
migration
]).
migrate
schema
=
dump_table_schema
"pg_uuids_4"
assert_match
(
/\bcreate_table "pg_uuids_4", id: :uuid, default: nil/
,
schema
)
ensure
drop_table
"pg_uuids_4"
ActiveRecord
::
Migration
.
verbose
=
@verbose_was
end
def
test_schema_dumper_for_uuid_primary_key_with_default_nil_in_legacy_migration
@verbose_was
=
ActiveRecord
::
Migration
.
verbose
ActiveRecord
::
Migration
.
verbose
=
false
migration
=
Class
.
new
(
ActiveRecord
::
Migration
[
5.0
])
do
def
version
;
101
end
def
migrate
(
x
)
create_table
(
"pg_uuids_4"
,
id: :uuid
,
default:
nil
)
end
end
.
new
ActiveRecord
::
Migrator
.
new
(
:up
,
[
migration
]).
migrate
schema
=
dump_table_schema
"pg_uuids_4"
assert_match
(
/\bcreate_table "pg_uuids_4", id: :uuid, default: nil/
,
schema
)
ensure
drop_table
"pg_uuids_4"
ActiveRecord
::
Migration
.
verbose
=
@verbose_was
end
end
...
...
@@ -367,23 +363,21 @@ class UuidComment < ActiveRecord::Base
drop_table
"pg_uuid_posts"
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
def
test_collection_association_with_uuid
post
=
UuidPost
.
create!
comment
=
post
.
uuid_comments
.
create!
assert
post
.
uuid_comments
.
find
(
comment
.
id
)
end
def
test_collection_association_with_uuid
post
=
UuidPost
.
create!
comment
=
post
.
uuid_comments
.
create!
assert
post
.
uuid_comments
.
find
(
comment
.
id
)
end
def
test_find_with_uuid
UuidPost
.
create!
assert_raise
ActiveRecord
::
RecordNotFound
do
UuidPost
.
find
(
123456
)
end
def
test_find_with_uuid
UuidPost
.
create!
assert_raise
ActiveRecord
::
RecordNotFound
do
UuidPost
.
find
(
123456
)
end
end
def
test_find_by_with_uuid
UuidPost
.
create!
assert_nil
UuidPost
.
find_by
(
id:
789
)
end
def
test_find_by_with_uuid
UuidPost
.
create!
assert_nil
UuidPost
.
find_by
(
id:
789
)
end
end
activerecord/test/cases/schema_dumper_test.rb
浏览文件 @
2c845f6b
...
...
@@ -294,34 +294,32 @@ def test_schema_dump_oid_type
assert_match
%r{t
\.
oid
\s
+"obj_id"$}
,
output
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
def
test_schema_dump_includes_extensions
connection
=
ActiveRecord
::
Base
.
connection
connection
.
stubs
(
:extensions
).
returns
([
"hstore"
])
output
=
perform_schema_dump
assert_match
"# These are extensions that must be enabled"
,
output
assert_match
%r{enable_extension "hstore"}
,
output
connection
.
stubs
(
:extensions
).
returns
([])
output
=
perform_schema_dump
assert_no_match
"# These are extensions that must be enabled"
,
output
assert_no_match
%r{enable_extension}
,
output
end
def
test_schema_dump_includes_extensions
connection
=
ActiveRecord
::
Base
.
connection
connection
.
stubs
(
:extensions
).
returns
([
"hstore"
])
output
=
perform_schema_dump
assert_match
"# These are extensions that must be enabled"
,
output
assert_match
%r{enable_extension "hstore"}
,
output
connection
.
stubs
(
:extensions
).
returns
([])
output
=
perform_schema_dump
assert_no_match
"# These are extensions that must be enabled"
,
output
assert_no_match
%r{enable_extension}
,
output
end
def
test_schema_dump_includes_extensions_in_alphabetic_order
connection
=
ActiveRecord
::
Base
.
connection
def
test_schema_dump_includes_extensions_in_alphabetic_order
connection
=
ActiveRecord
::
Base
.
connection
connection
.
stubs
(
:extensions
).
returns
([
"hstore"
,
"uuid-ossp"
,
"xml2"
])
output
=
perform_schema_dump
enabled_extensions
=
output
.
scan
(
%r{enable_extension "(.+)"}
).
flatten
assert_equal
[
"hstore"
,
"uuid-ossp"
,
"xml2"
],
enabled_extensions
connection
.
stubs
(
:extensions
).
returns
([
"hstore"
,
"uuid-ossp"
,
"xml2"
])
output
=
perform_schema_dump
enabled_extensions
=
output
.
scan
(
%r{enable_extension "(.+)"}
).
flatten
assert_equal
[
"hstore"
,
"uuid-ossp"
,
"xml2"
],
enabled_extensions
connection
.
stubs
(
:extensions
).
returns
([
"uuid-ossp"
,
"xml2"
,
"hstore"
])
output
=
perform_schema_dump
enabled_extensions
=
output
.
scan
(
%r{enable_extension "(.+)"}
).
flatten
assert_equal
[
"hstore"
,
"uuid-ossp"
,
"xml2"
],
enabled_extensions
end
connection
.
stubs
(
:extensions
).
returns
([
"uuid-ossp"
,
"xml2"
,
"hstore"
])
output
=
perform_schema_dump
enabled_extensions
=
output
.
scan
(
%r{enable_extension "(.+)"}
).
flatten
assert_equal
[
"hstore"
,
"uuid-ossp"
,
"xml2"
],
enabled_extensions
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录