@@ -58,38 +58,28 @@ func SkipInMemory(t *testing.T) {
5858// CreateOrReplace creates a new database with the given name (optionally from template), and schedules it to be dropped
5959// after test completion.
6060func CreateOrReplace (t testing.TB , u url.URL , dbName string , template string ) url.URL {
61- if u .Path == "" {
62- t .Fatal ("path missing from database URL" )
63- }
61+ require .NotEmpty (t , u .Path , "path missing from database URL" )
62+ require .Less (t , len (dbName ), 63 , "dbName %v too long (%d), max is 63 bytes" , dbName , len (dbName ))
6463
65- if l := len (dbName ); l > 63 {
66- t .Fatalf ("dbName %v too long (%d), max is 63 bytes" , dbName , l )
67- }
6864 // Cannot drop test database if we are connected to it, so we must connect
6965 // to a different one. 'postgres' should be present on all postgres installations
7066 u .Path = "/postgres"
7167 db , err := sql .Open (pg .DriverPostgres , u .String ())
72- if err != nil {
73- t .Fatalf ("in order to drop the test database, we need to connect to a separate database" +
74- " called 'postgres'. But we are unable to open 'postgres' database: %+v\n " , err )
75- }
68+ require .NoError (t , err , "in order to drop the test database, we need to connect to a separate database" +
69+ " called 'postgres'. But we are unable to open 'postgres' database" )
7670 defer db .Close ()
7771
7872 quotedName := pq .QuoteIdentifier (dbName )
7973 // WITH (FORCE) requires PostgreSQL 13+; terminates backends and avoids "being accessed by other users".
8074 _ , err = db .Exec (fmt .Sprintf ("DROP DATABASE IF EXISTS %s WITH (FORCE)" , quotedName ))
81- if err != nil {
82- t .Fatalf ("unable to drop postgres migrations test database: %v" , err )
83- }
75+ require .NoError (t , err , "unable to drop postgres migrations test database" )
8476 if template != "" {
8577 quotedTemplate := pq .QuoteIdentifier (template )
8678 _ , err = db .Exec (fmt .Sprintf ("CREATE DATABASE %s WITH TEMPLATE %s" , quotedName , quotedTemplate ))
8779 } else {
8880 _ , err = db .Exec (fmt .Sprintf ("CREATE DATABASE %s" , quotedName ))
8981 }
90- if err != nil {
91- t .Fatalf ("unable to create postgres test database with name '%s': %v" , dbName , err )
92- }
82+ require .NoError (t , err , "unable to create postgres test database with name '%s'" , dbName )
9383 u .Path = fmt .Sprintf ("/%s" , dbName )
9484 // simple best effort; some tests seem to hold a db connection and race with this drop
9585 t .Cleanup (func () {
@@ -100,7 +90,7 @@ func CreateOrReplace(t testing.TB, u url.URL, dbName string, template string) ur
10090 if err == nil {
10191 return
10292 }
103- time .Sleep (500 * time .Millisecond )
93+ time .Sleep (10 * time .Millisecond )
10494 }
10595 t .Logf ("unable to drop postgres test database with name '%s': %v" , dbName , err )
10696 })
0 commit comments