mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
upgraded migrations and context usage
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
package migrations
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type ApiMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
func CreateTableApi(db pg.DB) error {
|
||||
|
||||
func (am *ApiMigration) Create() error {
|
||||
models := []interface{}{
|
||||
(*models.ApiModel)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1,25 +1,22 @@
|
||||
package migrations
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type UsersMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *UsersMigration) Create() error {
|
||||
func CreateTableUsers(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.User)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1,25 +1,21 @@
|
||||
package migrations
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type WalletsMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *WalletsMigration) Create() error {
|
||||
func CreateTableWallets(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.Wallet)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
30
pkg/migrate/4_create_table_transaction_types.go
Normal file
30
pkg/migrate/4_create_table_transaction_types.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
func CreateTableTransactionTypes(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.TransactionType)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,25 +1,22 @@
|
||||
package migrations
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type TransactionsMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *TransactionsMigration) Create() error {
|
||||
func CreateTableTransactions(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.Transaction)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
30
pkg/migrate/6_create_table_subscription_types.go
Normal file
30
pkg/migrate/6_create_table_subscription_types.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
func CreateTableSubscriptionTypes(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.SubscriptionType)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,25 +1,20 @@
|
||||
package migrations
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
)
|
||||
|
||||
type SubscriptionsMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *SubscriptionsMigration) Create() error {
|
||||
func CreateTableSubscriptions(db pg.DB) error {
|
||||
models := []interface{}{
|
||||
(*models.Subscription)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
39
pkg/migrate/8_populate_subscription_types.go
Normal file
39
pkg/migrate/8_populate_subscription_types.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"github.com/go-pg/pg/v10"
|
||||
"wallet-api/pkg/models"
|
||||
)
|
||||
|
||||
func PopulateSubscriptionTypes(db pg.DB) error {
|
||||
daily := new(models.SubscriptionType)
|
||||
weekly := new(models.SubscriptionType)
|
||||
monthly := new(models.SubscriptionType)
|
||||
yearly := new(models.SubscriptionType)
|
||||
|
||||
daily.Init()
|
||||
daily.Name = "Daily"
|
||||
daily.Type = "daily"
|
||||
|
||||
weekly.Init()
|
||||
weekly.Name = "Weekly"
|
||||
weekly.Type = "weekly"
|
||||
|
||||
monthly.Init()
|
||||
monthly.Name = "Monthly"
|
||||
monthly.Type = "monthly"
|
||||
|
||||
yearly.Init()
|
||||
yearly.Name = "Yearly"
|
||||
yearly.Type = "yearly"
|
||||
|
||||
_, err := db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
|
||||
|
||||
_, err = db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
|
||||
|
||||
_, err = db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
|
||||
|
||||
_, err = db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
25
pkg/migrate/9_populate_transaction_types.go
Normal file
25
pkg/migrate/9_populate_transaction_types.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"github.com/go-pg/pg/v10"
|
||||
"wallet-api/pkg/models"
|
||||
)
|
||||
|
||||
func PopulateTransactionTypes(db pg.DB) error {
|
||||
gain := new(models.TransactionType)
|
||||
expense := new(models.TransactionType)
|
||||
|
||||
gain.Init()
|
||||
gain.Name = "Gain"
|
||||
gain.Type = "gain"
|
||||
|
||||
expense.Init()
|
||||
expense.Name = "Expense"
|
||||
expense.Type = "expense"
|
||||
|
||||
_, err := db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
|
||||
|
||||
_, err = db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -1,30 +1,49 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/migrate/migrations"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func Start(conn *pg.DB) error {
|
||||
apiMigration := migrations.ApiMigration{Db: conn}
|
||||
usersMigration := migrations.UsersMigration{Db: conn}
|
||||
walletsMigration := migrations.WalletsMigration{Db: conn}
|
||||
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
|
||||
transactionsMigration := migrations.TransactionsMigration{Db: conn}
|
||||
subscriptionTypesMigration := migrations.SubscriptionTypesMigration{Db: conn}
|
||||
subscriptionsMigration := migrations.SubscriptionsMigration{Db: conn}
|
||||
func Start(conn *pg.DB, version string) {
|
||||
migration001 := Migration{
|
||||
Version: "001",
|
||||
Migrations: []interface{}{
|
||||
CreateTableApi,
|
||||
CreateTableUsers,
|
||||
CreateTableWallets,
|
||||
CreateTableTransactionTypes,
|
||||
CreateTableTransactions,
|
||||
CreateTableSubscriptionTypes,
|
||||
CreateTableSubscriptions,
|
||||
},
|
||||
}
|
||||
migration002 := Migration{
|
||||
Version: "002",
|
||||
Migrations: []interface{}{
|
||||
PopulateSubscriptionTypes,
|
||||
PopulateTransactionTypes,
|
||||
},
|
||||
}
|
||||
|
||||
err := apiMigration.Create()
|
||||
err = usersMigration.Create()
|
||||
err = walletsMigration.Create()
|
||||
err = transactionTypesMigration.Create()
|
||||
err = subscriptionTypesMigration.Create()
|
||||
err = subscriptionsMigration.Create()
|
||||
err = transactionsMigration.Create()
|
||||
migrationsMap := []Migration{
|
||||
migration001,
|
||||
migration002,
|
||||
}
|
||||
|
||||
err = subscriptionTypesMigration.Populate()
|
||||
err = transactionTypesMigration.Populate()
|
||||
|
||||
return err
|
||||
for _, migrationCol := range migrationsMap {
|
||||
if version != "" && version == migrationCol.Version || version == "" {
|
||||
for _, migration := range migrationCol.Migrations {
|
||||
mgFunc, isFunc := migration.(func(pg.DB) error)
|
||||
if isFunc {
|
||||
mgFunc(*conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Migration struct {
|
||||
Version string
|
||||
Migrations []interface{}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type SubscriptionTypesMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *SubscriptionTypesMigration) Create() error {
|
||||
models := []interface{}{
|
||||
(*models.SubscriptionType)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (am *SubscriptionTypesMigration) Populate() error {
|
||||
daily := new(models.SubscriptionType)
|
||||
weekly := new(models.SubscriptionType)
|
||||
monthly := new(models.SubscriptionType)
|
||||
yearly := new(models.SubscriptionType)
|
||||
|
||||
daily.Init()
|
||||
daily.Name = "Daily"
|
||||
daily.Type = "daily"
|
||||
|
||||
weekly.Init()
|
||||
weekly.Name = "Weekly"
|
||||
weekly.Type = "weekly"
|
||||
|
||||
monthly.Init()
|
||||
monthly.Name = "Monthly"
|
||||
monthly.Type = "monthly"
|
||||
|
||||
yearly.Init()
|
||||
yearly.Name = "Yearly"
|
||||
yearly.Type = "yearly"
|
||||
|
||||
_, err := am.Db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type TransactionTypesMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *TransactionTypesMigration) Create() error {
|
||||
models := []interface{}{
|
||||
(*models.TransactionType)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (am *TransactionTypesMigration) Populate() error {
|
||||
gain := new(models.TransactionType)
|
||||
expense := new(models.TransactionType)
|
||||
|
||||
gain.Init()
|
||||
gain.Name = "Gain"
|
||||
gain.Type = "gain"
|
||||
|
||||
expense.Init()
|
||||
expense.Name = "Expense"
|
||||
expense.Type = "expense"
|
||||
|
||||
_, err := am.Db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
|
||||
|
||||
_, err = am.Db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user