update
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 12:47:10 +10:00
parent 18a2b7227e
commit 0d2f983eb3
14 changed files with 319 additions and 52 deletions

View File

@@ -1,11 +1,11 @@
package db
import (
"database/sql"
"embed"
"log/slog"
"vctp/db/queries"
"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"
)
@@ -13,7 +13,7 @@ import (
var migrations embed.FS
type Database interface {
DB() *sql.DB
DB() *sqlx.DB
Queries() *queries.Queries
Logger() *slog.Logger
Close() error
@@ -39,7 +39,7 @@ func Migrate(db Database) error {
panic(err)
}
if err := goose.Up(db.DB(), "migrations"); err != nil {
if err := goose.Up(db.DB().DB, "migrations"); err != nil {
panic(err)
}

View File

@@ -2,22 +2,28 @@ package db
import (
"database/sql"
"vctp/db/queries"
"log/slog"
"vctp/db/queries"
_ "github.com/tursodatabase/libsql-client-go/libsql"
//_ "github.com/tursodatabase/libsql-client-go/libsql"
"github.com/jmoiron/sqlx"
_ "modernc.org/sqlite"
)
type LocalDB struct {
logger *slog.Logger
db *sql.DB
db *sqlx.DB
queries *queries.Queries
}
type DB struct {
writeDB *sql.DB
readDB *sql.DB
}
var _ Database = (*LocalDB)(nil)
func (d *LocalDB) DB() *sql.DB {
func (d *LocalDB) DB() *sqlx.DB {
return d.db
}
@@ -34,9 +40,51 @@ func (d *LocalDB) Close() error {
}
func newLocalDB(logger *slog.Logger, path string) (*LocalDB, error) {
db, err := sql.Open("libsql", "file:"+path)
// TODO - work out if https://kerkour.com/sqlite-for-servers is possible without using sqlx
/*
writeDB, err := sql.Open("sqlite3", "file:"+path)
if err != nil {
logger.Error("can't create writedb connection", "error", err)
return nil, err
}
writeDB.SetMaxOpenConns(1)
readDB, err := sql.Open("sqlite3", "file:"+path)
if err != nil {
logger.Error("can't create readdb connection", "error", err)
return nil, err
}
readDB.SetMaxOpenConns(max(4, runtime.NumCPU()))
*/
//db, err := sql.Open("libsql", "file:"+path)
db, err := sqlx.Open("sqlite", "file:"+path)
if err != nil {
logger.Error("can't open database connection", "error", err)
return nil, err
}
db.SetMaxOpenConns(1)
// Execute PRAGMA commands
pragmas := []string{
"PRAGMA journal_mode = WAL;",
"PRAGMA busy_timeout = 5000;",
"PRAGMA synchronous = NORMAL;",
"PRAGMA cache_size = 1000000000;",
"PRAGMA foreign_keys = true;",
"PRAGMA temp_store = MEMORY;",
}
for _, pragma := range pragmas {
_, err := db.Exec(pragma)
if err != nil {
logger.Error("failed to execute pragma statement", "stmt", pragma, "error", err)
return nil, err
}
}
return &LocalDB{logger: logger, db: db, queries: queries.New(db)}, nil
}

31
db/queries/db.go Normal file
View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package queries
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

39
db/queries/models.go Normal file
View File

@@ -0,0 +1,39 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
package queries
import (
"database/sql"
)
type Inventory struct {
Iid sql.NullInt64
Name sql.NullString
Vcenter sql.NullString
VmId sql.NullString
EventKey sql.NullString
EventId sql.NullString
CreationTime sql.NullString
DeletionTime sql.NullString
ResourcePool sql.NullString
VmType sql.NullString
Datacenter sql.NullString
Cluster sql.NullString
Folder sql.NullString
ProvisionedDisk sql.NullFloat64
InitialVcpus sql.NullInt64
InitialRam sql.NullInt64
SrmPlaceholder sql.NullInt64
}
type Updates struct {
Uid sql.NullInt64
InventoryId sql.NullInt64
UpdateTime sql.NullString
UpdateType sql.NullString
NewVcpus sql.NullInt64
NewRam sql.NullInt64
NewResourcePool sql.NullString
}

View File

@@ -1,3 +1,15 @@
-- name: ListInventory :many
SELECT * FROM "Inventory"
ORDER BY "Name";
ORDER BY "Name";
-- name: GetInventoryByName :one
SELECT * FROM "Inventory"
WHERE "Name" = ? LIMIT 1;
-- name: CreateInventory :one
INSERT INTO "Inventory" (
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING *;

155
db/queries/query.sql.go Normal file
View File

@@ -0,0 +1,155 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: query.sql
package queries
import (
"context"
"database/sql"
)
const createInventory = `-- name: CreateInventory :one
INSERT INTO "Inventory" (
"Name", "Vcenter", "VmId", "EventKey", "EventId", "CreationTime", "ResourcePool", "VmType", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "InitialVcpus", "InitialRam", "SrmPlaceholder"
) VALUES(
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
RETURNING Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder
`
type CreateInventoryParams struct {
Name sql.NullString
Vcenter sql.NullString
VmId sql.NullString
EventKey sql.NullString
EventId sql.NullString
CreationTime sql.NullString
ResourcePool sql.NullString
VmType sql.NullString
Datacenter sql.NullString
Cluster sql.NullString
Folder sql.NullString
ProvisionedDisk sql.NullFloat64
InitialVcpus sql.NullInt64
InitialRam sql.NullInt64
SrmPlaceholder sql.NullInt64
}
func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams) (Inventory, error) {
row := q.db.QueryRowContext(ctx, createInventory,
arg.Name,
arg.Vcenter,
arg.VmId,
arg.EventKey,
arg.EventId,
arg.CreationTime,
arg.ResourcePool,
arg.VmType,
arg.Datacenter,
arg.Cluster,
arg.Folder,
arg.ProvisionedDisk,
arg.InitialVcpus,
arg.InitialRam,
arg.SrmPlaceholder,
)
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 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
`
func (q *Queries) GetInventoryByName(ctx context.Context, name sql.NullString) (Inventory, error) {
row := q.db.QueryRowContext(ctx, getInventoryByName, name)
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 listInventory = `-- name: ListInventory :many
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory"
ORDER BY "Name"
`
func (q *Queries) ListInventory(ctx context.Context) ([]Inventory, error) {
rows, err := q.db.QueryContext(ctx, listInventory)
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
}