From 6cfab72b9c9051a1dc2ecd93165afc5711758d4d Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 18 Nov 2024 20:29:45 +0700 Subject: [PATCH 1/2] add categories table and local env --- .vscode/launch.json | 2 +- Makefile | 14 +++++++++++- docker-compose-local.yml | 22 +++++++++++++++++++ docker-compose.yml | 2 +- go.mod | 4 +++- go.sum | 7 ++++++ migrations/20241109180651_add_user.sql | 4 ++-- .../20241118122922_add_coregories_table.sql | 16 ++++++++++++++ 8 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 docker-compose-local.yml create mode 100644 migrations/20241118122922_add_coregories_table.sql diff --git a/.vscode/launch.json b/.vscode/launch.json index 07692c6..3fb1181 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,7 +1,7 @@ { "configurations": [ { - "name": "Launch eparser", + "name": "Launch", "type": "go", "request": "launch", "mode": "debug", diff --git a/Makefile b/Makefile index b3c465d..120cf4a 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,22 @@ ifeq (local-migrations-create,$(firstword $(MAKECMDGOALS))) endif local-migrate-up: - @goose -allow-missing -dir ./migrations postgres "user=crab dbname=smm-core host=localhost port=5432 sslmode=disable" up + @goose -allow-missing -dir ./migrations postgres "user=crab password=crab dbname=smm-core host=localhost port=5432 sslmode=disable" up local-migrations-create: @goose -dir ./migrations postgres "user=crab dbname=smm-core host=localhost port=5432 sslmode=disable" create "${migrationName}" sql +local-up: + docker-compose -f docker-compose-local.yml up -d --force-recreate --build + +local-down: + docker-compose -f docker-compose-local.yml down + +local-wait_db: + timeout 10s bash -c "until docker exec db pg_isready; do sleep 1; done" + +local-restart: local-down local-up local-wait_db local-migrate-up + local-go-init: export GOBIN := $(LOCAL_BIN) local-go-init: go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway @@ -23,6 +34,7 @@ local-go-init: github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \ google.golang.org/protobuf/cmd/protoc-gen-go \ google.golang.org/grpc/cmd/protoc-gen-go-grpc + go install github.com/pressly/goose/v3/cmd/goose@latest generate: rm -rf proto resources diff --git a/docker-compose-local.yml b/docker-compose-local.yml new file mode 100644 index 0000000..c82489c --- /dev/null +++ b/docker-compose-local.yml @@ -0,0 +1,22 @@ +version: '3.9' + +services: + + db: + container_name: db + image: postgres + restart: always + shm_size: 128mb + environment: + POSTGRES_USER: crab + POSTGRES_PASSWORD: crab + POSTGRES_DB: smm-core + ports: + - 5432:5432 + + adminer: + container_name: adminer + image: adminer + restart: always + ports: + - 8100:8080 diff --git a/docker-compose.yml b/docker-compose.yml index 93e7ad8..635ab8c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,4 +12,4 @@ services: networks: common-network: - external: true \ No newline at end of file + external: true diff --git a/go.mod b/go.mod index 8436c0b..8d5c53d 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38 google.golang.org/grpc v1.67.1 - google.golang.org/protobuf v1.35.1 + google.golang.org/protobuf v1.35.2 ) require ( @@ -14,4 +14,6 @@ require ( golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.19.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect + google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5a937cc..a6c5f3b 100644 --- a/go.sum +++ b/go.sum @@ -14,5 +14,12 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= +google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A= +google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/migrations/20241109180651_add_user.sql b/migrations/20241109180651_add_user.sql index 2c9c2a4..81b8407 100644 --- a/migrations/20241109180651_add_user.sql +++ b/migrations/20241109180651_add_user.sql @@ -1,6 +1,6 @@ -- +goose Up -- +goose StatementBegin -CREATE TABLE IF NOT EXISTS "user" ( +CREATE TABLE IF NOT EXISTS "users" ( id SERIAL PRIMARY KEY, chat_id TEXT NOT NULL ); @@ -8,5 +8,5 @@ CREATE TABLE IF NOT EXISTS "user" ( -- +goose Down -- +goose StatementBegin -DROP TABLE IF EXISTS "user"; +DROP TABLE IF EXISTS "users"; -- +goose StatementEnd diff --git a/migrations/20241118122922_add_coregories_table.sql b/migrations/20241118122922_add_coregories_table.sql new file mode 100644 index 0000000..332b2e3 --- /dev/null +++ b/migrations/20241118122922_add_coregories_table.sql @@ -0,0 +1,16 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE IF NOT EXISTS categories ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL, + user_id INT REFERENCES "users"(id), + favorite BOOLEAN DEFAULT FALSE, + "limit" INT DEFAULT 0 +); +comment on column categories.limit is 'Лимит трат в месяц в копейках'; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS categories; +-- +goose StatementEnd From d9bcceb36749b235b01beaf2d5560f9fe563e1eb Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 18 Nov 2024 20:29:45 +0700 Subject: [PATCH 2/2] fix --- migrations/20241109180651_add_user.sql | 4 ++-- migrations/20241118122922_add_coregories_table.sql | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/migrations/20241109180651_add_user.sql b/migrations/20241109180651_add_user.sql index 81b8407..a590932 100644 --- a/migrations/20241109180651_add_user.sql +++ b/migrations/20241109180651_add_user.sql @@ -1,6 +1,6 @@ -- +goose Up -- +goose StatementBegin -CREATE TABLE IF NOT EXISTS "users" ( +CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, chat_id TEXT NOT NULL ); @@ -8,5 +8,5 @@ CREATE TABLE IF NOT EXISTS "users" ( -- +goose Down -- +goose StatementBegin -DROP TABLE IF EXISTS "users"; +DROP TABLE IF EXISTS users; -- +goose StatementEnd diff --git a/migrations/20241118122922_add_coregories_table.sql b/migrations/20241118122922_add_coregories_table.sql index 332b2e3..e44dd3a 100644 --- a/migrations/20241118122922_add_coregories_table.sql +++ b/migrations/20241118122922_add_coregories_table.sql @@ -3,11 +3,10 @@ CREATE TABLE IF NOT EXISTS categories ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, - user_id INT REFERENCES "users"(id), + user_id INT REFERENCES users(id), favorite BOOLEAN DEFAULT FALSE, - "limit" INT DEFAULT 0 + monthly_limit INT DEFAULT 0 ); -comment on column categories.limit is 'Лимит трат в месяц в копейках'; -- +goose StatementEnd -- +goose Down