database insert is working
Some checks failed
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-09-12 15:03:24 +10:00
parent 0d2f983eb3
commit 6b285e55b8
9 changed files with 165 additions and 28 deletions

View File

@@ -1,8 +1,10 @@
package db
import (
"database/sql"
"embed"
"log/slog"
"reflect"
"vctp/db/queries"
"github.com/jmoiron/sqlx"
@@ -66,3 +68,33 @@ func Migrate(db Database) error {
return nil
}
// ConvertToSQLParams is a utility function that generically converts a struct to a corresponding sqlc-generated struct
func ConvertToSQLParams(input interface{}, output interface{}) {
inputVal := reflect.ValueOf(input).Elem()
outputVal := reflect.ValueOf(output).Elem()
for i := 0; i < outputVal.NumField(); i++ {
outputField := outputVal.Field(i)
inputField := inputVal.FieldByName(outputVal.Type().Field(i).Name)
if !inputField.IsValid() || !outputField.CanSet() {
continue
}
switch outputField.Type() {
case reflect.TypeOf(sql.NullString{}):
if inputField.Kind() == reflect.Ptr && inputField.IsNil() {
outputField.Set(reflect.ValueOf(sql.NullString{Valid: false}))
} else {
outputField.Set(reflect.ValueOf(sql.NullString{String: inputField.String(), Valid: true}))
}
case reflect.TypeOf(sql.NullInt64{}):
if inputField.Int() == 0 {
outputField.Set(reflect.ValueOf(sql.NullInt64{Valid: false}))
} else {
outputField.Set(reflect.ValueOf(sql.NullInt64{Int64: inputField.Int(), Valid: true}))
}
}
}
}

View File

@@ -2,13 +2,13 @@
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS "Inventory" (
"Iid" INTEGER UNIQUE,
"Name" TEXT,
"Vcenter" TEXT,
"Name" TEXT NOT NULL,
"Vcenter" TEXT NOT NULL,
"VmId" TEXT,
"EventKey" TEXT,
"EventId" TEXT,
"CreationTime" TEXT,
"DeletionTime" TEXT,
"CreationTime" INTEGER,
"DeletionTime" INTEGER,
"ResourcePool" TEXT,
"VmType" TEXT,
"Datacenter" TEXT,
@@ -23,8 +23,8 @@ CREATE TABLE IF NOT EXISTS "Inventory" (
CREATE TABLE IF NOT EXISTS "Updates" (
"Uid" INTEGER UNIQUE,
"InventoryId" INTEGER,
"UpdateTime" TEXT,
"UpdateType" TEXT,
"UpdateTime" INTEGER,
"UpdateType" TEXT NOT NULL,
"NewVcpus" INTEGER,
"NewRam" INTEGER,
"NewResourcePool" TEXT

View File

@@ -10,13 +10,13 @@ import (
type Inventory struct {
Iid sql.NullInt64
Name sql.NullString
Vcenter sql.NullString
Name string
Vcenter string
VmId sql.NullString
EventKey sql.NullString
EventId sql.NullString
CreationTime sql.NullString
DeletionTime sql.NullString
CreationTime sql.NullInt64
DeletionTime sql.NullInt64
ResourcePool sql.NullString
VmType sql.NullString
Datacenter sql.NullString
@@ -31,8 +31,8 @@ type Inventory struct {
type Updates struct {
Uid sql.NullInt64
InventoryId sql.NullInt64
UpdateTime sql.NullString
UpdateType sql.NullString
UpdateTime sql.NullInt64
UpdateType string
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString

View File

@@ -20,12 +20,12 @@ RETURNING Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTim
`
type CreateInventoryParams struct {
Name sql.NullString
Vcenter sql.NullString
Name string
Vcenter string
VmId sql.NullString
EventKey sql.NullString
EventId sql.NullString
CreationTime sql.NullString
CreationTime sql.NullInt64
ResourcePool sql.NullString
VmType sql.NullString
Datacenter sql.NullString
@@ -83,7 +83,7 @@ SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime,
WHERE "Name" = ? LIMIT 1
`
func (q *Queries) GetInventoryByName(ctx context.Context, name sql.NullString) (Inventory, error) {
func (q *Queries) GetInventoryByName(ctx context.Context, name string) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryByName, name)
var i Inventory
err := row.Scan(