mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fix migration
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user