Added deployments overview
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"quay/app/models"
|
||||
"quay/app/repository"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type SQLiteDeploymentRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSQLiteDeploymentRepository(db *sql.DB) *SQLiteDeploymentRepository {
|
||||
return &SQLiteDeploymentRepository{db: db}
|
||||
}
|
||||
|
||||
var _ repository.DeploymentRepository = (*SQLiteDeploymentRepository)(nil)
|
||||
|
||||
func (r *SQLiteDeploymentRepository) GetDeploymentByID(id string) (*models.Deployment, error) {
|
||||
row := r.db.QueryRow(`
|
||||
SELECT id, site_id, commit_hash, message, status, start_time, finish_time, created_at
|
||||
FROM deployments WHERE id = ?`, id)
|
||||
|
||||
d, err := scanDeployment(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get deployment: %w", err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) GetDeploymentsForSite(siteId string, limit int) ([]models.Deployment, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT id, site_id, commit_hash, message, status, start_time, finish_time, created_at
|
||||
FROM deployments WHERE site_id = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?`, siteId, limit)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get deployments: %w", err)
|
||||
}
|
||||
|
||||
var deployments []models.Deployment
|
||||
for rows.Next() {
|
||||
d, err := scanDeployment(rows)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan deployment: %w", err)
|
||||
}
|
||||
deployments = append(deployments, *d)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate deployments: %w", err)
|
||||
}
|
||||
|
||||
return deployments, nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) CreateDeployment(d *models.Deployment) (string, error) {
|
||||
d.Id = uuid.NewString()
|
||||
if d.Status == "" || !d.Status.IsValid() {
|
||||
d.Status = models.DeploymentStatusPending
|
||||
}
|
||||
_, err := r.db.Exec(`
|
||||
INSERT INTO deployments (id, site_id, commit_hash, message, status, start_time, finish_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
d.Id, d.SiteId, d.CommitHash, d.Message, d.Status, d.StartTime, d.FinishTime,
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create deployment: %w", err)
|
||||
}
|
||||
return d.Id, nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) UpdateDeployment(d *models.Deployment) error {
|
||||
_, err := r.db.Exec(`
|
||||
UPDATE deployments SET site_id=?, commit_hash=?, message=?, status=?, start_time=?, finish_time=?
|
||||
WHERE id=?`,
|
||||
d.SiteId, d.CommitHash, d.Message, d.Status, d.StartTime, d.FinishTime, d.Id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update deployment: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) UpdateDeploymentStatus(deploymentId string, status models.DeploymentStatus) error {
|
||||
if !status.IsValid() {
|
||||
return fmt.Errorf("invalid deployment status")
|
||||
}
|
||||
|
||||
_, err := r.db.Exec(`
|
||||
UPDATE deployments SET status=? WHERE id=?`,
|
||||
status, deploymentId,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update deployment status: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) UpdateDeploymentFinishTime(deploymentId string, finishTime time.Time) error {
|
||||
_, err := r.db.Exec(`
|
||||
UPDATE deployments SET finish_time=? WHERE id=?`,
|
||||
finishTime.Format(time.DateTime), deploymentId,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update deployment finish time: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) UpdateDeploymentGitInfo(deploymentId, commitHash, message string) error {
|
||||
_, err := r.db.Exec(`
|
||||
UPDATE deployments SET commit_hash=?, message=? WHERE id=?`,
|
||||
commitHash, message, deploymentId,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update deployment git info: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SQLiteDeploymentRepository) DeleteDeployment(id string) error {
|
||||
_, err := r.db.Exec(`DELETE FROM deployments WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete deployment: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanDeployment(s scanner) (*models.Deployment, error) {
|
||||
var d models.Deployment
|
||||
err := s.Scan(
|
||||
&d.Id, &d.SiteId, &d.CommitHash, &d.Message,
|
||||
&d.Status, &d.StartTime, &d.FinishTime, &d.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &d, nil
|
||||
}
|
||||
Reference in New Issue
Block a user