1
|
|
|
package testinstance |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/onsi/gomega" |
9
|
|
|
"github.com/testcontainers/testcontainers-go" |
10
|
|
|
"github.com/testcontainers/testcontainers-go/wait" |
11
|
|
|
|
12
|
|
|
"github.com/Permify/permify/internal/config" |
13
|
|
|
"github.com/Permify/permify/internal/storage" |
14
|
|
|
"github.com/Permify/permify/internal/storage/postgres/utils" |
15
|
|
|
"github.com/Permify/permify/pkg/database" |
16
|
|
|
PQDatabase "github.com/Permify/permify/pkg/database/postgres" |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
func PostgresDB(postgresVersion string) database.Database { |
20
|
|
|
ctx := context.Background() |
21
|
|
|
|
22
|
|
|
image := fmt.Sprintf("postgres:%s-alpine", postgresVersion) |
23
|
|
|
|
24
|
|
|
postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
25
|
|
|
ContainerRequest: testcontainers.ContainerRequest{ |
26
|
|
|
Image: image, |
27
|
|
|
ExposedPorts: []string{"5432/tcp"}, |
28
|
|
|
Env: map[string]string{"POSTGRES_USER": "postgres", "POSTGRES_PASSWORD": "postgres", "POSTGRES_DB": "permify"}, |
29
|
|
|
WaitingFor: wait.ForAll( |
30
|
|
|
wait.ForLog("database system is ready to accept connections"), |
31
|
|
|
wait.ForListeningPort("5432/tcp"), |
32
|
|
|
), |
33
|
|
|
}, |
34
|
|
|
Started: true, |
35
|
|
|
}) |
36
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
37
|
|
|
|
38
|
|
|
// Execute the command in the container |
39
|
|
|
_, _, execErr := postgres.Exec(ctx, []string{"psql", "-U", "postgres", "-c", "ALTER SYSTEM SET track_commit_timestamp = on;"}) |
40
|
|
|
gomega.Expect(execErr).ShouldNot(gomega.HaveOccurred()) |
41
|
|
|
|
42
|
|
|
stopTimeout := 2 * time.Second |
43
|
|
|
err = postgres.Stop(context.Background(), &stopTimeout) |
44
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
45
|
|
|
|
46
|
|
|
err = postgres.Start(context.Background()) |
47
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
48
|
|
|
|
49
|
|
|
cmd := []string{"sh", "-c", "export PGPASSWORD=postgres" + "; psql -U postgres -d permify -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'"} |
50
|
|
|
|
51
|
|
|
_, _, err = postgres.Exec(ctx, cmd) |
52
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
53
|
|
|
|
54
|
|
|
host, err := postgres.Host(ctx) |
55
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
56
|
|
|
|
57
|
|
|
port, err := postgres.MappedPort(ctx, "5432") |
58
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
59
|
|
|
|
60
|
|
|
dbAddr := fmt.Sprintf("%s:%s", host, port.Port()) |
61
|
|
|
postgresDSN := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", "postgres", "postgres", dbAddr, "permify") |
62
|
|
|
|
63
|
|
|
cfg := config.Database{ |
64
|
|
|
Engine: "postgres", |
65
|
|
|
URI: postgresDSN, |
66
|
|
|
AutoMigrate: true, |
67
|
|
|
MaxOpenConnections: 20, |
68
|
|
|
MaxIdleConnections: 1, |
69
|
|
|
MaxConnectionLifetime: 300, |
70
|
|
|
MaxConnectionIdleTime: 60, |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
err = storage.Migrate(cfg) |
74
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
75
|
|
|
|
76
|
|
|
var db database.Database |
77
|
|
|
db, err = PQDatabase.New(cfg.URI, |
78
|
|
|
PQDatabase.MaxOpenConnections(cfg.MaxOpenConnections), |
79
|
|
|
PQDatabase.MaxIdleConnections(cfg.MaxIdleConnections), |
80
|
|
|
PQDatabase.MaxConnectionIdleTime(cfg.MaxConnectionIdleTime), |
81
|
|
|
PQDatabase.MaxConnectionLifeTime(cfg.MaxConnectionLifetime), |
82
|
|
|
) |
83
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
84
|
|
|
|
85
|
|
|
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).WritePool) |
86
|
|
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
87
|
|
|
|
88
|
|
|
return db |
89
|
|
|
} |
90
|
|
|
|