Compare commits
4 commits
f60a6f1204
...
e1a4c1234f
| Author | SHA1 | Date | |
|---|---|---|---|
| e1a4c1234f | |||
|
|
5b7b08f733 | ||
|
|
03c1025147 | ||
|
|
5f26218307 |
8 changed files with 64 additions and 9 deletions
10
.env
10
.env
|
|
@ -1,5 +1,5 @@
|
||||||
WRSR_DATABASE_NAME="wsrs"
|
WSRS_DATABASE_NAME="wsrs"
|
||||||
WRSR_DATABASE_USER="postgres"
|
WSRS_DATABASE_USER="postgres"
|
||||||
WRSR_DATABASE_PASSWORD="123456789"
|
WSRS_DATABASE_PASSWORD="123456789"
|
||||||
WRSR_DATABASE_PORT=5432
|
WSRS_DATABASE_PORT=5432
|
||||||
WRSR_DATABASE_HOST="localhost"
|
WSRS_DATABASE_HOST="localhost"
|
||||||
|
|
|
||||||
|
|
@ -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_rooms_table
|
||||||
$ tern new --migrations ./internal/store/pgstore/migrations create_messages_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.
|
||||||
|
|
|
||||||
19
cmd/tools/terndotenv/main.go
Normal file
19
cmd/tools/terndotenv/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,11 +3,11 @@ services:
|
||||||
image: postgres:latest
|
image: postgres:latest
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- ${WRSR_DATABASE_PORT:-5432}:5432
|
- ${WSRS_DATABASE_PORT:-5432}:5432
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: ${WRSR_DATABASE_USER}
|
POSTGRES_USER: ${WSRS_DATABASE_USER}
|
||||||
POSTGRES_PASSWORD: ${WRSR_DATABASE_PASSWORD}
|
POSTGRES_PASSWORD: ${WSRS_DATABASE_PASSWORD}
|
||||||
POSTGRES_DB: ${WRSR_DATABASE_NAME}
|
POSTGRES_DB: ${WSRS_DATABASE_NAME}
|
||||||
volumes:
|
volumes:
|
||||||
- db:/var/lib/postgresql/data
|
- db:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module forgejo.home.viniciussouza.me/learning/go-react-server
|
module forgejo.home.viniciussouza.me/learning/go-react-server
|
||||||
|
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
|
require github.com/joho/godotenv v1.5.1
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -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;
|
||||||
8
internal/store/pgstore/migrations/tern.conf
Normal file
8
internal/store/pgstore/migrations/tern.conf
Normal 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" }}
|
||||||
Loading…
Reference in a new issue