提交 2c845f6b 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #30976 from yahonda/supports_extensions_always_true

`supports_extensions?` return always true since PostgreSQL 9.1
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册