diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 777e9fa..8a7c089 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,9 +50,9 @@ jobs: - activerecord_6.1 - activerecord_edge adapter: - - '' # SQLite3 + - sqlite3:///tmp/closure_tree_test - mysql2://root:root@0/closure_tree_test - - postgres://closure_tree:closure_tree@0/closure_tree_test + - postgres://postgres:postgres@0/closure_tree_test exclude: - ruby: '3.0' rails: activerecord_edge diff --git a/.github/workflows/ci_jruby.yml b/.github/workflows/ci_jruby.yml index 04f1d5a..558fbcc 100644 --- a/.github/workflows/ci_jruby.yml +++ b/.github/workflows/ci_jruby.yml @@ -43,9 +43,9 @@ jobs: - activerecord_7.0 - activerecord_6.1 adapter: - - '' # SQLite3 + - sqlite3:///tmp/closure_tree_test - mysql2://root:root@0/closure_tree_test - - postgres://closure_tree:closure_tree@0/closure_tree_test + - postgres://postgres:postgres@0/closure_tree_test steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.github/workflows/ci_truffleruby.yml b/.github/workflows/ci_truffleruby.yml index 7dc07ab..77bf270 100644 --- a/.github/workflows/ci_truffleruby.yml +++ b/.github/workflows/ci_truffleruby.yml @@ -45,9 +45,9 @@ jobs: - activerecord_7.0 - activerecord_6.1 adapter: - - '' # SQLite3 + - sqlite3:///tmp/closure_tree_test - mysql2://root:root@0/closure_tree_test - - postgres://closure_tree:closure_tree@0/closure_tree_test + - postgres://postgres:postgres@0/closure_tree_test steps: - name: Checkout diff --git a/.gitignore b/.gitignore index 2f6900c..e9f555f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ tmp/ .ruby-* *.iml coverage/ +.env diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2466790..aabdd6f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true - require 'database_cleaner' require 'closure_tree/test/matcher' require 'tmpdir' @@ -11,6 +10,7 @@ require 'active_record' require 'active_support/core_ext/array' +puts "Using ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" # Start Simplecov if RUBY_ENGINE == 'ruby' @@ -20,20 +20,25 @@ end end -database_file = SecureRandom.hex -ActiveRecord::Base.configurations = debug = { +primary_database_url = ENV['DATABASE_URL'].presence || "sqlite3:///tmp/closure_tree_test" +secondary_database_url = ENV['SECONDARY_DATABASE_URL'].presence || "sqlite3:///tmp/closure_tree_test-s" + +puts "Using primary database #{primary_database_url}" +puts "Using secondary database #{secondary_database_url}" + +ActiveRecord::Base.configurations = { default_env: { - url: ENV['DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}.sqlite3", - properties: { allowPublicKeyRetrieval: true } # for JRuby madness - }, - secondary_env: { - url: ENV['SECONDARY_DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}-s.sqlite3", - properties: { allowPublicKeyRetrieval: true } # for JRuby madness + primary: { + url: primary_database_url, + properties: { allowPublicKeyRetrieval: true } # for JRuby madness + }, + secondary: { + url: secondary_database_url, + properties: { allowPublicKeyRetrieval: true } # for JRuby madness + } } } -puts "Testing with #{debug}" - # Configure ActiveRecord ActiveRecord::Migration.verbose = false ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s @@ -75,15 +80,12 @@ def sqlite? # disable monkey patching # see: https://relishapp.com/rspec/rspec-core/v/3-8/docs/configuration/zero-monkey-patching-mode config.disable_monkey_patching! + config.before(:suite) do + ENV['FLOCK_DIR'] = Dir.mktmpdir if sqlite? + end - if sqlite? - config.before(:suite) do - ENV['FLOCK_DIR'] = Dir.mktmpdir - end - - config.after(:suite) do - FileUtils.remove_entry_secure ENV['FLOCK_DIR'] - end + config.after(:suite) do + FileUtils.remove_entry_secure(ENV['FLOCK_DIR']) if sqlite? end end @@ -94,10 +96,19 @@ def sqlite? # See: https://github.com/ClosureTree/with_advisory_lock ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex -ActiveRecord::Base.connection.recreate_database("closure_tree_test") unless sqlite? -puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" # Require our gem require 'closure_tree' +begin + ActiveRecord::Base.establish_connection(:primary) +rescue + ActiveRecord::Tasks::DatabaseTasks.create_current('primary') +end + +begin + ActiveRecord::Base.establish_connection(:secondary) +rescue + ActiveRecord::Tasks::DatabaseTasks.create_current('secondary') +end # Load test helpers require_relative 'support/schema' @@ -105,3 +116,4 @@ def sqlite? require_relative 'support/helpers' require_relative 'support/exceed_query_limit' require_relative 'support/query_counter' +puts "Testing with #{env_db} database" diff --git a/spec/support/models.rb b/spec/support/models.rb index 077a7b7..eee6841 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -10,7 +10,7 @@ def to_s def add_destroyed_tag # Proof for the tests that the destroy rather than the delete method was called: - DestroyedTag.create(name:) + DestroyedTag.create(name: to_s) end end @@ -30,7 +30,7 @@ def to_s def add_destroyed_tag # Proof for the tests that the destroy rather than the delete method was called: - DestroyedTag.create(name:) + DestroyedTag.create(name: to_s) end end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 7d92f40..0626605 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -7,7 +7,7 @@ class ApplicationRecord < ActiveRecord::Base class SecondDatabaseRecord < ActiveRecord::Base self.abstract_class = true - establish_connection :secondary_env + establish_connection :secondary end ActiveRecord::Schema.define(version: 0) do diff --git a/test/test_helper.rb b/test/test_helper.rb index cdb6c6b..8a870a6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,23 +7,29 @@ require 'support/query_counter' require 'parallel' -database_file = SecureRandom.hex -ActiveRecord::Base.configurations = debug = { +puts "Using ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" + +primary_database_url = ENV['DATABASE_URL'].presence || "sqlite3:///tmp/closure_tree_test" +secondary_database_url = ENV['SECONDARY_DATABASE_URL'].presence || "sqlite3:///tmp/closure_tree_test-s" + +puts "Using primary database #{primary_database_url}" +puts "Using secondary database #{secondary_database_url}" + +ActiveRecord::Base.configurations = { default_env: { - url: ENV['DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}.sqlite3", - properties: { allowPublicKeyRetrieval: true } # for JRuby madness - }, - secondary_env: { - url: ENV['SECONDARY_DATABASE_URL'].presence || "sqlite3://#{Dir.tmpdir}/#{database_file}-s.sqlite3", - properties: { allowPublicKeyRetrieval: true } # for JRuby madness + primary: { + url: primary_database_url, + properties: { allowPublicKeyRetrieval: true } # for JRuby madness + }, + secondary: { + url: secondary_database_url, + properties: { allowPublicKeyRetrieval: true } # for JRuby madness + } } } -puts "Testing with #{debug}" - ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex - def env_db @env_db ||= ActiveRecord::Base.connection_db_config.adapter.to_sym end @@ -37,9 +43,9 @@ def sqlite? env_db == :sqlite3 end -puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" DatabaseCleaner.strategy = :truncation +DatabaseCleaner.allow_remote_database_url = true module Minitest class Spec @@ -61,6 +67,19 @@ class Spec Thread.abort_on_exception = true require 'closure_tree' +begin + ActiveRecord::Base.establish_connection(:primary) +rescue + ActiveRecord::Tasks::DatabaseTasks.create_current('primary') +end + +begin + ActiveRecord::Base.establish_connection(:secondary) +rescue + ActiveRecord::Tasks::DatabaseTasks.create_current('secondary') +end + require_relative '../spec/support/schema' require_relative '../spec/support/models' -ActiveRecord::Base.connection.recreate_database('closure_tree_test') unless sqlite? + +puts "Testing with #{env_db} database"