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

@@ -3,6 +3,24 @@ type: docker
name: default name: default
steps: steps:
- name: restore-cache-with-filesystem
image: meltwater/drone-cache
pull: true
settings:
backend: "filesystem"
#debug: true
restore: true
cache_key: '{{ .Repo.Name }}_{{ arch }}_{{ os }}'
archive_format: "tar"
filesystem_cache_root: "/go"
local_root: /
mount:
- pkg.mod
- pkg.build
volumes:
- name: cache
path: /go
- name: build - name: build
image: golang image: golang
environment: environment:
@@ -14,8 +32,8 @@ steps:
path: /shared path: /shared
commands: commands:
#- cp /shared/index.html ./www/ #- cp /shared/index.html ./www/
- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest #- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
- sqlc generate #- sqlc generate
- chmod +x .drone.sh - chmod +x .drone.sh
- ./.drone.sh - ./.drone.sh
@@ -35,9 +53,29 @@ steps:
overwrite: true overwrite: true
verbose: true verbose: true
- name: rebuild-cache-with-filesystem
image: meltwater/drone-cache
pull: true
#when:
# event:
# - tag
settings:
backend: "filesystem"
#debug: true
rebuild: true
cache_key: '{{ .Repo.Name }}_{{ arch }}_{{ os }}'
archive_format: "tar"
filesystem_cache_root: "/go"
mount:
- pkg.mod
- pkg.build
volumes:
- name: cache
path: /go
volumes: volumes:
- name: shared - name: shared
temp: {} temp: {}
#- name: cache - name: cache
# host: host:
# path: /var/lib/cache path: /var/lib/cache

View File

@@ -28,13 +28,17 @@ A few different technologies are configured to help getting off the ground easie
- The script `upgrade_templ.sh` is available to make upgrading easier - The script `upgrade_templ.sh` is available to make upgrading easier
- [HTMX](https://htmx.org/) for HTML interaction - [HTMX](https://htmx.org/) for HTML interaction
- The script `upgrade_htmx.sh` is available to make upgrading easier - The script `upgrade_htmx.sh` is available to make upgrading easier
- [goose](https://github.com/pressly/goose) for DB migrations
- [golang migrate](https://github.com/golang-migrate/migrate) for DB migrations, TODO replace with [goose](https://github.com/pressly/goose)
Technologies we're no longer using:
- [golang migrate](https://github.com/golang-migrate/migrate) for DB migrations
- [playwright-go](https://github.com/playwright-community/playwright-go) for E2E testing. - [playwright-go](https://github.com/playwright-community/playwright-go) for E2E testing.
Everything else uses the standard library. Everything else uses the standard library.
## Structure ## Structure
(Now out of date)
```text ```text
. .
@@ -97,28 +101,18 @@ This is where `templ` files live. Anything you want to render to the user goes h
This is the directory that `sqlc` generates to. Update `queries.sql` to build This is the directory that `sqlc` generates to. Update `queries.sql` to build
your database operations. your database operations.
Once `queries.sql` is updated, run `make generate-sql` to update the generated models
#### DB Migrations #### DB Migrations
This project now uses [goose](https://github.com/pressly/goose) for DB migrations. This project now uses [goose](https://github.com/pressly/goose) for DB migrations.
Install via `brew install goose` on a mac, or install via golang with command `go install github.com/pressly/goose/v3/cmd/goose@latest` Install via `brew install goose` on a mac, or install via golang with command `go install github.com/pressly/goose/v3/cmd/goose@latest`
Create a new up/down migration file with this command
```shell ```shell
goose -dir db/migrations sqlite3 ./db.sqlite3 create init sql goose -dir db/migrations sqlite3 ./db.sqlite3 create init sql
``` ```
#### Deprecated
This project no longer uses [golang migrate](https://github.com/golang-migrate/migrate) for DB
migrations. `sqlc` uses the `db/migrations` directory to generating DB tables. Call
`db.Migrate(..)` to automatically migrate your database to the latest version. To add migration
call the following command,
```shell
migrate create -ext sql -dir db/migrations <name of migration>
```
This package can be easily update to use `sqlx` as well.
### Dist ### Dist
This is where your assets live. Any Javascript, images, or styling needs to go in the This is where your assets live. Any Javascript, images, or styling needs to go in the

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 NewVcpus sql.NullInt64
NewRam sql.NullInt64 NewRam sql.NullInt64
NewResourcePool sql.NullString NewResourcePool sql.NullString
EventKey sql.NullString
EventId sql.NullString
} }

View File

@@ -2,9 +2,17 @@
SELECT * FROM "Inventory" SELECT * FROM "Inventory"
ORDER BY "Name"; ORDER BY "Name";
-- name: GetInventoryByName :one -- name: GetInventoryByName :many
SELECT * FROM "Inventory" 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 -- name: CreateInventory :one
INSERT INTO "Inventory" ( INSERT INTO "Inventory" (
@@ -13,3 +21,11 @@ INSERT INTO "Inventory" (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
) )
RETURNING *; 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 return i, err
} }
const getInventoryByName = `-- name: GetInventoryByName :one const createUpdate = `-- name: CreateUpdate :one
SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder FROM "Inventory" INSERT INTO "Updates" (
WHERE "Name" = ? LIMIT 1 "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) { type CreateUpdateParams struct {
row := q.db.QueryRowContext(ctx, getInventoryByName, name) 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 var i Inventory
err := row.Scan( err := row.Scan(
&i.Iid, &i.Iid,