diff --git a/.env b/.env index 74ebe01..64fb093 100644 --- a/.env +++ b/.env @@ -4,4 +4,7 @@ DB_NAME=postgres DB_PORT=5432 HOST_DB=db SERVER_PORT=8000 -POSTGRESQL_URL='postgres://postgres:postgres@db:5432/postgres?sslmode=disable' \ No newline at end of file +POSTGRESQL_URL='postgres://postgres:postgres@db:5432/postgres?sslmode=disable' + +# MIGRATOR +MIGRATOR_PORT=3000 \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 4747959..4ebda88 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,10 +4,6 @@ services: image: postgres:16 env_file: - .env - environment: - - POSTGRES_USER=${DB_USER} - - POSTGRES_PASSWORD=${DB_PASSWORD} - - POSTGRES_DB=${DB_NAME} ports: - "${DB_PORT}:${DB_PORT}" volumes: @@ -28,5 +24,13 @@ services: - db command: ./bin/api.exe + migrator: + build: ./migrator + env_file: + - .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/migrations/20240806055315_init_user_table.sql b/migrations/20240806055315_init_user_table.sql deleted file mode 100644 index 96eb899..0000000 --- a/migrations/20240806055315_init_user_table.sql +++ /dev/null @@ -1,15 +0,0 @@ --- +goose Up --- +goose StatementBegin -CREATE TABLE IF NOT EXISTS users( - Id UUID PRIMARY KEY NOT NULL, - name VARCHAR(50) NOT NULL, - email VARCHAR(50), - password VARCHAR(1000) NOT NULL, - phone VARCHAR(30) NOT NULL -); --- +goose StatementEnd - --- +goose Down --- +goose StatementBegin -DROP TABLE IF EXISTS users; --- +goose StatementEnd diff --git a/migrations/goose.env b/migrations/goose.env deleted file mode 100644 index 913dead..0000000 --- a/migrations/goose.env +++ /dev/null @@ -1,3 +0,0 @@ -GOOSE_DRIVER=postgres -GOOSE_DBSTRING="user=postgres dbname=postgres sslmode=disable" -GOOSE_MIGRATION_DIR=/ \ No newline at end of file diff --git a/migrator/Dockerfile b/migrator/Dockerfile new file mode 100644 index 0000000..eeb64e7 --- /dev/null +++ b/migrator/Dockerfile @@ -0,0 +1,10 @@ +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 diff --git a/migrator/migrations/20240808094749_create_users_table.sql b/migrator/migrations/20240808094749_create_users_table.sql new file mode 100644 index 0000000..a9630ac --- /dev/null +++ b/migrator/migrations/20240808094749_create_users_table.sql @@ -0,0 +1,15 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE users( + id UUID NOT NULL PRIMARY KEY, + name VARCHAR(50) NOT NULL, + email VARCHAR(50) NOT NULL UNIQUE INDEX, + password VARCHAR(500) NOT NULL, + phone VARCHAR(50) +); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE users; +-- +goose StatementEnd