-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from fabiante/feat/postgres
- Loading branch information
Showing
17 changed files
with
183 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
/.env | ||
*.sqlite |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
func LoadEnv() { | ||
path := ".env" | ||
|
||
// check if .env file exists - if not, exit early. | ||
_, err := os.Stat(path) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return | ||
} | ||
|
||
err = godotenv.Load(path) | ||
if err != nil { | ||
panic(fmt.Errorf("loading env failed: %w", err)) | ||
} | ||
} | ||
|
||
func DbDSN() string { | ||
dsn := os.Getenv("PERSURL_DB_DSN") | ||
if dsn == "" { | ||
dsn = os.Getenv("DATABASE_URL") | ||
} | ||
if dsn == "" { | ||
log.Fatalf("persurl db dsn may not be empty") | ||
} | ||
return dsn | ||
} | ||
|
||
func DbMaxConnections() int { | ||
val := os.Getenv("PERSURL_DB_MAX_CONNECTIONS") | ||
if val == "" { | ||
val = "10" | ||
} | ||
|
||
maxCon, err := strconv.ParseInt(val, 10, 32) | ||
if err != nil { | ||
log.Fatalf("invalid db max connection parameter %s", val) | ||
} | ||
|
||
return int(maxCon) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/doug-martin/goqu/v9" | ||
) | ||
|
||
// EmptyTables is used to empty a collection of tables. This may be useful if truncating a | ||
// table is not possible. | ||
func EmptyTables(db *goqu.Database, tables ...string) error { | ||
return db.WithTx(func(db *goqu.TxDatabase) error { | ||
var errs []error | ||
for _, table := range tables { | ||
_, err := db.Delete(table).Executor().Exec() | ||
errs = append(errs, err) | ||
} | ||
return errors.Join(errs...) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
version: '3.8' | ||
|
||
services: | ||
db: | ||
image: postgres:14-alpine | ||
environment: | ||
- POSTGRES_DB=persurl | ||
- POSTGRES_USER=persurl | ||
- POSTGRES_PASSWORD=persurl | ||
ports: | ||
- '5432:5432' | ||
volumes: | ||
- db:/var/lib/postgresql/data:rw | ||
healthcheck: | ||
test: [ "CMD-SHELL", "pg_isready" ] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 5 | ||
|
||
pgadmin: | ||
profiles: ["pgadmin"] | ||
image: dpage/pgadmin4 | ||
environment: | ||
PGADMIN_DEFAULT_EMAIL: [email protected] | ||
PGADMIN_DEFAULT_PASSWORD: root | ||
ports: | ||
- "5454:80" | ||
|
||
volumes: | ||
db: |
Oops, something went wrong.