migration script

This commit is contained in:
Ruidy 2024-02-24 21:18:56 +01:00
parent 9bcd8e611f
commit 0ccabe71b3
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 142 additions and 2 deletions

View file

@ -1,11 +1,43 @@
package config
type Host struct {
CustomerSeed int
PaymentMethods []string
Platforms []string
Items []HostItem
CustomerSeed int
}
type HostItem struct {
Name string
Price float64
}
func NewHost() *Host {
return &Host{
CustomerSeed: 286,
CustomerSeed: 286,
PaymentMethods: []string{"Card", "Cash", "Cheque", "Transfer"},
Platforms: []string{"Booking", "AirBnb", "TripAdvisor", "Other"},
Items: []HostItem{
{
Name: "T2",
Price: 59.0,
},
{
Name: "T3",
Price: 80.0,
},
{
Name: "Airport",
Price: 25.0,
},
{
Name: "Port",
Price: 20.0,
},
{
Name: "Taxes",
Price: 1.5,
},
},
}
}

View file

@ -34,5 +34,12 @@ func main() {
log.Fatalf("error migrating the database %s\n", err)
}
if os.Getenv("MIGRATE_DB") != "" {
err := MigrateDB(db)
if err != nil {
log.Fatalln(err)
}
}
server.New(booking.NewService(db), pdf.NewPdfService(), config.NewHost()).Start()
}

101
migrateDB.go Normal file
View file

@ -0,0 +1,101 @@
package main
import (
"fmt"
"log"
"time"
"github.com/rjNemo/rentease/config"
"github.com/rjNemo/rentease/internal/booking"
u "github.com/rjNemo/underscore"
"gorm.io/gorm"
)
type Invoice struct {
To time.Time
From time.Time
Platform string
Name string
PhoneNumber string `gorm:"column:phoneNumber"`
Room string
PaymentMethod string `gorm:"column:paymentMethod"`
Status string
Id int
Price float64 `gorm:"type:decimal(10,2)"`
PlatformFees float64 `gorm:"type:decimal(10,2);column:platformFees"`
CustomerNumber int `gorm:"column:customersNumber"`
WithTaxes bool `gorm:"column:withTaxes"`
}
func (Invoice) TableName() string {
return "Invoice"
}
var hc *config.Host = config.NewHost()
func MigrateDB(db *gorm.DB) error {
return db.Transaction(func(tx *gorm.DB) error {
invoices := make([]Invoice, 0)
if err := tx.Find(&invoices).Error; err != nil {
return err
}
// log.Println(invoices)
// - for each row in Invoice table,
for _, i := range invoices {
// create a booking strcut based on the invoice value, commit
fmt.Println(" ")
b := &booking.Booking{
From: i.From,
To: i.To,
PhoneNumber: i.PhoneNumber,
CustomerNumber: i.CustomerNumber,
Platform: i.Platform,
PlatformFees: i.PlatformFees,
Name: i.Name,
}
if err := tx.Create(b).Error; err != nil {
return err
}
log.Printf("%+v\n", b)
// take the new booking id and create a item struct based on the Invoice value, commit
item := &booking.Item{
BookingId: b.Id,
Item: i.Room,
Quantity: computeQty(i),
Price: priceFromConfig(i, hc),
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.Status,
}
if err := tx.Create(item).Error; err != nil {
return err
}
log.Printf("%+v\n", item)
if i.WithTaxes {
taxItem := &booking.Item{
BookingId: b.Id,
Item: "Taxes",
Quantity: computeQty(i) * i.CustomerNumber,
Price: 1.5,
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.Status,
}
if err := tx.Create(taxItem).Error; err != nil {
return err
}
log.Printf("%+v\n", taxItem)
}
}
return nil
})
}
func priceFromConfig(i Invoice, hc *config.Host) float64 {
itm, _ := u.Find(hc.Items, func(hi config.HostItem) bool {
return hi.Name == i.Room
})
return itm.Price
}
func computeQty(i Invoice) int {
return int(i.To.Sub(i.From) / 86400000000000)
}