init
This commit is contained in:
204
internal/db/repo_store.go
Normal file
204
internal/db/repo_store.go
Normal file
@@ -0,0 +1,204 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"deployment-manager/internal/model"
|
||||
)
|
||||
|
||||
type RepoStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewRepoStore(db *sql.DB) *RepoStore {
|
||||
return &RepoStore{db: db}
|
||||
}
|
||||
|
||||
func (rs *RepoStore) Create(repo *model.Repo) error {
|
||||
query := `
|
||||
INSERT INTO repos (repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
now := time.Now()
|
||||
result, err := rs.db.Exec(query, repo.RepoURL, repo.Status, repo.UserID, repo.Type, repo.ImageTag, repo.LastError, now, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create repo: %w", err)
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get last insert id: %w", err)
|
||||
}
|
||||
|
||||
repo.ID = id
|
||||
repo.CreatedAt = now
|
||||
repo.UpdatedAt = now
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *RepoStore) Get(id int64) (*model.Repo, error) {
|
||||
query := `
|
||||
SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
|
||||
FROM repos
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
repo := &model.Repo{}
|
||||
var imageTag, lastError sql.NullString
|
||||
|
||||
err := rs.db.QueryRow(query, id).Scan(
|
||||
&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
|
||||
&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get repo: %w", err)
|
||||
}
|
||||
|
||||
if imageTag.Valid {
|
||||
repo.ImageTag = &imageTag.String
|
||||
}
|
||||
if lastError.Valid {
|
||||
repo.LastError = &lastError.String
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
func (rs *RepoStore) Update(repo *model.Repo) error {
|
||||
query := `
|
||||
UPDATE repos
|
||||
SET repo_url = ?, status = ?, user_id = ?, type = ?, image_tag = ?, last_error = ?, updated_at = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
repo.UpdatedAt = time.Now()
|
||||
result, err := rs.db.Exec(query, repo.RepoURL, repo.Status, repo.UserID, repo.Type, repo.ImageTag, repo.LastError, repo.UpdatedAt, repo.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update repo: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("no rows affected, repo not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *RepoStore) Delete(id int64) error {
|
||||
query := `DELETE FROM repos WHERE id = ?`
|
||||
|
||||
result, err := rs.db.Exec(query, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete repo: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("no rows affected, repo not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *RepoStore) ListByStatus(status model.RepoStatus) ([]*model.Repo, error) {
|
||||
query := `
|
||||
SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
|
||||
FROM repos
|
||||
WHERE status = ?
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
rows, err := rs.db.Query(query, status)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list repos by status: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var repos []*model.Repo
|
||||
for rows.Next() {
|
||||
repo := &model.Repo{}
|
||||
var imageTag, lastError sql.NullString
|
||||
|
||||
err := rows.Scan(
|
||||
&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
|
||||
&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan repo row: %w", err)
|
||||
}
|
||||
|
||||
if imageTag.Valid {
|
||||
repo.ImageTag = &imageTag.String
|
||||
}
|
||||
if lastError.Valid {
|
||||
repo.LastError = &lastError.String
|
||||
}
|
||||
|
||||
repos = append(repos, repo)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("error iterating repo rows: %w", err)
|
||||
}
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
func (rs *RepoStore) ListByUser(userID string) ([]*model.Repo, error) {
|
||||
query := `
|
||||
SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
|
||||
FROM repos
|
||||
WHERE user_id = ?
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
rows, err := rs.db.Query(query, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list repos by user: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var repos []*model.Repo
|
||||
for rows.Next() {
|
||||
repo := &model.Repo{}
|
||||
var imageTag, lastError sql.NullString
|
||||
|
||||
err := rows.Scan(
|
||||
&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
|
||||
&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan repo row: %w", err)
|
||||
}
|
||||
|
||||
if imageTag.Valid {
|
||||
repo.ImageTag = &imageTag.String
|
||||
}
|
||||
if lastError.Valid {
|
||||
repo.LastError = &lastError.String
|
||||
}
|
||||
|
||||
repos = append(repos, repo)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("error iterating repo rows: %w", err)
|
||||
}
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
45
internal/db/sqlite.go
Normal file
45
internal/db/sqlite.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func Migrate(db *sql.DB) error {
|
||||
migrationDir := "./migrations"
|
||||
|
||||
files, err := ioutil.ReadDir(migrationDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read migration directory: %w", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) != ".sql" {
|
||||
continue
|
||||
}
|
||||
|
||||
migrationPath := filepath.Join(migrationDir, file.Name())
|
||||
log.Printf("Running migration: %s", file.Name())
|
||||
|
||||
if err := runMigration(db, migrationPath); err != nil {
|
||||
return fmt.Errorf("failed to run migration %s: %w", file.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runMigration(db *sql.DB, path string) error {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec(string(content))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user