From b70edd54246e5c3faf67ae8552a434d8e5525625 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 16 Apr 2015 11:41:54 +1000 Subject: [PATCH] Enabling non-privileged postgres users to run specs Addressing issue #245. A combination of fixtures and foreign key constraints requires the postgres user to be superuser. Otherwise an attempt to disable constraints fails. This got fixed in Rails 4 and this patch brings the same behaviour back to Rails 3. It will allow us to run the specs with a nosuperuser postgres user. See: - https://github.com/matthuhiggins/foreigner/issues/61 - https://github.com/garysweaver/rails/commit/9bb27f7ffe3eb732df737e477cd8fc25e007f77b --- spec/spec_helper.rb | 3 ++ ..._postgresql_referential_integrity_patch.rb | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 spec/support/active_record_postgresql_referential_integrity_patch.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0679613855..806ed37f9b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -48,6 +48,9 @@ Capybara.default_max_wait_time = 30 require "paperclip/matchers" +#Fix fixtures with foreign keys, fixed in Rails4 +require_relative "support/active_record_postgresql_referential_integrity_patch" + # Override setting in Spree engine: Spree::Core::MailSettings ActionMailer::Base.default_url_options[:host] = 'test.host' diff --git a/spec/support/active_record_postgresql_referential_integrity_patch.rb b/spec/support/active_record_postgresql_referential_integrity_patch.rb new file mode 100644 index 0000000000..ae508d653d --- /dev/null +++ b/spec/support/active_record_postgresql_referential_integrity_patch.rb @@ -0,0 +1,31 @@ +# A combination of fixtures and foreign key constraints requires the postgres +# user to be superuser. Otherwise an attempt to disable constraints fails. +# This got fixed in Rails 4 and this patch brings the same behaviour back to +# Rails 3. It will allow us to run the specs with a nosuperuser postgres user. +# +# See: +# - https://github.com/matthuhiggins/foreigner/issues/61 +# - https://github.com/garysweaver/rails/commit/9bb27f7ffe3eb732df737e477cd8fc25e007f77b + +if Rails::VERSION::MAJOR < 4 + class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + def disable_referential_integrity #:nodoc: + if supports_disable_referential_integrity? then + begin + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) + rescue + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER USER" }.join(";")) + end + end + yield + ensure + if supports_disable_referential_integrity? then + begin + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) + rescue + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER USER" }.join(";")) + end + end + end + end +end