sql updates
Some checks are pending
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 passing

This commit is contained in:
2024-09-13 09:25:12 +10:00
parent 3f2a67cf7b
commit f0278388cb
6 changed files with 209 additions and 26 deletions

View File

@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE "Updates" ADD COLUMN EventKey TEXT;
ALTER TABLE "Updates" ADD COLUMN EventId TEXT;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
ALTER TABLE "Updates" DROP COLUMN EventKey;
ALTER TABLE "Updates" DROP COLUMN EventId;
-- +goose StatementEnd

View File

@@ -36,4 +36,6 @@ type Updates struct {
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString
EventKey sql.NullString
EventId sql.NullString
}

View File

@@ -2,9 +2,17 @@
SELECT * FROM "Inventory"
ORDER BY "Name";
-- name: GetInventoryByName :one
-- name: GetInventoryByName :many
SELECT * FROM "Inventory"
WHERE "Name" = ? LIMIT 1;
WHERE "Name" = ?;
-- name: GetInventoryVmId :one
SELECT * FROM "Inventory"
WHERE "VmId" = ? LIMIT 1;
-- name: GetInventoryEventId :one
SELECT * FROM "Inventory"
WHERE "EventId" = ? LIMIT 1;
-- name: CreateInventory :one
INSERT INTO "Inventory" (
@@ -12,4 +20,12 @@ INSERT INTO "Inventory" (
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;
-- name: CreateUpdate :one
INSERT INTO "Updates" (
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

View File

@@ -78,13 +78,135 @@ func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams
return i, err
}
const getInventoryByName = `-- name: GetInventoryByName :one
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
WHERE "Name" = ? LIMIT 1
const createUpdate = `-- name: CreateUpdate :one
INSERT INTO "Updates" (
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId
`
func (q *Queries) GetInventoryByName(ctx context.Context, name string) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryByName, name)
type CreateUpdateParams struct {
InventoryId sql.NullInt64
EventKey sql.NullString
EventId sql.NullString
UpdateTime sql.NullInt64
UpdateType string
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString
}
func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) {
row := q.db.QueryRowContext(ctx, createUpdate,
arg.InventoryId,
arg.EventKey,
arg.EventId,
arg.UpdateTime,
arg.UpdateType,
arg.NewVcpus,
arg.NewRam,
arg.NewResourcePool,
)
var i Updates
err := row.Scan(
&i.Uid,
&i.InventoryId,
&i.UpdateTime,
&i.UpdateType,
&i.NewVcpus,
&i.NewRam,
&i.NewResourcePool,
&i.EventKey,
&i.EventId,
)
return i, err
}
const getInventoryByName = `-- name: GetInventoryByName :many
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
WHERE "Name" = ?
`
func (q *Queries) GetInventoryByName(ctx context.Context, name string) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, getInventoryByName, name)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Inventory
for rows.Next() {
var i Inventory
if err := rows.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.EventId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.SrmPlaceholder,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getInventoryEventId = `-- name: GetInventoryEventId :one
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
WHERE "EventId" = ? LIMIT 1
`
func (q *Queries) GetInventoryEventId(ctx context.Context, eventid sql.NullString) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryEventId, eventid)
var i Inventory
err := row.Scan(
&i.Iid,
&i.Name,
&i.Vcenter,
&i.VmId,
&i.EventKey,
&i.EventId,
&i.CreationTime,
&i.DeletionTime,
&i.ResourcePool,
&i.VmType,
&i.Datacenter,
&i.Cluster,
&i.Folder,
&i.ProvisionedDisk,
&i.InitialVcpus,
&i.InitialRam,
&i.SrmPlaceholder,
)
return i, err
}
const getInventoryVmId = `-- name: GetInventoryVmId :one
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
WHERE "VmId" = ? LIMIT 1
`
func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryVmId, vmid)
var i Inventory
err := row.Scan(
&i.Iid,