diff --git a/.drone.yml b/.drone.yml index 1476f8f..e2d5e55 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,6 +3,24 @@ type: docker name: default 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 image: golang environment: @@ -14,8 +32,8 @@ steps: path: /shared commands: #- cp /shared/index.html ./www/ - - go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest - - sqlc generate + #- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest + #- sqlc generate - chmod +x .drone.sh - ./.drone.sh @@ -35,9 +53,29 @@ steps: overwrite: 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: - name: shared temp: {} -#- name: cache -# host: -# path: /var/lib/cache \ No newline at end of file +- name: cache + host: + path: /var/lib/cache \ No newline at end of file diff --git a/README.md b/README.md index f296176..81a2233 100644 --- a/README.md +++ b/README.md @@ -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 - [HTMX](https://htmx.org/) for HTML interaction - 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. Everything else uses the standard library. ## Structure +(Now out of date) ```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 your database operations. +Once `queries.sql` is updated, run `make generate-sql` to update the generated models + #### 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` +Create a new up/down migration file with this command ```shell 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 -``` - -This package can be easily update to use `sqlx` as well. - - ### Dist This is where your assets live. Any Javascript, images, or styling needs to go in the diff --git a/db/migrations/20240912231739_extend_updates.sql b/db/migrations/20240912231739_extend_updates.sql new file mode 100644 index 0000000..08fb42b --- /dev/null +++ b/db/migrations/20240912231739_extend_updates.sql @@ -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 diff --git a/db/queries/models.go b/db/queries/models.go index 1866bdb..35b29ae 100644 --- a/db/queries/models.go +++ b/db/queries/models.go @@ -36,4 +36,6 @@ type Updates struct { NewVcpus sql.NullInt64 NewRam sql.NullInt64 NewResourcePool sql.NullString + EventKey sql.NullString + EventId sql.NullString } diff --git a/db/queries/query.sql b/db/queries/query.sql index f71a2f0..10949f2 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -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 *; \ No newline at end of file diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index 26e9ca5..d09f797 100644 --- a/db/queries/query.sql.go +++ b/db/queries/query.sql.go @@ -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,