@@ -45,10 +45,46 @@ def self.schema_file
4545 @schema_file ||= root_path . join ( "db/schema.rb" )
4646 end
4747
48+ def self . database_url
49+ @__database_url__ ||=
50+ begin
51+ url = ENV [ "DATABASE_URL" ]
52+
53+ if url
54+ url
55+ else
56+ url =
57+ case ENV [ "DATABASE" ] . to_s
58+ when "sqlite3" then "sqlite3://#{ db_path } "
59+ when "postgresql" then "postgresql://postgres:postgres@#{ db_host } :5432/sentry"
60+ else
61+ raise "Unsupported database for tests: #{ ENV [ "DATABASE" ] . inspect } "
62+ end
63+
64+ # Set it too to make AR happy
65+ ENV [ "DATABASE_URL" ] = url
66+
67+ url
68+ end
69+ end
70+ end
71+
4872 def self . db_path
4973 @db_path ||= root_path . join ( "db" , "db.sqlite3" )
5074 end
5175
76+ def self . db_host
77+ @db_host ||= ( ENV [ "DATABASE_HOST" ] || "postgres" )
78+ end
79+
80+ def self . db_uri
81+ @db_url ||= URI ( database_url )
82+ end
83+
84+ def self . db_name
85+ @db_name ||= File . basename ( db_uri . path [ 1 ..-1 ] )
86+ end
87+
5288 def self . application_file
5389 @application_file ||= begin
5490 current = Dir [ root_path . join ( "config/applications/rails-*.rb" ) ]
@@ -61,14 +97,29 @@ def self.application_file
6197
6298 def self . load_test_schema
6399 @__schema_loaded__ ||= begin
64- # This is more reliable than setting config/database.yml
65- ENV [ "DATABASE_URL" ] = "sqlite3://#{ db_path } "
66-
67100 # Silence migrations output
68101 ActiveRecord ::Migration . verbose = false
69102
70103 # We need to connect manually here
71- ActiveRecord ::Base . establish_connection ( adapter : "sqlite3" , database : db_path )
104+ case db_uri . scheme
105+ when "postgresql"
106+ ActiveRecord ::Base . establish_connection (
107+ adapter : db_uri . scheme ,
108+ host : db_uri . host ,
109+ username : db_uri . user ,
110+ password : db_uri . password ,
111+ database : db_name
112+ )
113+ when "sqlite3"
114+ ActiveRecord ::Base . establish_connection (
115+ adapter : db_uri . scheme ,
116+ database : db_path . to_s
117+ )
118+ else
119+ ActiveRecord ::Base . establish_connection (
120+ adapter : db_uri . scheme , database : db_name
121+ )
122+ end
72123
73124 # Load schema from db/schema.rb into the current connection
74125 require Test ::Application . schema_file
0 commit comments