1
1
# frozen_string_literal: true
2
2
3
-
4
3
require 'database_cleaner'
5
4
require 'closure_tree/test/matcher'
6
5
require 'tmpdir'
11
10
require 'active_record'
12
11
require 'active_support/core_ext/array'
13
12
13
+ puts "Using ActiveRecord #{ ActiveRecord . gem_version } and #{ RUBY_ENGINE } #{ RUBY_ENGINE_VERSION } as #{ RUBY_VERSION } "
14
14
15
15
# Start Simplecov
16
16
if RUBY_ENGINE == 'ruby'
20
20
end
21
21
end
22
22
23
- database_file = SecureRandom . hex
24
- ActiveRecord ::Base . configurations = debug = {
23
+ primary_database_url = ENV [ 'DATABASE_URL' ] . presence || "sqlite3:///tmp/closure_tree_test"
24
+ secondary_database_url = ENV [ 'SECONDARY_DATABASE_URL' ] . presence || "sqlite3:///tmp/closure_tree_test-s"
25
+
26
+ puts "Using primary database #{ primary_database_url } "
27
+ puts "Using secondary database #{ secondary_database_url } "
28
+
29
+ ActiveRecord ::Base . configurations = {
25
30
default_env : {
26
- url : ENV [ 'DATABASE_URL' ] . presence || "sqlite3://#{ Dir . tmpdir } /#{ database_file } .sqlite3" ,
27
- properties : { allowPublicKeyRetrieval : true } # for JRuby madness
28
- } ,
29
- secondary_env : {
30
- url : ENV [ 'SECONDARY_DATABASE_URL' ] . presence || "sqlite3://#{ Dir . tmpdir } /#{ database_file } -s.sqlite3" ,
31
- properties : { allowPublicKeyRetrieval : true } # for JRuby madness
31
+ primary : {
32
+ url : primary_database_url ,
33
+ properties : { allowPublicKeyRetrieval : true } # for JRuby madness
34
+ } ,
35
+ secondary : {
36
+ url : secondary_database_url ,
37
+ properties : { allowPublicKeyRetrieval : true } # for JRuby madness
38
+ }
32
39
}
33
40
}
34
41
35
- puts "Testing with #{ debug } "
36
-
37
42
# Configure ActiveRecord
38
43
ActiveRecord ::Migration . verbose = false
39
44
ActiveRecord ::Base . table_name_prefix = ENV [ 'DB_PREFIX' ] . to_s
@@ -75,15 +80,12 @@ def sqlite?
75
80
# disable monkey patching
76
81
# see: https://relishapp.com/rspec/rspec-core/v/3-8/docs/configuration/zero-monkey-patching-mode
77
82
config . disable_monkey_patching!
83
+ config . before ( :suite ) do
84
+ ENV [ 'FLOCK_DIR' ] = Dir . mktmpdir if sqlite?
85
+ end
78
86
79
- if sqlite?
80
- config . before ( :suite ) do
81
- ENV [ 'FLOCK_DIR' ] = Dir . mktmpdir
82
- end
83
-
84
- config . after ( :suite ) do
85
- FileUtils . remove_entry_secure ENV [ 'FLOCK_DIR' ]
86
- end
87
+ config . after ( :suite ) do
88
+ FileUtils . remove_entry_secure ( ENV [ 'FLOCK_DIR' ] ) if sqlite?
87
89
end
88
90
end
89
91
@@ -94,14 +96,24 @@ def sqlite?
94
96
# See: https://github.com/ClosureTree/with_advisory_lock
95
97
ENV [ 'WITH_ADVISORY_LOCK_PREFIX' ] ||= SecureRandom . hex
96
98
97
- ActiveRecord ::Base . connection . recreate_database ( "closure_tree_test" ) unless sqlite?
98
- puts "Testing with #{ env_db } database, ActiveRecord #{ ActiveRecord . gem_version } and #{ RUBY_ENGINE } #{ RUBY_ENGINE_VERSION } as #{ RUBY_VERSION } "
99
99
# Require our gem
100
100
require 'closure_tree'
101
+ begin
102
+ ActiveRecord ::Base . establish_connection ( :primary )
103
+ rescue
104
+ ActiveRecord ::Tasks ::DatabaseTasks . create_current ( 'primary' )
105
+ end
106
+
107
+ begin
108
+ ActiveRecord ::Base . establish_connection ( :secondary )
109
+ rescue
110
+ ActiveRecord ::Tasks ::DatabaseTasks . create_current ( 'secondary' )
111
+ end
101
112
102
113
# Load test helpers
103
114
require_relative 'support/schema'
104
115
require_relative 'support/models'
105
116
require_relative 'support/helpers'
106
117
require_relative 'support/exceed_query_limit'
107
118
require_relative 'support/query_counter'
119
+ puts "Testing with #{ env_db } database"
0 commit comments