Skip to content

Commit f4a634d

Browse files
committed
Fix bugs with postgresql. Closes #837, Closes #838
1 parent bd8133c commit f4a634d

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang as builder
1+
FROM golang AS builder
22

33
ARG GIT_COMMIT
44
ENV GIT_COMMIT=$GIT_COMMIT

api/pkg/di/container.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ func (container *Container) GormLogger() gormLogger.Interface {
231231
}
232232

233233
func (container *Container) connect(dsn string, config *gorm.Config) (db *gorm.DB, err error) {
234-
if strings.HasPrefix(dsn, "postgres://") {
235-
return gorm.Open(postgres.Open(dsn), config)
234+
if strings.HasPrefix(dsn, "libsql://") {
235+
return gorm.Open(sqlite.New(sqlite.Config{
236+
DriverName: "libsql",
237+
DSN: dsn,
238+
}), config)
236239
}
237-
return gorm.Open(sqlite.New(sqlite.Config{
238-
DriverName: "libsql",
239-
DSN: dsn,
240-
}), config)
240+
return gorm.Open(postgres.Open(dsn), config)
241241
}
242242

243243
// DedicatedDB creates an instance of gorm.DB if it has not been created already
@@ -339,11 +339,15 @@ func (container *Container) DB() (db *gorm.DB) {
339339
}
340340

341341
container.logger.Debug(fmt.Sprintf("Running migrations for %T", db))
342+
342343
// This prevents a bug in the Gorm AutoMigrate where it tries to delete this no existent constraints
343-
db.Exec(`
344+
// This is only applicable to PROD on cockroachDB
345+
if os.Getenv("DATABASE_MIGRATION_CONSTRAINT_FIX") == "1" {
346+
db.Exec(`
344347
ALTER TABLE users ADD CONSTRAINT IF NOT EXISTS uni_users_api_key CHECK (api_key IS NOT NULL);
345348
ALTER TABLE phone_api_keys ADD CONSTRAINT IF NOT EXISTS uni_phone_api_keys_api_key CHECK (api_key IS NOT NULL);
346349
ALTER TABLE discords ADD CONSTRAINT IF NOT EXISTS uni_discords_server_id CHECK (server_id IS NOT NULL);`)
350+
}
347351

348352
if err = db.AutoMigrate(&entities.Message{}); err != nil {
349353
container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Message{})))

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.8"
2-
31
services:
42
postgres:
53
image: postgres:alpine
@@ -13,7 +11,7 @@ services:
1311
- "5435:5432"
1412
restart: on-failure
1513
healthcheck:
16-
test: ["CMD-SHELL", "pg_isready", "-U", "dbusername", "-d", "httpsms"]
14+
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
1715
interval: 30s
1816
timeout: 60s
1917
retries: 5

web/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# build stage
2-
FROM node:lts-alpine as build
2+
FROM node:lts-alpine AS build
33

44
WORKDIR /app
55

0 commit comments

Comments
 (0)