From f9a76158014f813eb99f372528b6e9759fbc69b7 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Thu, 8 Aug 2024 16:15:40 +0500 Subject: [PATCH] add users table migrations. Need to install goose into migrator --- .env | 1 + docker-compose.yaml | 1 - migrator/Dockerfile | 9 +++++---- migrator/go.mod | 3 +++ .../migrations/20240808094749_create_users_table.sql | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 migrator/go.mod diff --git a/.env b/.env index 64fb093..eaf333a 100644 --- a/.env +++ b/.env @@ -4,6 +4,7 @@ DB_NAME=postgres DB_PORT=5432 HOST_DB=db SERVER_PORT=8000 +SSLmode=disable POSTGRESQL_URL='postgres://postgres:postgres@db:5432/postgres?sslmode=disable' # MIGRATOR diff --git a/docker-compose.yaml b/docker-compose.yaml index 4ebda88..dcfae78 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -30,7 +30,6 @@ services: - .env ports: - "${MIGRATOR_PORT}:${MIGRATOR_PORT}" - command: "goose -dir ./migratios postgres 'user=postgres dbname=postgres sslmode=disable' up" volumes: postgres-db: \ No newline at end of file diff --git a/migrator/Dockerfile b/migrator/Dockerfile index eeb64e7..c513482 100644 --- a/migrator/Dockerfile +++ b/migrator/Dockerfile @@ -1,10 +1,11 @@ FROM golang:1.22.5 -RUN go get github.com/pressly/goose/cmd/goose - - WORKDIR /app COPY migrations/* app/migrations/ -CMD [ "goose", "up", "-dir=/app/migrations" ] \ No newline at end of file +COPY go.mod app/ + +RUN go install github.com/pressly/goose/v3/cmd/goose@latest + +CMD ["goose", "-dir=/app/migrations", "user=${DB_USER}", "dbname=${DB_NAME}", "sslmode=${SSLmode}" "up"] \ No newline at end of file diff --git a/migrator/go.mod b/migrator/go.mod new file mode 100644 index 0000000..79a718a --- /dev/null +++ b/migrator/go.mod @@ -0,0 +1,3 @@ +module migrator + +go 1.22.5 diff --git a/migrator/migrations/20240808094749_create_users_table.sql b/migrator/migrations/20240808094749_create_users_table.sql index a9630ac..a63ac39 100644 --- a/migrator/migrations/20240808094749_create_users_table.sql +++ b/migrator/migrations/20240808094749_create_users_table.sql @@ -1,6 +1,6 @@ -- +goose Up -- +goose StatementBegin -CREATE TABLE users( +CREATE TABLE IF NOT EXISTS users( id UUID NOT NULL PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL UNIQUE INDEX, @@ -11,5 +11,5 @@ CREATE TABLE users( -- +goose Down -- +goose StatementBegin -DROP TABLE users; +DROP TABLE IF EXISTS users; -- +goose StatementEnd