Remove GORM PoC; it's used in Flamenco Manager now anyway
This commit is contained in:
parent
0f8bacd2e5
commit
860ad168a6
4
Makefile
4
Makefile
@ -32,7 +32,6 @@ application: ${RESOURCES}
|
||||
go generate ${PKG}/...
|
||||
go build -v ${BUILD_FLAGS} ${PKG}/cmd/flamenco-manager-poc
|
||||
go build -v ${BUILD_FLAGS} ${PKG}/cmd/flamenco-worker-poc
|
||||
go build -v ${BUILD_FLAGS} ${PKG}/cmd/psql-gorm-poc
|
||||
|
||||
# resource.syso: resource/thermogui.ico resource/versioninfo.json
|
||||
# goversioninfo -icon=resource/thermogui.ico -64 resource/versioninfo.json
|
||||
@ -72,7 +71,8 @@ clean:
|
||||
|
||||
# static: vet lint resource.syso
|
||||
static: vet lint
|
||||
CGO_ENABLED=0 go build -v -o psql-gorm-poc-static -tags netgo -ldflags="-extldflags \"-static\" -w -s ${LDFLAGS}" ${PKG}/cmd/psql-gorm-poc
|
||||
CGO_ENABLED=0 go build -v -o flamenco-manager-poc-static -tags netgo -ldflags="-extldflags \"-static\" -w -s ${LDFLAGS}" ${PKG}/cmd/flamenco-manager-poc
|
||||
CGO_ENABLED=0 go build -v -o flamenco-worker-poc-static -tags netgo -ldflags="-extldflags \"-static\" -w -s ${LDFLAGS}" ${PKG}/cmd/flamenco-worker-poc
|
||||
|
||||
.gitlabAccessToken:
|
||||
$(error gitlabAccessToken does not exist, visit Visit https://gitlab.com/profile/personal_access_tokens, create a Personal Access Token with API access then save it to the file .gitlabAccessToken)
|
||||
|
@ -1,91 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-colorable"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/blender/flamenco-ng-poc/internal/appinfo"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
output := zerolog.ConsoleWriter{Out: colorable.NewColorableStdout(), TimeFormat: time.RFC3339}
|
||||
log.Logger = log.Output(output)
|
||||
log.Info().Str("version", appinfo.ApplicationVersion).Msgf("starting %v GORM test", appinfo.ApplicationName)
|
||||
|
||||
dsn := "host=localhost user=flamenco password=flamenco dbname=flamenco TimeZone=Europe/Amsterdam"
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to connect database")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
if err := db.AutoMigrate(&Dude{}); err != nil {
|
||||
log.Panic().Err(err).Msg("failed to automigrate database")
|
||||
}
|
||||
|
||||
var dude Dude
|
||||
|
||||
db.Transaction(func(tx *gorm.DB) error {
|
||||
// Find pre-existing
|
||||
findResult := tx.First(&dude, "metadata ->> 'name' = ?", "Daš D°°D")
|
||||
switch findResult.Error {
|
||||
case gorm.ErrRecordNotFound:
|
||||
// Create if not found
|
||||
dude = Dude{
|
||||
Name: "Dude",
|
||||
Email: "the@dude.nl",
|
||||
Metadata: make(map[string]interface{}),
|
||||
}
|
||||
dude.Metadata["name"] = "Daš D°°D"
|
||||
dude.Metadata["integer"] = 47
|
||||
dude.Metadata["float"] = 47.327
|
||||
|
||||
log.Info().Interface("data", dude).Msg("the data pre-insert")
|
||||
|
||||
createResult := tx.Create(&dude)
|
||||
if createResult.Error != nil {
|
||||
log.Error().Err(createResult.Error).Msg("failed to insert dude")
|
||||
return createResult.Error
|
||||
}
|
||||
|
||||
log.Info().Interface("data", dude).Msg("the data post-insert")
|
||||
case nil:
|
||||
log.Info().Interface("dude", dude).Msg("the found dude")
|
||||
default:
|
||||
log.Error().Err(findResult.Error).Msg("failed to fetch dude")
|
||||
return findResult.Error
|
||||
}
|
||||
|
||||
// Update
|
||||
var theInt int
|
||||
switch v := dude.Metadata["integer"].(type) {
|
||||
case float64:
|
||||
theInt = int(v)
|
||||
case int:
|
||||
theInt = v
|
||||
default:
|
||||
log.Panic().Interface("value", v).Msg("unexpected type in JSON")
|
||||
}
|
||||
|
||||
dude.Metadata["integer"] = theInt + 1
|
||||
|
||||
tx.Model(&dude).Update("metadata", dude.Metadata)
|
||||
if tx.Error != nil {
|
||||
log.Panic().Err(tx.Error).Msg("failed to update dude")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// Fetch again
|
||||
var newDude Dude
|
||||
tx := db.First(&newDude, dude.ID)
|
||||
if tx.Error != nil {
|
||||
log.Panic().Err(tx.Error).Msg("failed to re-fetch dude")
|
||||
}
|
||||
log.Info().Interface("newDude", newDude).Msg("the updated dude")
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Dude struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"type:varchar(50)" json:"name"`
|
||||
Email string `gorm:"type:varchar(50)" json:"email"`
|
||||
Metadata JSONB `gorm:"type:jsonb" json:"metadata"`
|
||||
}
|
||||
|
||||
// JSONB Interface for JSONB Field of Dude Table
|
||||
type JSONB map[string]interface{}
|
||||
|
||||
// Value Marshal
|
||||
func (a JSONB) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
}
|
||||
|
||||
// Scan Unmarshal
|
||||
func (a *JSONB) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(b, &a)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user