mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 15:51:26 +00:00
refactor(db): port preview environments to sqlc (#77)
CI / Changed paths (push) Canceled after 0s
CI / Infra (Compose + Scripts) (push) Canceled after 0s
CI / Backend (Go) (push) Canceled after 0s
CI / Frontend (React) (push) Canceled after 0s
CI / Mobile (Expo) (push) Canceled after 0s
CI / Compose boot smoke (push) Canceled after 0s
CI / Build & publish images (push) Canceled after 0s
CI / Release (push) Canceled after 0s
CI / Changed paths (push) Canceled after 0s
CI / Infra (Compose + Scripts) (push) Canceled after 0s
CI / Backend (Go) (push) Canceled after 0s
CI / Frontend (React) (push) Canceled after 0s
CI / Mobile (Expo) (push) Canceled after 0s
CI / Compose boot smoke (push) Canceled after 0s
CI / Build & publish images (push) Canceled after 0s
CI / Release (push) Canceled after 0s
Tranche 3: all 30 raw sites in preview_environments.go — list/detail joins, access-check reads, create/update/delete, promote, user cleanup, TTL sweep, and the status-sync UPDATE...FROM CASE. Deployments and preview_environments schema.sql definitions completed to match runtime. Shared mapper converts generated rows to the PreviewEnvironment DTO; DeleteServiceByID from tranche 2 is reused for clone teardown. Generated with [Devin](https://devin.ai) Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
687555fc78
commit
202fdd7b5a
7 files changed
+1183
-287
No files matched your search
@@ -0,0 +1,635 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: preview_environments.sql
|
||||
|
||||
package sqlcdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const countActivePreviewsForBranch = `-- name: CountActivePreviewsForBranch :one
|
||||
SELECT COUNT(*) FROM preview_environments
|
||||
WHERE service_id = $1 AND branch_name = $2 AND status NOT IN ('expired', 'stopped')
|
||||
`
|
||||
|
||||
type CountActivePreviewsForBranchParams struct {
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
}
|
||||
|
||||
func (q *Queries) CountActivePreviewsForBranch(ctx context.Context, arg CountActivePreviewsForBranchParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, countActivePreviewsForBranch, arg.ServiceID, arg.BranchName)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const createPreviewDomain = `-- name: CreatePreviewDomain :exec
|
||||
INSERT INTO service_domains (service_id, domain, is_default, cert_type) VALUES ($1, $2, true, 'tls')
|
||||
`
|
||||
|
||||
type CreatePreviewDomainParams struct {
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
Domain string `json:"domain"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreatePreviewDomain(ctx context.Context, arg CreatePreviewDomainParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createPreviewDomain, arg.ServiceID, arg.Domain)
|
||||
return err
|
||||
}
|
||||
|
||||
const createPreviewEnvironment = `-- name: CreatePreviewEnvironment :exec
|
||||
INSERT INTO preview_environments
|
||||
(id, project_id, service_id, preview_service_id, branch_name, pr_number, environment,
|
||||
status, url, expires_at, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||
`
|
||||
|
||||
type CreatePreviewEnvironmentParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
PrNumber sql.NullInt32 `json:"pr_number"`
|
||||
Environment string `json:"environment"`
|
||||
Status string `json:"status"`
|
||||
Url sql.NullString `json:"url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreatePreviewEnvironment(ctx context.Context, arg CreatePreviewEnvironmentParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createPreviewEnvironment,
|
||||
arg.ID,
|
||||
arg.ProjectID,
|
||||
arg.ServiceID,
|
||||
arg.PreviewServiceID,
|
||||
arg.BranchName,
|
||||
arg.PrNumber,
|
||||
arg.Environment,
|
||||
arg.Status,
|
||||
arg.Url,
|
||||
arg.ExpiresAt,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteAllServiceDomains = `-- name: DeleteAllServiceDomains :exec
|
||||
DELETE FROM service_domains WHERE service_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteAllServiceDomains(ctx context.Context, serviceID uuid.UUID) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteAllServiceDomains, serviceID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deletePreviewEnvironment = `-- name: DeletePreviewEnvironment :exec
|
||||
DELETE FROM preview_environments WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePreviewEnvironment(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.ExecContext(ctx, deletePreviewEnvironment, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getPreviewEnvironment = `-- name: GetPreviewEnvironment :one
|
||||
SELECT pe.id, pe.project_id, pe.service_id, pe.preview_service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at,
|
||||
s.id AS svc_id, s.name AS service_name, s.type AS service_type
|
||||
FROM preview_environments pe
|
||||
LEFT JOIN services s ON pe.service_id = s.id
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.is_approved OR p.owner_id = $2 OR $3::bool
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $2))
|
||||
`
|
||||
|
||||
type GetPreviewEnvironmentParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
Column3 bool `json:"column_3"`
|
||||
}
|
||||
|
||||
type GetPreviewEnvironmentRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
PrNumber sql.NullInt32 `json:"pr_number"`
|
||||
Environment string `json:"environment"`
|
||||
Status string `json:"status"`
|
||||
Url sql.NullString `json:"url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
SvcID uuid.NullUUID `json:"svc_id"`
|
||||
ServiceName sql.NullString `json:"service_name"`
|
||||
ServiceType sql.NullString `json:"service_type"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviewEnvironment(ctx context.Context, arg GetPreviewEnvironmentParams) (GetPreviewEnvironmentRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getPreviewEnvironment, arg.ID, arg.OwnerID, arg.Column3)
|
||||
var i GetPreviewEnvironmentRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.ServiceID,
|
||||
&i.PreviewServiceID,
|
||||
&i.BranchName,
|
||||
&i.PrNumber,
|
||||
&i.Environment,
|
||||
&i.Status,
|
||||
&i.Url,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.SvcID,
|
||||
&i.ServiceName,
|
||||
&i.ServiceType,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getPreviewEnvironmentForPromote = `-- name: GetPreviewEnvironmentForPromote :one
|
||||
SELECT pe.id, pe.project_id, pe.service_id, pe.preview_service_id, pe.branch_name, pe.environment, pe.status
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.owner_id = $2 OR $3::bool)
|
||||
`
|
||||
|
||||
type GetPreviewEnvironmentForPromoteParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
Column3 bool `json:"column_3"`
|
||||
}
|
||||
|
||||
type GetPreviewEnvironmentForPromoteRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
Environment string `json:"environment"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviewEnvironmentForPromote(ctx context.Context, arg GetPreviewEnvironmentForPromoteParams) (GetPreviewEnvironmentForPromoteRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getPreviewEnvironmentForPromote, arg.ID, arg.OwnerID, arg.Column3)
|
||||
var i GetPreviewEnvironmentForPromoteRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.ServiceID,
|
||||
&i.PreviewServiceID,
|
||||
&i.BranchName,
|
||||
&i.Environment,
|
||||
&i.Status,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getPreviewEnvironmentForWrite = `-- name: GetPreviewEnvironmentForWrite :one
|
||||
SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.owner_id = $2 OR $3::bool)
|
||||
`
|
||||
|
||||
type GetPreviewEnvironmentForWriteParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
Column3 bool `json:"column_3"`
|
||||
}
|
||||
|
||||
type GetPreviewEnvironmentForWriteRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
PrNumber sql.NullInt32 `json:"pr_number"`
|
||||
Environment string `json:"environment"`
|
||||
Status string `json:"status"`
|
||||
Url sql.NullString `json:"url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviewEnvironmentForWrite(ctx context.Context, arg GetPreviewEnvironmentForWriteParams) (GetPreviewEnvironmentForWriteRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getPreviewEnvironmentForWrite, arg.ID, arg.OwnerID, arg.Column3)
|
||||
var i GetPreviewEnvironmentForWriteRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.ServiceID,
|
||||
&i.BranchName,
|
||||
&i.PrNumber,
|
||||
&i.Environment,
|
||||
&i.Status,
|
||||
&i.Url,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getPreviewEnvironmentOwner = `-- name: GetPreviewEnvironmentOwner :one
|
||||
SELECT p.owner_id
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetPreviewEnvironmentOwner(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
||||
row := q.db.QueryRowContext(ctx, getPreviewEnvironmentOwner, id)
|
||||
var owner_id uuid.UUID
|
||||
err := row.Scan(&owner_id)
|
||||
return owner_id, err
|
||||
}
|
||||
|
||||
const getPreviewServiceID = `-- name: GetPreviewServiceID :one
|
||||
SELECT preview_service_id FROM preview_environments WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetPreviewServiceID(ctx context.Context, id uuid.UUID) (uuid.NullUUID, error) {
|
||||
row := q.db.QueryRowContext(ctx, getPreviewServiceID, id)
|
||||
var preview_service_id uuid.NullUUID
|
||||
err := row.Scan(&preview_service_id)
|
||||
return preview_service_id, err
|
||||
}
|
||||
|
||||
const getProjectBrief = `-- name: GetProjectBrief :one
|
||||
SELECT id, name, owner_id FROM projects WHERE id = $1
|
||||
`
|
||||
|
||||
type GetProjectBriefRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetProjectBrief(ctx context.Context, id uuid.UUID) (GetProjectBriefRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getProjectBrief, id)
|
||||
var i GetProjectBriefRow
|
||||
err := row.Scan(&i.ID, &i.Name, &i.OwnerID)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getServiceForDeploy = `-- name: GetServiceForDeploy :one
|
||||
SELECT s.id, s.project_id, s.name, s.type, s.status, s.image, s.command,
|
||||
s.environment, s.git_repo, s.git_branch, s.build_path, s.cpu, s.memory,
|
||||
COALESCE(s.replicas, 1) AS replicas, COALESCE(s.port, 0) AS port,
|
||||
COALESCE(s.domain, '') AS domain, COALESCE(s.healthcheck_path, '') AS healthcheck_path,
|
||||
COALESCE(s.restart_policy, 'unless-stopped') AS restart_policy,
|
||||
COALESCE(s.builder, 'auto') AS builder, COALESCE(s.cpu_reserve, '') AS cpu_reserve,
|
||||
COALESCE(s.memory_reserve, '') AS memory_reserve, COALESCE(s.static_build_cmd, '') AS static_build_cmd,
|
||||
COALESCE(s.static_dir, '') AS static_dir,
|
||||
s.created_at, s.updated_at
|
||||
FROM services s
|
||||
WHERE s.id = $1
|
||||
`
|
||||
|
||||
type GetServiceForDeployRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
Name string `json:"name"`
|
||||
Type sql.NullString `json:"type"`
|
||||
Status sql.NullString `json:"status"`
|
||||
Image sql.NullString `json:"image"`
|
||||
Command sql.NullString `json:"command"`
|
||||
Environment sql.NullString `json:"environment"`
|
||||
GitRepo sql.NullString `json:"git_repo"`
|
||||
GitBranch sql.NullString `json:"git_branch"`
|
||||
BuildPath sql.NullString `json:"build_path"`
|
||||
Cpu sql.NullString `json:"cpu"`
|
||||
Memory sql.NullString `json:"memory"`
|
||||
Replicas int32 `json:"replicas"`
|
||||
Port int32 `json:"port"`
|
||||
Domain string `json:"domain"`
|
||||
HealthcheckPath string `json:"healthcheck_path"`
|
||||
RestartPolicy string `json:"restart_policy"`
|
||||
Builder string `json:"builder"`
|
||||
CpuReserve string `json:"cpu_reserve"`
|
||||
MemoryReserve string `json:"memory_reserve"`
|
||||
StaticBuildCmd string `json:"static_build_cmd"`
|
||||
StaticDir string `json:"static_dir"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetServiceForDeploy(ctx context.Context, id uuid.UUID) (GetServiceForDeployRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getServiceForDeploy, id)
|
||||
var i GetServiceForDeployRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.Name,
|
||||
&i.Type,
|
||||
&i.Status,
|
||||
&i.Image,
|
||||
&i.Command,
|
||||
&i.Environment,
|
||||
&i.GitRepo,
|
||||
&i.GitBranch,
|
||||
&i.BuildPath,
|
||||
&i.Cpu,
|
||||
&i.Memory,
|
||||
&i.Replicas,
|
||||
&i.Port,
|
||||
&i.Domain,
|
||||
&i.HealthcheckPath,
|
||||
&i.RestartPolicy,
|
||||
&i.Builder,
|
||||
&i.CpuReserve,
|
||||
&i.MemoryReserve,
|
||||
&i.StaticBuildCmd,
|
||||
&i.StaticDir,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getServiceTypeBrief = `-- name: GetServiceTypeBrief :one
|
||||
SELECT id, name, COALESCE(type, service_type, '') AS service_type FROM services WHERE id = $1 AND project_id = $2
|
||||
`
|
||||
|
||||
type GetServiceTypeBriefParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
}
|
||||
|
||||
type GetServiceTypeBriefRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ServiceType string `json:"service_type"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetServiceTypeBrief(ctx context.Context, arg GetServiceTypeBriefParams) (GetServiceTypeBriefRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getServiceTypeBrief, arg.ID, arg.ProjectID)
|
||||
var i GetServiceTypeBriefRow
|
||||
err := row.Scan(&i.ID, &i.Name, &i.ServiceType)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertPendingDeployment = `-- name: InsertPendingDeployment :exec
|
||||
INSERT INTO deployments (id, service_id, version, commit_hash, status, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, 'pending', NOW(), NOW())
|
||||
`
|
||||
|
||||
type InsertPendingDeploymentParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
Version string `json:"version"`
|
||||
CommitHash sql.NullString `json:"commit_hash"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertPendingDeployment(ctx context.Context, arg InsertPendingDeploymentParams) error {
|
||||
_, err := q.db.ExecContext(ctx, insertPendingDeployment,
|
||||
arg.ID,
|
||||
arg.ServiceID,
|
||||
arg.Version,
|
||||
arg.CommitHash,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listExpiredPreviewsForUser = `-- name: ListExpiredPreviewsForUser :many
|
||||
SELECT pe.id, pe.project_id, pe.service_id, pe.preview_service_id, pe.branch_name, pe.environment
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE (p.owner_id = $1 OR $2::bool) AND pe.expires_at < NOW() AND pe.status != 'expired'
|
||||
`
|
||||
|
||||
type ListExpiredPreviewsForUserParams struct {
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
Column2 bool `json:"column_2"`
|
||||
}
|
||||
|
||||
type ListExpiredPreviewsForUserRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
Environment string `json:"environment"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListExpiredPreviewsForUser(ctx context.Context, arg ListExpiredPreviewsForUserParams) ([]ListExpiredPreviewsForUserRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listExpiredPreviewsForUser, arg.OwnerID, arg.Column2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListExpiredPreviewsForUserRow{}
|
||||
for rows.Next() {
|
||||
var i ListExpiredPreviewsForUserRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.ServiceID,
|
||||
&i.PreviewServiceID,
|
||||
&i.BranchName,
|
||||
&i.Environment,
|
||||
); 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 listPreviewEnvironmentsForProject = `-- name: ListPreviewEnvironmentsForProject :many
|
||||
SELECT pe.id, pe.project_id, pe.service_id, pe.preview_service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at,
|
||||
s.id AS svc_id, s.name AS service_name, s.type AS service_type
|
||||
FROM preview_environments pe
|
||||
LEFT JOIN services s ON pe.service_id = s.id
|
||||
WHERE pe.project_id = $1
|
||||
ORDER BY pe.created_at DESC
|
||||
`
|
||||
|
||||
type ListPreviewEnvironmentsForProjectRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
BranchName string `json:"branch_name"`
|
||||
PrNumber sql.NullInt32 `json:"pr_number"`
|
||||
Environment string `json:"environment"`
|
||||
Status string `json:"status"`
|
||||
Url sql.NullString `json:"url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
SvcID uuid.NullUUID `json:"svc_id"`
|
||||
ServiceName sql.NullString `json:"service_name"`
|
||||
ServiceType sql.NullString `json:"service_type"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListPreviewEnvironmentsForProject(ctx context.Context, projectID uuid.UUID) ([]ListPreviewEnvironmentsForProjectRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listPreviewEnvironmentsForProject, projectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListPreviewEnvironmentsForProjectRow{}
|
||||
for rows.Next() {
|
||||
var i ListPreviewEnvironmentsForProjectRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ProjectID,
|
||||
&i.ServiceID,
|
||||
&i.PreviewServiceID,
|
||||
&i.BranchName,
|
||||
&i.PrNumber,
|
||||
&i.Environment,
|
||||
&i.Status,
|
||||
&i.Url,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.SvcID,
|
||||
&i.ServiceName,
|
||||
&i.ServiceType,
|
||||
); 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 listSweepablePreviews = `-- name: ListSweepablePreviews :many
|
||||
SELECT id, preview_service_id FROM preview_environments
|
||||
WHERE expires_at < NOW() AND status NOT IN ('expired', 'stopped')
|
||||
`
|
||||
|
||||
type ListSweepablePreviewsRow struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
PreviewServiceID uuid.NullUUID `json:"preview_service_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListSweepablePreviews(ctx context.Context) ([]ListSweepablePreviewsRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listSweepablePreviews)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListSweepablePreviewsRow{}
|
||||
for rows.Next() {
|
||||
var i ListSweepablePreviewsRow
|
||||
if err := rows.Scan(&i.ID, &i.PreviewServiceID); 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 markPreviewEnvironmentStatus = `-- name: MarkPreviewEnvironmentStatus :exec
|
||||
UPDATE preview_environments SET status = $1, updated_at = NOW() WHERE id = $2
|
||||
`
|
||||
|
||||
type MarkPreviewEnvironmentStatusParams struct {
|
||||
Status string `json:"status"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) MarkPreviewEnvironmentStatus(ctx context.Context, arg MarkPreviewEnvironmentStatusParams) error {
|
||||
_, err := q.db.ExecContext(ctx, markPreviewEnvironmentStatus, arg.Status, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const syncPreviewStatuses = `-- name: SyncPreviewStatuses :exec
|
||||
UPDATE preview_environments pe
|
||||
SET status = derived.st, updated_at = NOW()
|
||||
FROM (
|
||||
SELECT pe2.id,
|
||||
CASE
|
||||
WHEN s.status IN ('running', 'deployed') THEN 'running'
|
||||
WHEN s.status IN ('failed', 'error') THEN 'failed'
|
||||
WHEN s.status IN ('building', 'deploying', 'queued', 'pending', 'cloning') THEN 'building'
|
||||
ELSE pe2.status
|
||||
END AS st
|
||||
FROM preview_environments pe2
|
||||
JOIN services s ON pe2.preview_service_id = s.id
|
||||
) derived
|
||||
WHERE pe.id = derived.id
|
||||
AND pe.status IN ('building', 'running', 'failed')
|
||||
AND pe.status <> derived.st
|
||||
`
|
||||
|
||||
func (q *Queries) SyncPreviewStatuses(ctx context.Context) error {
|
||||
_, err := q.db.ExecContext(ctx, syncPreviewStatuses)
|
||||
return err
|
||||
}
|
||||
|
||||
const updatePreviewEnvironment = `-- name: UpdatePreviewEnvironment :exec
|
||||
UPDATE preview_environments
|
||||
SET status = $1, url = $2, expires_at = $3, updated_at = $4
|
||||
WHERE id = $5
|
||||
`
|
||||
|
||||
type UpdatePreviewEnvironmentParams struct {
|
||||
Status string `json:"status"`
|
||||
Url sql.NullString `json:"url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdatePreviewEnvironment(ctx context.Context, arg UpdatePreviewEnvironmentParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updatePreviewEnvironment,
|
||||
arg.Status,
|
||||
arg.Url,
|
||||
arg.ExpiresAt,
|
||||
arg.UpdatedAt,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateServicePreviewBranch = `-- name: UpdateServicePreviewBranch :exec
|
||||
UPDATE services SET git_branch = $1, domain = '', updated_at = NOW() WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateServicePreviewBranchParams struct {
|
||||
GitBranch sql.NullString `json:"git_branch"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateServicePreviewBranch(ctx context.Context, arg UpdateServicePreviewBranchParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateServicePreviewBranch, arg.GitBranch, arg.ID)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user