structure appears to work
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 Build is failing

This commit is contained in:
2024-09-12 11:59:41 +10:00
parent eb10ca9ca3
commit 18a2b7227e
25 changed files with 841 additions and 125 deletions

View File

@@ -3,13 +3,10 @@ package db
import (
"database/sql"
"embed"
"fmt"
"vctp/db/queries"
"log/slog"
"vctp/db/queries"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/pressly/goose/v3"
)
//go:embed migrations/*.sql
@@ -35,21 +32,37 @@ func New(logger *slog.Logger, url string) (Database, error) {
// Migrate runs the migrations on the database. Assumes the database is SQLite.
func Migrate(db Database) error {
driver, err := sqlite3.WithInstance(db.DB(), &sqlite3.Config{})
if err != nil {
return fmt.Errorf("failed to create database driver: %w", err)
goose.SetBaseFS(migrations)
if err := goose.SetDialect("sqlite3"); err != nil {
panic(err)
}
iofsDriver, err := iofs.New(migrations, "migrations")
if err != nil {
return fmt.Errorf("failed to create iofs: %w", err)
}
defer iofsDriver.Close()
m, err := migrate.NewWithInstance("iofs", iofsDriver, "sqlite3", driver)
if err != nil {
return fmt.Errorf("failed to create migration: %w", err)
if err := goose.Up(db.DB(), "migrations"); err != nil {
panic(err)
}
return m.Up()
// TODO - replace with goose
/*
driver, err := sqlite3.WithInstance(db.DB(), &sqlite3.Config{})
if err != nil {
return fmt.Errorf("failed to create database driver: %w", err)
}
iofsDriver, err := iofs.New(migrations, "migrations")
if err != nil {
return fmt.Errorf("failed to create iofs: %w", err)
}
defer iofsDriver.Close()
m, err := migrate.NewWithInstance("iofs", iofsDriver, "sqlite3", driver)
if err != nil {
return fmt.Errorf("failed to create migration: %w", err)
}
return m.Up()
*/
return nil
}

View File

@@ -1,7 +0,0 @@
CREATE TABLE IF NOT EXISTS authors (
id INTEGER PRIMARY KEY,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
name TEXT NOT NULL,
bio TEXT
);

View File

@@ -0,0 +1,38 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS "Inventory" (
"Iid" INTEGER UNIQUE,
"Name" TEXT,
"Vcenter" TEXT,
"VmId" TEXT,
"EventKey" TEXT,
"EventId" TEXT,
"CreationTime" TEXT,
"DeletionTime" TEXT,
"ResourcePool" TEXT,
"VmType" TEXT,
"Datacenter" TEXT,
"Cluster" TEXT,
"Folder" TEXT,
"ProvisionedDisk" REAL,
"InitialVcpus" INTEGER,
"InitialRam" INTEGER,
"SrmPlaceholder" INTEGER
);
CREATE TABLE IF NOT EXISTS "Updates" (
"Uid" INTEGER UNIQUE,
"InventoryId" INTEGER,
"UpdateTime" TEXT,
"UpdateType" TEXT,
"NewVcpus" INTEGER,
"NewRam" INTEGER,
"NewResourcePool" TEXT
)
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE "Inventory";
DROP TABLE "Updates";
-- +goose StatementEnd

View File

@@ -1,25 +1,3 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = ? LIMIT 1;
-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)
RETURNING *;
-- name: UpdateAuthor :exec
UPDATE authors
SET name = ?,
bio = ?
WHERE id = ?;
-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = ?;
-- name: ListInventory :many
SELECT * FROM "Inventory"
ORDER BY "Name";

25
db/queries/query.txt Normal file
View File

@@ -0,0 +1,25 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = ? LIMIT 1;
-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)
RETURNING *;
-- name: UpdateAuthor :exec
UPDATE authors
SET name = ?,
bio = ?
WHERE id = ?;
-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = ?;