mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-23 04:09:15 +08:00
418 lines
11 KiB
Go
418 lines
11 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: query.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const countHosts = `-- name: CountHosts :one
|
|
SELECT count(*) FROM hosts WHERE deleted_at IS NULL AND status <> 'pending'
|
|
`
|
|
|
|
func (q *Queries) CountHosts(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countHosts)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createEnrollment = `-- name: CreateEnrollment :one
|
|
INSERT INTO hosts(user_id,name,architecture,version) VALUES($1,$2,$3,$4) RETURNING id
|
|
`
|
|
|
|
type CreateEnrollmentParams struct {
|
|
UserID string
|
|
Name string
|
|
Architecture string
|
|
Version string
|
|
}
|
|
|
|
func (q *Queries) CreateEnrollment(ctx context.Context, arg CreateEnrollmentParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, createEnrollment,
|
|
arg.UserID,
|
|
arg.Name,
|
|
arg.Architecture,
|
|
arg.Version,
|
|
)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const deleteHost = `-- name: DeleteHost :execrows
|
|
UPDATE hosts h SET status='revoked',deleted_at=now()
|
|
WHERE h.id=$1 AND h.deleted_at IS NULL AND NOT EXISTS (
|
|
SELECT 1 FROM environments e WHERE e.host_id=h.id
|
|
AND (e.archived_at IS NULL OR (h.status='active' AND h.last_seen_at>now()-interval '90 seconds'))
|
|
AND NOT(e.desired='deleted' AND e.phase='deleted' AND e.observed_generation=e.generation)
|
|
) AND NOT EXISTS(SELECT 1 FROM host_upgrades u WHERE u.host_id=h.id AND u.status IN ('pending','running'))
|
|
`
|
|
|
|
func (q *Queries) DeleteHost(ctx context.Context, id string) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteHost, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const disableHost = `-- name: DisableHost :exec
|
|
UPDATE hosts SET enabled=false WHERE id=$1
|
|
`
|
|
|
|
func (q *Queries) DisableHost(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, disableHost, id)
|
|
return err
|
|
}
|
|
|
|
const enrollHost = `-- name: EnrollHost :one
|
|
UPDATE hosts SET public_key=$1,status='active',activated_at=COALESCE(activated_at,now()),
|
|
architecture=$2,name=COALESCE(NULLIF(name,''),$3::text)
|
|
WHERE id=$4 AND deleted_at IS NULL AND status IN ('pending','active')
|
|
AND (public_key IS NULL OR public_key=$1) AND (public_key IS NULL OR architecture=$2) AND version=$5
|
|
RETURNING id
|
|
`
|
|
|
|
type EnrollHostParams struct {
|
|
PublicKey []byte
|
|
Architecture string
|
|
Name string
|
|
HostID string
|
|
Version string
|
|
}
|
|
|
|
func (q *Queries) EnrollHost(ctx context.Context, arg EnrollHostParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, enrollHost,
|
|
arg.PublicKey,
|
|
arg.Architecture,
|
|
arg.Name,
|
|
arg.HostID,
|
|
arg.Version,
|
|
)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const expireUpgrade = `-- name: ExpireUpgrade :exec
|
|
UPDATE host_upgrades SET status='failed',reason='upgrade_timeout',updated_at=now() WHERE host_id=$1 AND status IN ('pending','running') AND created_at<now()-interval '30 minutes'
|
|
`
|
|
|
|
func (q *Queries) ExpireUpgrade(ctx context.Context, hostID string) error {
|
|
_, err := q.db.Exec(ctx, expireUpgrade, hostID)
|
|
return err
|
|
}
|
|
|
|
const getUpgrade = `-- name: GetUpgrade :one
|
|
SELECT host_id,id,from_version,target_version AS version,installer_sha256,manifest_sha256,status,reason,created_at,updated_at
|
|
FROM host_upgrades WHERE host_id=$1 ORDER BY created_at DESC,id DESC LIMIT 1
|
|
`
|
|
|
|
type GetUpgradeRow struct {
|
|
HostID string
|
|
ID string
|
|
FromVersion string
|
|
Version string
|
|
InstallerSha256 string
|
|
ManifestSha256 string
|
|
Status string
|
|
Reason string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) GetUpgrade(ctx context.Context, hostID string) (GetUpgradeRow, error) {
|
|
row := q.db.QueryRow(ctx, getUpgrade, hostID)
|
|
var i GetUpgradeRow
|
|
err := row.Scan(
|
|
&i.HostID,
|
|
&i.ID,
|
|
&i.FromVersion,
|
|
&i.Version,
|
|
&i.InstallerSha256,
|
|
&i.ManifestSha256,
|
|
&i.Status,
|
|
&i.Reason,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const hostExists = `-- name: HostExists :one
|
|
SELECT EXISTS(SELECT 1 FROM hosts WHERE id=$1 AND deleted_at IS NULL)
|
|
`
|
|
|
|
func (q *Queries) HostExists(ctx context.Context, id string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, hostExists, id)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const hostKey = `-- name: HostKey :one
|
|
SELECT public_key FROM hosts WHERE id=$1 AND status='active' AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) HostKey(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, hostKey, id)
|
|
var public_key []byte
|
|
err := row.Scan(&public_key)
|
|
return public_key, err
|
|
}
|
|
|
|
const installationHost = `-- name: InstallationHost :one
|
|
SELECT id,version,status,public_key FROM hosts WHERE id=$1 AND deleted_at IS NULL AND status IN ('pending','active')
|
|
`
|
|
|
|
type InstallationHostRow struct {
|
|
ID string
|
|
Version string
|
|
Status string
|
|
PublicKey []byte
|
|
}
|
|
|
|
func (q *Queries) InstallationHost(ctx context.Context, id string) (InstallationHostRow, error) {
|
|
row := q.db.QueryRow(ctx, installationHost, id)
|
|
var i InstallationHostRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.PublicKey,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const legacyHostOccupied = `-- name: LegacyHostOccupied :one
|
|
SELECT EXISTS(SELECT 1 FROM environments WHERE host_id=$1 AND archived_at IS NULL
|
|
AND NOT(desired='deleted' AND phase='deleted' AND observed_generation=generation))
|
|
`
|
|
|
|
func (q *Queries) LegacyHostOccupied(ctx context.Context, hostID string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, legacyHostOccupied, hostID)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const listHosts = `-- name: ListHosts :many
|
|
SELECT id,user_id,name,architecture,version,status,activated_at,last_seen_at,report,created_at,enabled
|
|
FROM hosts WHERE deleted_at IS NULL AND status <> 'pending' ORDER BY created_at DESC,id DESC LIMIT $1 OFFSET $2
|
|
`
|
|
|
|
type ListHostsParams struct {
|
|
Limit int32
|
|
Offset int32
|
|
}
|
|
|
|
type ListHostsRow struct {
|
|
ID string
|
|
UserID string
|
|
Name string
|
|
Architecture string
|
|
Version string
|
|
Status string
|
|
ActivatedAt *time.Time
|
|
LastSeenAt *time.Time
|
|
Report []byte
|
|
CreatedAt time.Time
|
|
Enabled bool
|
|
}
|
|
|
|
func (q *Queries) ListHosts(ctx context.Context, arg ListHostsParams) ([]ListHostsRow, error) {
|
|
rows, err := q.db.Query(ctx, listHosts, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListHostsRow{}
|
|
for rows.Next() {
|
|
var i ListHostsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.Name,
|
|
&i.Architecture,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.ActivatedAt,
|
|
&i.LastSeenAt,
|
|
&i.Report,
|
|
&i.CreatedAt,
|
|
&i.Enabled,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockControl = `-- name: LockControl :exec
|
|
SELECT pg_advisory_xact_lock(129678,hashtext(current_schema() || ':environments'))
|
|
`
|
|
|
|
func (q *Queries) LockControl(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, lockControl)
|
|
return err
|
|
}
|
|
|
|
const recordHeartbeat = `-- name: RecordHeartbeat :execrows
|
|
UPDATE hosts SET last_seen_at=$1::timestamptz,report=$2,
|
|
name=COALESCE(NULLIF(name,''),$3::text),
|
|
version=COALESCE(NULLIF($2::jsonb->>'engine_version',''),version)
|
|
WHERE id=$4 AND status='active' AND deleted_at IS NULL AND (last_seen_at IS NULL OR last_seen_at<$1::timestamptz)
|
|
`
|
|
|
|
type RecordHeartbeatParams struct {
|
|
At time.Time
|
|
Report []byte
|
|
Candidate string
|
|
ID string
|
|
}
|
|
|
|
func (q *Queries) RecordHeartbeat(ctx context.Context, arg RecordHeartbeatParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, recordHeartbeat,
|
|
arg.At,
|
|
arg.Report,
|
|
arg.Candidate,
|
|
arg.ID,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const reportUpgrade = `-- name: ReportUpgrade :exec
|
|
UPDATE host_upgrades SET status=$1,reason=$2,updated_at=now() WHERE host_id=$3 AND id=$4 AND status IN ('pending','running')
|
|
`
|
|
|
|
type ReportUpgradeParams struct {
|
|
Status string
|
|
Reason string
|
|
HostID string
|
|
ID string
|
|
}
|
|
|
|
func (q *Queries) ReportUpgrade(ctx context.Context, arg ReportUpgradeParams) error {
|
|
_, err := q.db.Exec(ctx, reportUpgrade,
|
|
arg.Status,
|
|
arg.Reason,
|
|
arg.HostID,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const revokeHost = `-- name: RevokeHost :execrows
|
|
UPDATE hosts SET status='revoked' WHERE id=$1 AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokeHost(ctx context.Context, id string) (int64, error) {
|
|
result, err := q.db.Exec(ctx, revokeHost, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const revokePendingHost = `-- name: RevokePendingHost :execrows
|
|
UPDATE hosts SET status='revoked',deleted_at=now(),enabled=false WHERE id=$1 AND status='pending' AND public_key IS NULL AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) RevokePendingHost(ctx context.Context, id string) (int64, error) {
|
|
result, err := q.db.Exec(ctx, revokePendingHost, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const setUpgrade = `-- name: SetUpgrade :one
|
|
INSERT INTO host_upgrades(id,host_id,from_version,target_version,installer_sha256,manifest_sha256)
|
|
SELECT $1,h.id,h.version,$2,$3,$4 FROM hosts h WHERE h.id=$5
|
|
RETURNING host_id,id,from_version,target_version AS version,installer_sha256,manifest_sha256,status,reason,created_at,updated_at
|
|
`
|
|
|
|
type SetUpgradeParams struct {
|
|
ID string
|
|
Version string
|
|
InstallerSha256 string
|
|
ManifestSha256 string
|
|
HostID string
|
|
}
|
|
|
|
type SetUpgradeRow struct {
|
|
HostID string
|
|
ID string
|
|
FromVersion string
|
|
Version string
|
|
InstallerSha256 string
|
|
ManifestSha256 string
|
|
Status string
|
|
Reason string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) SetUpgrade(ctx context.Context, arg SetUpgradeParams) (SetUpgradeRow, error) {
|
|
row := q.db.QueryRow(ctx, setUpgrade,
|
|
arg.ID,
|
|
arg.Version,
|
|
arg.InstallerSha256,
|
|
arg.ManifestSha256,
|
|
arg.HostID,
|
|
)
|
|
var i SetUpgradeRow
|
|
err := row.Scan(
|
|
&i.HostID,
|
|
&i.ID,
|
|
&i.FromVersion,
|
|
&i.Version,
|
|
&i.InstallerSha256,
|
|
&i.ManifestSha256,
|
|
&i.Status,
|
|
&i.Reason,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upgradeHost = `-- name: UpgradeHost :one
|
|
SELECT id,architecture,version,status,last_seen_at,report FROM hosts WHERE id=$1 AND deleted_at IS NULL FOR UPDATE
|
|
`
|
|
|
|
type UpgradeHostRow struct {
|
|
ID string
|
|
Architecture string
|
|
Version string
|
|
Status string
|
|
LastSeenAt *time.Time
|
|
Report []byte
|
|
}
|
|
|
|
func (q *Queries) UpgradeHost(ctx context.Context, id string) (UpgradeHostRow, error) {
|
|
row := q.db.QueryRow(ctx, upgradeHost, id)
|
|
var i UpgradeHostRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Architecture,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.LastSeenAt,
|
|
&i.Report,
|
|
)
|
|
return i, err
|
|
}
|