mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 07:41:27 +00:00
Tranche 6 — 15 of 17 raw sites in cron.go. Adds cron_jobs and cron_executions to schema.sql and a full query set: job CRUD, owner joins (via project and via service), execution list/insert/finish, due-job scan for the scheduler, run bookkeeping, and retention trim. The filtered list query and the sparse UPDATE builder stay raw — both are genuinely dynamic. Go models keep string IDs; cronJobFrom and cronExecutionFrom map uuid/null types at the boundary. TestCronLiveInvariants covers insert/read, both owner joins, the trigger read, execution lifecycle, retention trim, and delete on live Postgres. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
333 lines
8.7 KiB
Go
333 lines
8.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: cron.sql
|
|
|
|
package sqlcdb
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const deleteCronJob = `-- name: DeleteCronJob :exec
|
|
DELETE FROM cron_jobs WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteCronJob(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.ExecContext(ctx, deleteCronJob, id)
|
|
return err
|
|
}
|
|
|
|
const finishCronExecution = `-- name: FinishCronExecution :exec
|
|
UPDATE cron_executions SET finished_at = $1, status = $2, output = $3, error = $4 WHERE id = $5
|
|
`
|
|
|
|
type FinishCronExecutionParams struct {
|
|
FinishedAt sql.NullTime `json:"finished_at"`
|
|
Status sql.NullString `json:"status"`
|
|
Output sql.NullString `json:"output"`
|
|
Error sql.NullString `json:"error"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) FinishCronExecution(ctx context.Context, arg FinishCronExecutionParams) error {
|
|
_, err := q.db.ExecContext(ctx, finishCronExecution,
|
|
arg.FinishedAt,
|
|
arg.Status,
|
|
arg.Output,
|
|
arg.Error,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const getCronJob = `-- name: GetCronJob :one
|
|
SELECT id, project_id, service_id, name, schedule, command, timezone,
|
|
enabled, last_run_at, next_run_at, last_status, last_output,
|
|
retention, created_at, updated_at
|
|
FROM cron_jobs WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetCronJob(ctx context.Context, id uuid.UUID) (CronJob, error) {
|
|
row := q.db.QueryRowContext(ctx, getCronJob, id)
|
|
var i CronJob
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.ServiceID,
|
|
&i.Name,
|
|
&i.Schedule,
|
|
&i.Command,
|
|
&i.Timezone,
|
|
&i.Enabled,
|
|
&i.LastRunAt,
|
|
&i.NextRunAt,
|
|
&i.LastStatus,
|
|
&i.LastOutput,
|
|
&i.Retention,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getCronJobForTrigger = `-- name: GetCronJobForTrigger :one
|
|
SELECT cj.service_id, cj.command, cj.schedule, cj.timezone, cj.retention, p.owner_id
|
|
FROM cron_jobs cj
|
|
JOIN projects p ON cj.project_id = p.id
|
|
WHERE cj.id = $1
|
|
`
|
|
|
|
type GetCronJobForTriggerRow struct {
|
|
ServiceID uuid.UUID `json:"service_id"`
|
|
Command string `json:"command"`
|
|
Schedule string `json:"schedule"`
|
|
Timezone sql.NullString `json:"timezone"`
|
|
Retention sql.NullInt32 `json:"retention"`
|
|
OwnerID uuid.UUID `json:"owner_id"`
|
|
}
|
|
|
|
func (q *Queries) GetCronJobForTrigger(ctx context.Context, id uuid.UUID) (GetCronJobForTriggerRow, error) {
|
|
row := q.db.QueryRowContext(ctx, getCronJobForTrigger, id)
|
|
var i GetCronJobForTriggerRow
|
|
err := row.Scan(
|
|
&i.ServiceID,
|
|
&i.Command,
|
|
&i.Schedule,
|
|
&i.Timezone,
|
|
&i.Retention,
|
|
&i.OwnerID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getCronJobOwner = `-- name: GetCronJobOwner :one
|
|
SELECT p.owner_id FROM cron_jobs cj
|
|
JOIN projects p ON cj.project_id = p.id
|
|
WHERE cj.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetCronJobOwner(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
|
row := q.db.QueryRowContext(ctx, getCronJobOwner, id)
|
|
var owner_id uuid.UUID
|
|
err := row.Scan(&owner_id)
|
|
return owner_id, err
|
|
}
|
|
|
|
const getCronJobProjectID = `-- name: GetCronJobProjectID :one
|
|
SELECT project_id FROM cron_jobs WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetCronJobProjectID(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
|
row := q.db.QueryRowContext(ctx, getCronJobProjectID, id)
|
|
var project_id uuid.UUID
|
|
err := row.Scan(&project_id)
|
|
return project_id, err
|
|
}
|
|
|
|
const getCronOwnerViaService = `-- name: GetCronOwnerViaService :one
|
|
SELECT p.owner_id FROM projects p
|
|
JOIN services s ON s.project_id = p.id
|
|
WHERE s.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetCronOwnerViaService(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
|
row := q.db.QueryRowContext(ctx, getCronOwnerViaService, id)
|
|
var owner_id uuid.UUID
|
|
err := row.Scan(&owner_id)
|
|
return owner_id, err
|
|
}
|
|
|
|
const insertCronExecution = `-- name: InsertCronExecution :exec
|
|
INSERT INTO cron_executions (id, cron_job_id, started_at, status)
|
|
VALUES ($1, $2, $3, $4)
|
|
`
|
|
|
|
type InsertCronExecutionParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
CronJobID uuid.UUID `json:"cron_job_id"`
|
|
StartedAt time.Time `json:"started_at"`
|
|
Status sql.NullString `json:"status"`
|
|
}
|
|
|
|
func (q *Queries) InsertCronExecution(ctx context.Context, arg InsertCronExecutionParams) error {
|
|
_, err := q.db.ExecContext(ctx, insertCronExecution,
|
|
arg.ID,
|
|
arg.CronJobID,
|
|
arg.StartedAt,
|
|
arg.Status,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertCronJob = `-- name: InsertCronJob :exec
|
|
INSERT INTO cron_jobs (id, project_id, service_id, name, schedule, command, timezone, enabled, next_run_at, retention, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
|
`
|
|
|
|
type InsertCronJobParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
ServiceID uuid.UUID `json:"service_id"`
|
|
Name string `json:"name"`
|
|
Schedule string `json:"schedule"`
|
|
Command string `json:"command"`
|
|
Timezone sql.NullString `json:"timezone"`
|
|
Enabled sql.NullBool `json:"enabled"`
|
|
NextRunAt sql.NullTime `json:"next_run_at"`
|
|
Retention sql.NullInt32 `json:"retention"`
|
|
CreatedAt sql.NullTime `json:"created_at"`
|
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertCronJob(ctx context.Context, arg InsertCronJobParams) error {
|
|
_, err := q.db.ExecContext(ctx, insertCronJob,
|
|
arg.ID,
|
|
arg.ProjectID,
|
|
arg.ServiceID,
|
|
arg.Name,
|
|
arg.Schedule,
|
|
arg.Command,
|
|
arg.Timezone,
|
|
arg.Enabled,
|
|
arg.NextRunAt,
|
|
arg.Retention,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listCronExecutions = `-- name: ListCronExecutions :many
|
|
SELECT id, cron_job_id, started_at, finished_at, status, output, error
|
|
FROM cron_executions
|
|
WHERE cron_job_id = $1
|
|
ORDER BY started_at DESC
|
|
LIMIT 100
|
|
`
|
|
|
|
func (q *Queries) ListCronExecutions(ctx context.Context, cronJobID uuid.UUID) ([]CronExecution, error) {
|
|
rows, err := q.db.QueryContext(ctx, listCronExecutions, cronJobID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []CronExecution{}
|
|
for rows.Next() {
|
|
var i CronExecution
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.CronJobID,
|
|
&i.StartedAt,
|
|
&i.FinishedAt,
|
|
&i.Status,
|
|
&i.Output,
|
|
&i.Error,
|
|
); 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 listDueCronJobs = `-- name: ListDueCronJobs :many
|
|
SELECT id, project_id, service_id, name, schedule, command, timezone, enabled, retention
|
|
FROM cron_jobs
|
|
WHERE enabled = TRUE AND next_run_at IS NOT NULL AND next_run_at <= NOW()
|
|
`
|
|
|
|
type ListDueCronJobsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ProjectID uuid.UUID `json:"project_id"`
|
|
ServiceID uuid.UUID `json:"service_id"`
|
|
Name string `json:"name"`
|
|
Schedule string `json:"schedule"`
|
|
Command string `json:"command"`
|
|
Timezone sql.NullString `json:"timezone"`
|
|
Enabled sql.NullBool `json:"enabled"`
|
|
Retention sql.NullInt32 `json:"retention"`
|
|
}
|
|
|
|
func (q *Queries) ListDueCronJobs(ctx context.Context) ([]ListDueCronJobsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, listDueCronJobs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDueCronJobsRow{}
|
|
for rows.Next() {
|
|
var i ListDueCronJobsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ProjectID,
|
|
&i.ServiceID,
|
|
&i.Name,
|
|
&i.Schedule,
|
|
&i.Command,
|
|
&i.Timezone,
|
|
&i.Enabled,
|
|
&i.Retention,
|
|
); 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 trimCronExecutions = `-- name: TrimCronExecutions :exec
|
|
DELETE FROM cron_executions WHERE cron_executions.cron_job_id = $1 AND cron_executions.id NOT IN (
|
|
SELECT ce.id FROM cron_executions ce WHERE ce.cron_job_id = $1
|
|
ORDER BY ce.started_at DESC LIMIT $2)
|
|
`
|
|
|
|
type TrimCronExecutionsParams struct {
|
|
CronJobID uuid.UUID `json:"cron_job_id"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
func (q *Queries) TrimCronExecutions(ctx context.Context, arg TrimCronExecutionsParams) error {
|
|
_, err := q.db.ExecContext(ctx, trimCronExecutions, arg.CronJobID, arg.Limit)
|
|
return err
|
|
}
|
|
|
|
const updateCronJobRun = `-- name: UpdateCronJobRun :exec
|
|
UPDATE cron_jobs SET last_run_at = $1, last_status = $2, last_output = $3, next_run_at = $4 WHERE id = $5
|
|
`
|
|
|
|
type UpdateCronJobRunParams struct {
|
|
LastRunAt sql.NullTime `json:"last_run_at"`
|
|
LastStatus sql.NullString `json:"last_status"`
|
|
LastOutput sql.NullString `json:"last_output"`
|
|
NextRunAt sql.NullTime `json:"next_run_at"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateCronJobRun(ctx context.Context, arg UpdateCronJobRunParams) error {
|
|
_, err := q.db.ExecContext(ctx, updateCronJobRun,
|
|
arg.LastRunAt,
|
|
arg.LastStatus,
|
|
arg.LastOutput,
|
|
arg.NextRunAt,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|