提交 0ab9301f 编写于 作者: A Abdelkader Boudih

Added enable_extension! to helper

上级 7025d776
......@@ -12,12 +12,7 @@ class PgArray < ActiveRecord::Base
def setup
@connection = ActiveRecord::Base.connection
unless @connection.extension_enabled?('hstore')
@connection.enable_extension 'hstore'
@connection.commit_db_transaction
end
@connection.reconnect!
enable_extension!('hstore', @connection)
@connection.transaction do
@connection.create_table('pg_arrays') do |t|
......@@ -32,6 +27,7 @@ def setup
teardown do
@connection.execute 'drop table if exists pg_arrays'
disable_extension!('hstore', @connection)
end
def test_column
......
......@@ -10,12 +10,7 @@ class Citext < ActiveRecord::Base
def setup
@connection = ActiveRecord::Base.connection
unless @connection.extension_enabled?('citext')
@connection.enable_extension 'citext'
@connection.commit_db_transaction
end
@connection.reconnect!
enable_extension!('citext', @connection)
@connection.create_table('citexts') do |t|
t.citext 'cival'
......@@ -24,7 +19,7 @@ def setup
teardown do
@connection.execute 'DROP TABLE IF EXISTS citexts;'
@connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;'
disable_extension!('citext', @connection)
end
def test_citext_enabled
......
......@@ -9,9 +9,7 @@ class Ltree < ActiveRecord::Base
def setup
@connection = ActiveRecord::Base.connection
unless @connection.extension_enabled?('ltree')
@connection.enable_extension 'ltree'
end
enable_extension!('ltree', @connection)
@connection.transaction do
@connection.create_table('ltrees') do |t|
......
......@@ -118,7 +118,7 @@ class UUID < ActiveRecord::Base
end
setup do
enable_uuid_ossp!(connection)
enable_extension!('uuid-ossp', connection)
connection.create_table('pg_uuids', id: :uuid, default: 'uuid_generate_v1()') do |t|
t.string 'name'
......@@ -144,6 +144,7 @@ class UUID < ActiveRecord::Base
drop_table "pg_uuids"
drop_table 'pg_uuids_2'
connection.execute 'DROP FUNCTION IF EXISTS my_uuid_generator();'
disable_extension!('uuid-ossp', connection)
end
if ActiveRecord::Base.connection.supports_extensions?
......@@ -189,7 +190,7 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::TestCase
include PostgresqlUUIDHelper
setup do
enable_uuid_ossp!(connection)
enable_extension!('uuid-ossp', connection)
connection.create_table('pg_uuids', id: false) do |t|
t.primary_key :id, :uuid, default: nil
......@@ -199,6 +200,7 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::TestCase
teardown do
drop_table "pg_uuids"
disable_extension!('uuid-ossp', connection)
end
if ActiveRecord::Base.connection.supports_extensions?
......@@ -226,7 +228,7 @@ class UuidComment < ActiveRecord::Base
end
setup do
enable_uuid_ossp!(connection)
enable_extension!('uuid-ossp', connection)
connection.transaction do
connection.create_table('pg_uuid_posts', id: :uuid) do |t|
......@@ -240,10 +242,9 @@ class UuidComment < ActiveRecord::Base
end
teardown do
connection.transaction do
drop_table "pg_uuid_comments"
drop_table "pg_uuid_posts"
end
disable_extension!('uuid-ossp', connection)
end
if ActiveRecord::Base.connection.supports_extensions?
......
......@@ -119,15 +119,23 @@ def verify_default_timezone_config
end
end
def enable_uuid_ossp!(connection)
def enable_extension!(extension, connection)
return false unless connection.supports_extensions?
return connection.reconnect! if connection.extension_enabled?('uuid-ossp')
return connection.reconnect! if connection.extension_enabled?(extension)
connection.enable_extension 'uuid-ossp'
connection.enable_extension extension
connection.commit_db_transaction
connection.reconnect!
end
def disable_extension!(extension, connection)
return false unless connection.supports_extensions?
return true unless connection.extension_enabled?(extension)
connection.disable_extension extension
connection.reconnect!
end
unless ENV['FIXTURE_DEBUG']
module ActiveRecord::TestFixtures::ClassMethods
def try_to_load_dependency_with_silence(*args)
......
......@@ -78,11 +78,12 @@ def test_rename_table_for_postgresql_should_also_rename_default_sequence
end
def test_renaming_table_doesnt_attempt_to_rename_non_existent_sequences
enable_uuid_ossp!(connection)
enable_extension!('uuid-ossp', connection)
connection.create_table :cats, id: :uuid
assert_nothing_raised { rename_table :cats, :felines }
assert connection.table_exists? :felines
ensure
disable_extension!('uuid-ossp', connection)
connection.drop_table :cats if connection.table_exists? :cats
connection.drop_table :felines if connection.table_exists? :felines
end
......
......@@ -10,7 +10,7 @@ def except(adapter_names_to_exclude)
#put adapter specific setup here
case adapter_name
when "PostgreSQL"
enable_uuid_ossp!(ActiveRecord::Base.connection)
enable_extension!('uuid-ossp', ActiveRecord::Base.connection)
create_table :uuid_parents, id: :uuid, force: true do |t|
t.string :name
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册