From 062bb7422177d919e02e7bb6fe5a2709700f1daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Thu, 13 Apr 2023 14:35:36 +0200 Subject: [PATCH] fix migration --- .gitignore | 6 ++-- .qovery.yml | 17 --------- Dockerfile | 17 ++++----- cmd/api/main.go | 4 +-- docker-compose.yml | 27 ++++++++++++++ logs.txt | 0 pkg/api/api.go | 2 +- .../10_create_table_transaction_status.go | 2 +- pkg/migrate/11_populate_transaction_status.go | 2 +- pkg/migrate/1_create_table_api.go | 2 +- pkg/migrate/2_create_table_users.go | 2 +- pkg/migrate/3_create_table_wallets.go | 2 +- .../4_create_table_transaction_types.go | 2 +- pkg/migrate/5_create_table_transactions.go | 2 +- .../6_create_table_subscription_types.go | 2 +- pkg/migrate/7_create_table_subscriptions.go | 2 +- pkg/migrate/8_populate_subscription_types.go | 2 +- pkg/migrate/9_populate_transaction_types.go | 2 +- pkg/migrate/migrate.go | 35 ++++++++++++------- pkg/utl/db/db.go | 9 +++-- querys.txt | 28 --------------- 21 files changed, 80 insertions(+), 87 deletions(-) delete mode 100644 .qovery.yml create mode 100644 docker-compose.yml delete mode 100644 logs.txt delete mode 100644 querys.txt diff --git a/.gitignore b/.gitignore index f7ddef1..ef64353 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,7 @@ _testmain.go *.test *.prof -.Dockerfile +# .Dockerfile -logs.txt -querys.txt \ No newline at end of file +logs.log +querys.log \ No newline at end of file diff --git a/.qovery.yml b/.qovery.yml deleted file mode 100644 index 989d7d3..0000000 --- a/.qovery.yml +++ /dev/null @@ -1,17 +0,0 @@ ---- -application: - name: wallet-go-api - project: wallet-api - organization: QoveryCommunity - -databases: - - type: POSTGRESQL - name: wallet - version: 12 - -routers: - - name: main-wallet-go-api - routes: - - application_name: wallet-go-api - paths: - - / diff --git a/Dockerfile b/Dockerfile index 9adae44..051cca8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,14 @@ -FROM golang:alpine as builder +FROM golang:alpine WORKDIR /app -COPY . . +COPY go.mod go.sum ./ +RUN go mod download -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/api ./cmd/api/main.go +COPY . ./ -FROM scratch +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /api ./cmd/api/main.go -WORKDIR /app +EXPOSE 4000 -COPY --from=builder /app/bin/api /usr/bin/ - -EXPOSE 80 - -CMD ["api"] +CMD ["/api"] diff --git a/cmd/api/main.go b/cmd/api/main.go index 0240486..60e2d62 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -21,9 +21,9 @@ func main() { r := gin.New() r.Use(middleware.CORSMiddleware()) - file, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { - log.Print("Cannot open file logs.txt") + log.Print("Cannot open file logs.log") } log.SetOutput(file) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..79db2a8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +version: '3' +services: + backend: + volumes: + - ./:/app + build: + dockerfile: Dockerfile + environment: + DATABASE_URL: postgresql://postgres:postgres@db:5432/wallet?sslmode=disable + PORT: 4000 + ports: + - 4000:4000 + depends_on: + - db + db: + image: postgres:13.1-alpine + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + PGDATA: /var/lib/postgresql/data/pgdata + restart: always + volumes: + - pgdata:/var/lib/postgresql/data + ports: + - 5432:5432 +volumes: + pgdata: \ No newline at end of file diff --git a/logs.txt b/logs.txt deleted file mode 100644 index e69de29..0000000 diff --git a/pkg/api/api.go b/pkg/api/api.go index 35f3864..2026482 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -14,7 +14,7 @@ Initializes Web API Routes. *pg.DB: Postgres Database Client. */ func Init(s *gin.Engine, db *pg.DB) { - Routes(s, db) + Routes(s, db) } type API struct { diff --git a/pkg/migrate/10_create_table_transaction_status.go b/pkg/migrate/10_create_table_transaction_status.go index 05b9d52..edb8232 100644 --- a/pkg/migrate/10_create_table_transaction_status.go +++ b/pkg/migrate/10_create_table_transaction_status.go @@ -20,7 +20,7 @@ Creates transaction_status table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableTransactionStatus(db pg.DB) error { +func CreateTableTransactionStatus(db *pg.Tx) error { models := []interface{}{ (*model.TransactionStatus)(nil), } diff --git a/pkg/migrate/11_populate_transaction_status.go b/pkg/migrate/11_populate_transaction_status.go index 85523ae..9ba32ec 100644 --- a/pkg/migrate/11_populate_transaction_status.go +++ b/pkg/migrate/11_populate_transaction_status.go @@ -18,7 +18,7 @@ Populates transaction_status table if it exists. Returns: error: Returns if there is an error with populating table */ -func PopulateTransactionStatus(db pg.DB) error { +func PopulateTransactionStatus(db *pg.Tx) error { completed := new(model.TransactionStatus) pending := new(model.TransactionStatus) deleted := new(model.TransactionStatus) diff --git a/pkg/migrate/1_create_table_api.go b/pkg/migrate/1_create_table_api.go index 2f6d20b..bb3e511 100644 --- a/pkg/migrate/1_create_table_api.go +++ b/pkg/migrate/1_create_table_api.go @@ -20,7 +20,7 @@ Creates api table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableApi(db pg.DB) error { +func CreateTableApi(db *pg.Tx) error { models := []interface{}{ (*model.ApiModel)(nil), diff --git a/pkg/migrate/2_create_table_users.go b/pkg/migrate/2_create_table_users.go index c3a9f23..2083512 100644 --- a/pkg/migrate/2_create_table_users.go +++ b/pkg/migrate/2_create_table_users.go @@ -20,7 +20,7 @@ Creates users table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableUsers(db pg.DB) error { +func CreateTableUsers(db *pg.Tx) error { models := []interface{}{ (*model.User)(nil), } diff --git a/pkg/migrate/3_create_table_wallets.go b/pkg/migrate/3_create_table_wallets.go index 7269224..c03f296 100644 --- a/pkg/migrate/3_create_table_wallets.go +++ b/pkg/migrate/3_create_table_wallets.go @@ -20,7 +20,7 @@ Creates wallets table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableWallets(db pg.DB) error { +func CreateTableWallets(db *pg.Tx) error { models := []interface{}{ (*model.Wallet)(nil), } diff --git a/pkg/migrate/4_create_table_transaction_types.go b/pkg/migrate/4_create_table_transaction_types.go index 691d7b8..1dd561e 100644 --- a/pkg/migrate/4_create_table_transaction_types.go +++ b/pkg/migrate/4_create_table_transaction_types.go @@ -20,7 +20,7 @@ Creates transaction_types table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableTransactionTypes(db pg.DB) error { +func CreateTableTransactionTypes(db *pg.Tx) error { models := []interface{}{ (*model.TransactionType)(nil), } diff --git a/pkg/migrate/5_create_table_transactions.go b/pkg/migrate/5_create_table_transactions.go index 3801db0..6cab613 100644 --- a/pkg/migrate/5_create_table_transactions.go +++ b/pkg/migrate/5_create_table_transactions.go @@ -20,7 +20,7 @@ Creates transactions table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableTransactions(db pg.DB) error { +func CreateTableTransactions(db *pg.Tx) error { models := []interface{}{ (*model.Transaction)(nil), } diff --git a/pkg/migrate/6_create_table_subscription_types.go b/pkg/migrate/6_create_table_subscription_types.go index 890b887..3dd306c 100644 --- a/pkg/migrate/6_create_table_subscription_types.go +++ b/pkg/migrate/6_create_table_subscription_types.go @@ -20,7 +20,7 @@ Creates subscription_types table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableSubscriptionTypes(db pg.DB) error { +func CreateTableSubscriptionTypes(db *pg.Tx) error { models := []interface{}{ (*model.SubscriptionType)(nil), } diff --git a/pkg/migrate/7_create_table_subscriptions.go b/pkg/migrate/7_create_table_subscriptions.go index b6efb90..9cc857a 100644 --- a/pkg/migrate/7_create_table_subscriptions.go +++ b/pkg/migrate/7_create_table_subscriptions.go @@ -19,7 +19,7 @@ Creates subscriptions table if it does not exist. Returns: error: Returns if there is an error with table creation */ -func CreateTableSubscriptions(db pg.DB) error { +func CreateTableSubscriptions(db *pg.Tx) error { models := []interface{}{ (*model.Subscription)(nil), } diff --git a/pkg/migrate/8_populate_subscription_types.go b/pkg/migrate/8_populate_subscription_types.go index 74801ab..eb52f0c 100644 --- a/pkg/migrate/8_populate_subscription_types.go +++ b/pkg/migrate/8_populate_subscription_types.go @@ -18,7 +18,7 @@ Populates subscription_types table if it exists. Returns: error: Returns if there is an error with populating table */ -func PopulateSubscriptionTypes(db pg.DB) error { +func PopulateSubscriptionTypes(db *pg.Tx) error { daily := new(model.SubscriptionType) weekly := new(model.SubscriptionType) monthly := new(model.SubscriptionType) diff --git a/pkg/migrate/9_populate_transaction_types.go b/pkg/migrate/9_populate_transaction_types.go index 52dbeb9..54bf46d 100644 --- a/pkg/migrate/9_populate_transaction_types.go +++ b/pkg/migrate/9_populate_transaction_types.go @@ -18,7 +18,7 @@ Populates transaction_types table if it exists. Returns: error: Returns if there is an error with populating table */ -func PopulateTransactionTypes(db pg.DB) error { +func PopulateTransactionTypes(db *pg.Tx) error { gain := new(model.TransactionType) expense := new(model.TransactionType) diff --git a/pkg/migrate/migrate.go b/pkg/migrate/migrate.go index e23444e..25b549b 100644 --- a/pkg/migrate/migrate.go +++ b/pkg/migrate/migrate.go @@ -1,6 +1,8 @@ package migrate import ( + "context" + "github.com/go-pg/pg/v10" ) @@ -8,10 +10,11 @@ import ( Start Starts database migration. - Args: - *pg.DB: Postgres database client - Returns: - error: Returns if there is an error with populating table + + Args: + *pg.DB: Postgres database client + Returns: + error: Returns if there is an error with populating table */ func Start(conn *pg.DB, version string) []error { migration001 := Migration{ @@ -21,28 +24,28 @@ func Start(conn *pg.DB, version string) []error { CreateTableUsers, CreateTableWallets, CreateTableTransactionTypes, - CreateTableTransactions, + PopulateTransactionTypes, CreateTableSubscriptionTypes, - CreateTableSubscriptions, + PopulateSubscriptionTypes, }, } migration002 := Migration{ Version: "002", Migrations: []interface{}{ - PopulateSubscriptionTypes, - PopulateTransactionTypes, + CreateTableTransactionStatus, }, } migration003 := Migration{ Version: "003", Migrations: []interface{}{ - CreateTableTransactionStatus, + PopulateTransactionStatus, }, } migration004 := Migration{ Version: "004", Migrations: []interface{}{ - PopulateTransactionStatus, + CreateTableSubscriptions, + CreateTableTransactions, }, } @@ -55,12 +58,18 @@ func Start(conn *pg.DB, version string) []error { var errors []error + ctx := context.Background() + + tx, _ := conn.BeginContext(ctx) + + defer tx.Rollback() + for _, migrationCol := range migrationsMap { if version != "" && version == migrationCol.Version || version == "" { for _, migration := range migrationCol.Migrations { - mgFunc, isFunc := migration.(func(pg.DB) error) + mgFunc, isFunc := migration.(func(*pg.Tx) error) if isFunc { - err := mgFunc(*conn) + err := mgFunc(tx) if err != nil { errors = append(errors, err) } @@ -69,6 +78,8 @@ func Start(conn *pg.DB, version string) []error { } } + tx.CommitContext(ctx) + return errors } diff --git a/pkg/utl/db/db.go b/pkg/utl/db/db.go index 982ac88..9a5544a 100644 --- a/pkg/utl/db/db.go +++ b/pkg/utl/db/db.go @@ -2,7 +2,6 @@ package db import ( "context" - "fmt" "log" "os" "wallet-api/pkg/utl/common" @@ -34,14 +33,18 @@ func (e *LoggerHook) AfterQuery(ctx context.Context, qe *pg.QueryEvent) error { func CreateConnection(ctx context.Context, dbUrl string) *pg.DB { opt, err := pg.ParseURL(dbUrl) common.CheckError(err) - file, err := os.OpenFile("querys.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + file, err := os.OpenFile("querys.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) common.CheckError(err) QueryLogger := log.New(file, "Query: ", log.Ldate|log.Ltime|log.Lshortfile) conn := pg.Connect(opt) + err = conn.Ping(ctx) + if err != nil { + log.Fatalln(err) + } loggerHook := LoggerHook{QueryLogger} conn.AddQueryHook(&loggerHook) db := conn.WithContext(ctx) - fmt.Println("Successfully connected!") + log.Printf("Successfully connected to %s!", dbUrl) return db } diff --git a/querys.txt b/querys.txt deleted file mode 100644 index 2c29039..0000000 --- a/querys.txt +++ /dev/null @@ -1,28 +0,0 @@ -Query: 2022/06/15 18:36:12 db.go:24: [83 69 76 69 67 84 32 34 117 115 101 114 115 34 46 34 105 100 34 44 32 34 117 115 101 114 115 34 46 34 100 97 116 101 95 99 114 101 97 116 101 100 34 44 32 34 117 115 101 114 115 34 46 34 100 97 116 101 95 117 112 100 97 116 101 100 34 44 32 34 117 115 101 114 115 34 46 34 105 115 95 97 99 116 105 118 101 34 44 32 34 117 115 101 114 115 34 46 34 117 115 101 114 110 97 109 101 34 44 32 34 117 115 101 114 115 34 46 34 112 97 115 115 119 111 114 100 34 44 32 34 117 115 101 114 115 34 46 34 101 109 97 105 108 34 32 70 82 79 77 32 34 117 115 101 114 115 34 32 65 83 32 34 117 115 101 114 115 34 32 87 72 69 82 69 32 40 34 101 109 97 105 108 34 32 61 32 39 39 41] -Query: 2022/06/15 18:36:20 db.go:24: [66 69 71 73 78] -Query: 2022/06/15 18:36:20 db.go:24: [83 69 76 69 67 84 32 34 117 115 101 114 115 34 46 34 105 100 34 44 32 34 117 115 101 114 115 34 46 34 100 97 116 101 95 99 114 101 97 116 101 100 34 44 32 34 117 115 101 114 115 34 46 34 100 97 116 101 95 117 112 100 97 116 101 100 34 44 32 34 117 115 101 114 115 34 46 34 105 115 95 97 99 116 105 118 101 34 44 32 34 117 115 101 114 115 34 46 34 117 115 101 114 110 97 109 101 34 44 32 34 117 115 101 114 115 34 46 34 112 97 115 115 119 111 114 100 34 44 32 34 117 115 101 114 115 34 46 34 101 109 97 105 108 34 32 70 82 79 77 32 34 117 115 101 114 115 34 32 65 83 32 34 117 115 101 114 115 34 32 87 72 69 82 69 32 40 34 117 115 101 114 110 97 109 101 34 32 61 32 39 121 117 114 109 97 39 41 32 79 82 32 40 34 101 109 97 105 108 34 32 61 32 39 39 41] -Query: 2022/06/15 18:36:20 db.go:22: ERROR #23502 null value in column "email" of relation "users" violates not-null constraint -Query: 2022/06/15 18:36:20 db.go:24: [67 79 77 77 73 84] -Query: 2022/06/15 18:36:20 db.go:22: pg: transaction has already been committed or rolled back -Query: 2022/06/15 18:37:07 db.go:24: [66 69 71 73 78] -Query: 2022/06/15 18:38:44 db.go:28: BEGIN -Query: 2022/06/15 18:38:44 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("username" = 'fjurma12@gmail.com') OR ("email" = '') -Query: 2022/06/15 18:38:44 db.go:22: ERROR #23502 null value in column "email" of relation "users" violates not-null constraint -Query: 2022/06/15 18:38:44 db.go:28: COMMIT -Query: 2022/06/15 18:38:44 db.go:22: pg: transaction has already been committed or rolled back -Query: 2022/06/15 18:38:49 db.go:28: BEGIN -Query: 2022/06/15 18:38:49 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("username" = '') OR ("email" = 'fjurma12@gmail.com') -Query: 2022/06/15 18:38:49 db.go:22: ERROR #23502 null value in column "username" of relation "users" violates not-null constraint -Query: 2022/06/15 18:38:49 db.go:28: COMMIT -Query: 2022/06/15 18:38:49 db.go:22: pg: transaction has already been committed or rolled back -Query: 2022/06/15 18:39:31 db.go:28: BEGIN -Query: 2022/06/15 18:39:31 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("username" = 'yurma') OR ("email" = 'fjurma12@gmail.com') -Query: 2022/06/15 18:39:31 db.go:28: INSERT INTO "users" ("id", "date_created", "date_updated", "is_active", "username", "password", "email") VALUES ('b2c8bb5e-fcb7-4e91-9c20-2441dd6a1422', '2022-06-15 16:39:31.6930018+00:00:00', '2022-06-15 16:39:31.6930018+00:00:00', TRUE, 'yurma', '$2a$10$Uf/PlQtyt6J671sdvDskm.6m5OTgMUPNQlaqyG8rUQamGnR3Zi2Z.', 'fjurma12@gmail.com') -Query: 2022/06/15 18:39:31 db.go:28: COMMIT -Query: 2022/06/15 18:39:31 db.go:22: pg: transaction has already been committed or rolled back -Query: 2022/06/15 18:40:26 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("email" = '') -Query: 2022/06/15 18:40:36 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("email" = 'fjurma12@gmail.com') -Query: 2022/06/15 18:42:30 db.go:28: BEGIN -Query: 2022/06/15 18:42:30 db.go:28: SELECT "users"."id", "users"."date_created", "users"."date_updated", "users"."is_active", "users"."username", "users"."password", "users"."email" FROM "users" AS "users" WHERE ("email" = 'fjurma12@gmail.com') -Query: 2022/06/15 18:42:30 db.go:28: COMMIT -Query: 2022/06/15 18:42:30 db.go:22: pg: transaction has already been committed or rolled back