Compare commits

...

4 commits

Author SHA1 Message Date
e1a4c1234f chore(doc): add tern's wrapper documentation 2024-08-09 11:15:28 +00:00
Vinicius Souza
5b7b08f733 feat: add wrapper to tern
so environment variables are passed to tern's config file
2024-08-09 11:10:54 +00:00
Vinicius Souza
03c1025147 feat: add db migrations 2024-08-09 11:04:55 +00:00
Vinicius Souza
5f26218307 fix: environment variable names 2024-08-09 11:03:59 +00:00
8 changed files with 64 additions and 9 deletions

10
.env
View file

@ -1,5 +1,5 @@
WRSR_DATABASE_NAME="wsrs"
WRSR_DATABASE_USER="postgres"
WRSR_DATABASE_PASSWORD="123456789"
WRSR_DATABASE_PORT=5432
WRSR_DATABASE_HOST="localhost"
WSRS_DATABASE_NAME="wsrs"
WSRS_DATABASE_USER="postgres"
WSRS_DATABASE_PASSWORD="123456789"
WSRS_DATABASE_PORT=5432
WSRS_DATABASE_HOST="localhost"

View file

@ -22,3 +22,8 @@ $ go install github.com/jackc/tern/v2@latest
$ tern new --migrations ./internal/store/pgstore/migrations create_rooms_table
$ tern new --migrations ./internal/store/pgstore/migrations create_messages_table
```
### Wrapper around tern
`tern.config` does not read `.env` file directly so a wrapper was created to be
able to load environment variables.

View file

@ -0,0 +1,19 @@
package main
import (
"os/exec"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil {
panic(err)
}
cmd := exec.Command("tern", "migrate", "--migrations", "./internal/store/pgstore/migrations", "--config", "./internal/store/pgstore/migrations/tern.conf")
if err := cmd.Run(); err != nil {
panic(err)
}
}

View file

@ -3,11 +3,11 @@ services:
image: postgres:latest
restart: unless-stopped
ports:
- ${WRSR_DATABASE_PORT:-5432}:5432
- ${WSRS_DATABASE_PORT:-5432}:5432
environment:
POSTGRES_USER: ${WRSR_DATABASE_USER}
POSTGRES_PASSWORD: ${WRSR_DATABASE_PASSWORD}
POSTGRES_DB: ${WRSR_DATABASE_NAME}
POSTGRES_USER: ${WSRS_DATABASE_USER}
POSTGRES_PASSWORD: ${WSRS_DATABASE_PASSWORD}
POSTGRES_DB: ${WSRS_DATABASE_NAME}
volumes:
- db:/var/lib/postgresql/data

2
go.mod
View file

@ -1,3 +1,5 @@
module forgejo.home.viniciussouza.me/learning/go-react-server
go 1.22.2
require github.com/joho/godotenv v1.5.1

View file

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS rooms (
"id" uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
"theme" VARCHAR(255) NOT NULL
);
---- create above / drop below ----
DROP TABLE IF EXISTS rooms;

View file

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS messages (
"id" uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
"room_id" uuid NOT NULL,
"message" VARCHAR(255) NOT NULL,
"reaction_count" BIGINT NOT NULL DEFAULT 0,
"answered" BOOLEAN NOT NULL DEFAULT false,
FOREIGN KEY (room_id) REFERENCES rooms(id)
);
---- create above / drop below ----
DROP TABLE IF EXISTS messages;

View file

@ -0,0 +1,8 @@
[database]
host = {{ env "WSRS_DATABASE_HOST" }}
port = {{ env "WSRS_DATABASE_PORT" }}
# database is required
database = {{ env "WSRS_DATABASE_NAME" }}
# user defaults to OS user
user = {{ env "WSRS_DATABASE_USER" }}
password = {{ env "WSRS_DATABASE_PASSWORD" }}