@@ -45,10 +45,50 @@ 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_system
85+ @db_system ||= db_uri . scheme
86+ end
87+
88+ def self . db_name
89+ @db_name ||= File . basename ( db_uri . path [ 1 ..-1 ] )
90+ end
91+
5292 def self . application_file
5393 @application_file ||= begin
5494 current = Dir [ root_path . join ( "config/applications/rails-*.rb" ) ]
@@ -61,14 +101,29 @@ def self.application_file
61101
62102 def self . load_test_schema
63103 @__schema_loaded__ ||= begin
64- # This is more reliable than setting config/database.yml
65- ENV [ "DATABASE_URL" ] = "sqlite3://#{ db_path } "
66-
67104 # Silence migrations output
68105 ActiveRecord ::Migration . verbose = false
69106
70107 # We need to connect manually here
71- ActiveRecord ::Base . establish_connection ( adapter : "sqlite3" , database : db_path )
108+ case db_uri . scheme
109+ when "postgresql"
110+ ActiveRecord ::Base . establish_connection (
111+ adapter : db_uri . scheme ,
112+ host : db_uri . host ,
113+ username : db_uri . user ,
114+ password : db_uri . password ,
115+ database : db_name
116+ )
117+ when "sqlite3"
118+ ActiveRecord ::Base . establish_connection (
119+ adapter : db_uri . scheme ,
120+ database : db_path . to_s
121+ )
122+ else
123+ ActiveRecord ::Base . establish_connection (
124+ adapter : db_uri . scheme , database : db_name
125+ )
126+ end
72127
73128 # Load schema from db/schema.rb into the current connection
74129 require Test ::Application . schema_file
0 commit comments