@@ -3,6 +3,8 @@ package postgres
3
3
import (
4
4
"database/sql"
5
5
"fmt"
6
+ "io/ioutil"
7
+ "os"
6
8
"time"
7
9
8
10
"github.com/ory/dockertest/v3"
@@ -16,15 +18,29 @@ func Open() (string, func(), error) {
16
18
if err != nil {
17
19
return "" , nil , xerrors .Errorf ("create pool: %w" , err )
18
20
}
21
+ tempDir , err := ioutil .TempDir (os .TempDir (), "postgres" )
22
+ if err != nil {
23
+ return "" , nil , xerrors .Errorf ("create tempdir: %w" , err )
24
+ }
19
25
resource , err := pool .RunWithOptions (& dockertest.RunOptions {
20
26
Repository : "postgres" ,
21
27
Tag : "11" ,
22
28
Env : []string {
23
29
"POSTGRES_PASSWORD=postgres" ,
24
30
"POSTGRES_USER=postgres" ,
25
31
"POSTGRES_DB=postgres" ,
32
+ // The location for temporary database files!
33
+ "PGDATA=/tmp" ,
26
34
"listen_addresses = '*'" ,
27
35
},
36
+ Mounts : []string {
37
+ // The postgres image has a VOLUME parameter in it's image.
38
+ // If we don't mount at this point, Docker will allocate a
39
+ // volume for this directory.
40
+ //
41
+ // This isn't used anyways, since we override PGDATA.
42
+ fmt .Sprintf ("%s:/var/lib/postgresql/data" , tempDir ),
43
+ },
28
44
}, func (config * docker.HostConfig ) {
29
45
// set AutoRemove to true so that stopped container goes away by itself
30
46
config .AutoRemove = true
@@ -57,5 +73,6 @@ func Open() (string, func(), error) {
57
73
}
58
74
return dbURL , func () {
59
75
_ = pool .Purge (resource )
76
+ _ = os .RemoveAll (tempDir )
60
77
}, nil
61
78
}
0 commit comments