fix migration

This commit is contained in:
Fran Jurmanović
2023-04-13 14:35:36 +02:00
parent e1f9de58cd
commit 062bb74221
21 changed files with 80 additions and 87 deletions

6
.gitignore vendored
View File

@@ -30,7 +30,7 @@ _testmain.go
*.test
*.prof
.Dockerfile
# .Dockerfile
logs.txt
querys.txt
logs.log
querys.log

View File

@@ -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:
- /

View File

@@ -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"]

View File

@@ -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)

27
docker-compose.yml Normal file
View File

@@ -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:

View File

View File

@@ -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 {

View File

@@ -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),
}

View File

@@ -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)

View File

@@ -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),

View File

@@ -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),
}

View File

@@ -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),
}

View File

@@ -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),
}

View File

@@ -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),
}

View File

@@ -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),
}

View File

@@ -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),
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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] <nil>
Query: 2022/06/15 18:36:20 db.go:24: [66 69 71 73 78] <nil>
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] <nil>
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] <nil>
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] <nil>
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