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

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
}