MCPcopy Create free account
hub / github.com/Permify/permify / PostgresDB

Function PostgresDB

pkg/testinstance/postgres.go:58–135  ·  view source on GitHub ↗
(postgresVersion string)

Source from the content-addressed store, hash-verified

56}
57
58func PostgresDB(postgresVersion string) *PostgresInstance {
59 ctx := context.Background()
60
61 image := fmt.Sprintf("postgres:%s-alpine", postgresVersion)
62
63 postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
64 ContainerRequest: testcontainers.ContainerRequest{
65 Image: image,
66 ExposedPorts: []string{"5432/tcp"},
67 Env: map[string]string{"POSTGRES_USER": "postgres", "POSTGRES_PASSWORD": "postgres", "POSTGRES_DB": "permify"},
68 Cmd: []string{"postgres", "-c", "track_commit_timestamp=on"},
69 WaitingFor: wait.ForAll(
70 wait.ForLog("database system is ready to accept connections"),
71 wait.ForListeningPort("5432/tcp"),
72 ),
73 },
74 Started: true,
75 })
76 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
77
78 var db *PQDatabase.Postgres
79
80 cleanup := func() {
81 if db != nil {
82 _ = db.Close()
83 }
84 if postgres != nil {
85 terminateCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
86 defer cancel()
87 _ = postgres.Terminate(terminateCtx)
88 }
89 }
90
91 expectNoError := func(err error) {
92 if err != nil {
93 cleanup()
94 }
95 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
96 }
97
98 host, err := postgres.Host(ctx)
99 expectNoError(err)
100
101 port, err := postgres.MappedPort(ctx, "5432")
102 expectNoError(err)
103
104 dbAddr := fmt.Sprintf("%s:%s", host, port.Port())
105 postgresDSN := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", "postgres", "postgres", dbAddr, "permify")
106
107 cfg := config.Database{
108 Engine: "postgres",
109 URI: postgresDSN,
110 AutoMigrate: true,
111 MaxOpenConnections: 20,
112 MaxIdleConnections: 1,
113 MaxConnectionLifetime: 300,
114 MaxConnectionIdleTime: 60,
115 }

Callers 12

watch_test.goFile · 0.92
common_test.goFile · 0.92
options_test.goFile · 0.92
gc_test.goFile · 0.92

Calls 3

CloseMethod · 0.95
MigrateFunction · 0.92
EnsureDBVersionFunction · 0.92

Tested by

no test coverage detected