feat(store): schedules, notification_channels, domain last_check_status + методы

This commit is contained in:
2026-07-04 13:10:42 +07:00
parent 1cdb32b747
commit 6fd847a909
10 changed files with 814 additions and 5 deletions
+148
View File
@@ -0,0 +1,148 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: channels.sql
package db
import (
"context"
"github.com/google/uuid"
)
const createChannel = `-- name: CreateChannel :one
INSERT INTO notification_channels (id, project_id, type, config, secret_enc)
VALUES ($1, $2, $3, $4, $5) RETURNING id, project_id, type, config, secret_enc, enabled, created_at
`
type CreateChannelParams struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
Type string `json:"type"`
Config []byte `json:"config"`
SecretEnc string `json:"secret_enc"`
}
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (NotificationChannel, error) {
row := q.db.QueryRow(ctx, createChannel,
arg.ID,
arg.ProjectID,
arg.Type,
arg.Config,
arg.SecretEnc,
)
var i NotificationChannel
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Type,
&i.Config,
&i.SecretEnc,
&i.Enabled,
&i.CreatedAt,
)
return i, err
}
const deleteChannel = `-- name: DeleteChannel :exec
DELETE FROM notification_channels WHERE id = $1 AND project_id = $2
`
type DeleteChannelParams struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
}
func (q *Queries) DeleteChannel(ctx context.Context, arg DeleteChannelParams) error {
_, err := q.db.Exec(ctx, deleteChannel, arg.ID, arg.ProjectID)
return err
}
const getChannel = `-- name: GetChannel :one
SELECT id, project_id, type, config, secret_enc, enabled, created_at FROM notification_channels WHERE id = $1 AND project_id = $2
`
type GetChannelParams struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
}
func (q *Queries) GetChannel(ctx context.Context, arg GetChannelParams) (NotificationChannel, error) {
row := q.db.QueryRow(ctx, getChannel, arg.ID, arg.ProjectID)
var i NotificationChannel
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.Type,
&i.Config,
&i.SecretEnc,
&i.Enabled,
&i.CreatedAt,
)
return i, err
}
const listChannels = `-- name: ListChannels :many
SELECT id, project_id, type, config, secret_enc, enabled, created_at FROM notification_channels WHERE project_id = $1 ORDER BY created_at
`
func (q *Queries) ListChannels(ctx context.Context, projectID uuid.UUID) ([]NotificationChannel, error) {
rows, err := q.db.Query(ctx, listChannels, projectID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []NotificationChannel
for rows.Next() {
var i NotificationChannel
if err := rows.Scan(
&i.ID,
&i.ProjectID,
&i.Type,
&i.Config,
&i.SecretEnc,
&i.Enabled,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listEnabledChannels = `-- name: ListEnabledChannels :many
SELECT id, project_id, type, config, secret_enc, enabled, created_at FROM notification_channels WHERE project_id = $1 AND enabled ORDER BY created_at
`
func (q *Queries) ListEnabledChannels(ctx context.Context, projectID uuid.UUID) ([]NotificationChannel, error) {
rows, err := q.db.Query(ctx, listEnabledChannels, projectID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []NotificationChannel
for rows.Next() {
var i NotificationChannel
if err := rows.Scan(
&i.ID,
&i.ProjectID,
&i.Type,
&i.Config,
&i.SecretEnc,
&i.Enabled,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+35 -5
View File
@@ -15,7 +15,7 @@ import (
const createDomain = `-- name: CreateDomain :one
INSERT INTO domains (id, project_id, provider_account_id, zone_name, zone_id, template_id)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at, last_check_status
`
type CreateDomainParams struct {
@@ -45,6 +45,7 @@ func (q *Queries) CreateDomain(ctx context.Context, arg CreateDomainParams) (Dom
&i.ZoneID,
&i.TemplateID,
&i.CreatedAt,
&i.LastCheckStatus,
)
return i, err
}
@@ -64,7 +65,7 @@ func (q *Queries) DeleteDomain(ctx context.Context, arg DeleteDomainParams) erro
}
const getDomain = `-- name: GetDomain :one
SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at FROM domains WHERE id = $1 AND project_id = $2
SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at, last_check_status FROM domains WHERE id = $1 AND project_id = $2
`
type GetDomainParams struct {
@@ -83,15 +84,27 @@ func (q *Queries) GetDomain(ctx context.Context, arg GetDomainParams) (Domain, e
&i.ZoneID,
&i.TemplateID,
&i.CreatedAt,
&i.LastCheckStatus,
)
return i, err
}
const getDomainStatus = `-- name: GetDomainStatus :one
SELECT last_check_status FROM domains WHERE id = $1
`
func (q *Queries) GetDomainStatus(ctx context.Context, id uuid.UUID) (string, error) {
row := q.db.QueryRow(ctx, getDomainStatus, id)
var last_check_status string
err := row.Scan(&last_check_status)
return last_check_status, err
}
const importDomain = `-- name: ImportDomain :one
INSERT INTO domains (id, project_id, provider_account_id, zone_name, zone_id, template_id)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (project_id, zone_id) DO NOTHING
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at, last_check_status
`
type ImportDomainParams struct {
@@ -121,12 +134,13 @@ func (q *Queries) ImportDomain(ctx context.Context, arg ImportDomainParams) (Dom
&i.ZoneID,
&i.TemplateID,
&i.CreatedAt,
&i.LastCheckStatus,
)
return i, err
}
const listDomains = `-- name: ListDomains :many
SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at FROM domains WHERE project_id = $1 ORDER BY created_at
SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at, last_check_status FROM domains WHERE project_id = $1 ORDER BY created_at
`
func (q *Queries) ListDomains(ctx context.Context, projectID uuid.UUID) ([]Domain, error) {
@@ -146,6 +160,7 @@ func (q *Queries) ListDomains(ctx context.Context, projectID uuid.UUID) ([]Domai
&i.ZoneID,
&i.TemplateID,
&i.CreatedAt,
&i.LastCheckStatus,
); err != nil {
return nil, err
}
@@ -189,9 +204,23 @@ func (q *Queries) LoadDomainFull(ctx context.Context, arg LoadDomainFullParams)
return i, err
}
const setDomainStatus = `-- name: SetDomainStatus :exec
UPDATE domains SET last_check_status = $2 WHERE id = $1
`
type SetDomainStatusParams struct {
ID uuid.UUID `json:"id"`
LastCheckStatus string `json:"last_check_status"`
}
func (q *Queries) SetDomainStatus(ctx context.Context, arg SetDomainStatusParams) error {
_, err := q.db.Exec(ctx, setDomainStatus, arg.ID, arg.LastCheckStatus)
return err
}
const updateDomainTemplate = `-- name: UpdateDomainTemplate :one
UPDATE domains SET template_id = $3 WHERE id = $1 AND project_id = $2
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at
RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at, last_check_status
`
type UpdateDomainTemplateParams struct {
@@ -211,6 +240,7 @@ func (q *Queries) UpdateDomainTemplate(ctx context.Context, arg UpdateDomainTemp
&i.ZoneID,
&i.TemplateID,
&i.CreatedAt,
&i.LastCheckStatus,
)
return i, err
}
+20
View File
@@ -25,6 +25,17 @@ type Domain struct {
ZoneID string `json:"zone_id"`
TemplateID *uuid.UUID `json:"template_id"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
LastCheckStatus string `json:"last_check_status"`
}
type NotificationChannel struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
Type string `json:"type"`
Config []byte `json:"config"`
SecretEnc string `json:"secret_enc"`
Enabled bool `json:"enabled"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Project struct {
@@ -43,6 +54,15 @@ type ProviderAccount struct {
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Schedule struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
IntervalSeconds int32 `json:"interval_seconds"`
Enabled bool `json:"enabled"`
LastRunAt pgtype.Timestamptz `json:"last_run_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type Session struct {
ID uuid.UUID `json:"id"`
UserID uuid.UUID `json:"user_id"`
+110
View File
@@ -0,0 +1,110 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: schedules.sql
package db
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const getSchedule = `-- name: GetSchedule :one
SELECT id, project_id, interval_seconds, enabled, last_run_at, created_at FROM schedules WHERE project_id = $1
`
func (q *Queries) GetSchedule(ctx context.Context, projectID uuid.UUID) (Schedule, error) {
row := q.db.QueryRow(ctx, getSchedule, projectID)
var i Schedule
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.IntervalSeconds,
&i.Enabled,
&i.LastRunAt,
&i.CreatedAt,
)
return i, err
}
const listDueSchedules = `-- name: ListDueSchedules :many
SELECT id, project_id, interval_seconds, enabled, last_run_at, created_at FROM schedules
WHERE enabled AND (last_run_at IS NULL OR last_run_at + (interval_seconds || ' seconds')::interval <= $1)
`
func (q *Queries) ListDueSchedules(ctx context.Context, lastRunAt pgtype.Timestamptz) ([]Schedule, error) {
rows, err := q.db.Query(ctx, listDueSchedules, lastRunAt)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Schedule
for rows.Next() {
var i Schedule
if err := rows.Scan(
&i.ID,
&i.ProjectID,
&i.IntervalSeconds,
&i.Enabled,
&i.LastRunAt,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const touchScheduleRun = `-- name: TouchScheduleRun :exec
UPDATE schedules SET last_run_at = $2 WHERE project_id = $1
`
type TouchScheduleRunParams struct {
ProjectID uuid.UUID `json:"project_id"`
LastRunAt pgtype.Timestamptz `json:"last_run_at"`
}
func (q *Queries) TouchScheduleRun(ctx context.Context, arg TouchScheduleRunParams) error {
_, err := q.db.Exec(ctx, touchScheduleRun, arg.ProjectID, arg.LastRunAt)
return err
}
const upsertSchedule = `-- name: UpsertSchedule :one
INSERT INTO schedules (id, project_id, interval_seconds, enabled)
VALUES ($1, $2, $3, $4)
ON CONFLICT (project_id) DO UPDATE SET interval_seconds = $3, enabled = $4
RETURNING id, project_id, interval_seconds, enabled, last_run_at, created_at
`
type UpsertScheduleParams struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
IntervalSeconds int32 `json:"interval_seconds"`
Enabled bool `json:"enabled"`
}
func (q *Queries) UpsertSchedule(ctx context.Context, arg UpsertScheduleParams) (Schedule, error) {
row := q.db.QueryRow(ctx, upsertSchedule,
arg.ID,
arg.ProjectID,
arg.IntervalSeconds,
arg.Enabled,
)
var i Schedule
err := row.Scan(
&i.ID,
&i.ProjectID,
&i.IntervalSeconds,
&i.Enabled,
&i.LastRunAt,
&i.CreatedAt,
)
return i, err
}