Migrate ingest and API paths to string IDs
This commit is contained in:
@@ -5,14 +5,19 @@ import (
|
||||
"database/sql"
|
||||
|
||||
"reanimator/internal/domain"
|
||||
"reanimator/internal/idgen"
|
||||
)
|
||||
|
||||
type TicketRepository struct {
|
||||
db *sql.DB
|
||||
db *sql.DB
|
||||
idgen *idgen.Generator
|
||||
}
|
||||
|
||||
func NewTicketRepository(db *sql.DB) *TicketRepository {
|
||||
return &TicketRepository{db: db}
|
||||
return &TicketRepository{
|
||||
db: db,
|
||||
idgen: idgen.NewGenerator(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *TicketRepository) BeginTx(ctx context.Context) (*sql.Tx, error) {
|
||||
@@ -32,17 +37,25 @@ func execerFor(db *sql.DB, tx *sql.Tx) execer {
|
||||
return db
|
||||
}
|
||||
|
||||
func (r *TicketRepository) Upsert(ctx context.Context, tx *sql.Tx, ticket domain.Ticket) (int64, error) {
|
||||
func (r *TicketRepository) Upsert(ctx context.Context, tx *sql.Tx, ticket domain.Ticket) (string, error) {
|
||||
execer := execerFor(r.db, tx)
|
||||
_, err := execer.ExecContext(ctx,
|
||||
`INSERT INTO tickets (source, external_id, title, status, opened_at, closed_at, url)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
|
||||
// Generate ID for new tickets
|
||||
id, err := r.idgen.Generate(ctx, idgen.Ticket)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = execer.ExecContext(ctx,
|
||||
`INSERT INTO tickets (id, source, external_id, title, status, opened_at, closed_at, url)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title = VALUES(title),
|
||||
status = VALUES(status),
|
||||
opened_at = VALUES(opened_at),
|
||||
closed_at = VALUES(closed_at),
|
||||
url = VALUES(url)`,
|
||||
id,
|
||||
ticket.Source,
|
||||
ticket.ExternalID,
|
||||
ticket.Title,
|
||||
@@ -52,40 +65,48 @@ func (r *TicketRepository) Upsert(ctx context.Context, tx *sql.Tx, ticket domain
|
||||
ticket.URL,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
var id int64
|
||||
var resultID string
|
||||
row := execer.QueryRowContext(ctx,
|
||||
`SELECT id FROM tickets WHERE source = ? AND external_id = ?`,
|
||||
ticket.Source,
|
||||
ticket.ExternalID,
|
||||
)
|
||||
if err := row.Scan(&id); err != nil {
|
||||
return 0, err
|
||||
if err := row.Scan(&resultID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
return resultID, nil
|
||||
}
|
||||
|
||||
func (r *TicketRepository) LinkToAsset(ctx context.Context, tx *sql.Tx, ticketID, assetID int64) error {
|
||||
func (r *TicketRepository) LinkToAsset(ctx context.Context, tx *sql.Tx, ticketID, assetID string) error {
|
||||
execer := execerFor(r.db, tx)
|
||||
_, err := execer.ExecContext(ctx,
|
||||
`INSERT INTO ticket_links (ticket_id, asset_id)
|
||||
VALUES (?, ?)
|
||||
|
||||
// Generate ID for new ticket link
|
||||
id, err := r.idgen.Generate(ctx, idgen.TicketLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = execer.ExecContext(ctx,
|
||||
`INSERT INTO ticket_links (id, ticket_id, machine_id)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE ticket_id = VALUES(ticket_id)`,
|
||||
id,
|
||||
ticketID,
|
||||
assetID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *TicketRepository) ListByAsset(ctx context.Context, assetID int64) ([]domain.Ticket, error) {
|
||||
func (r *TicketRepository) ListByAsset(ctx context.Context, assetID string) ([]domain.Ticket, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT t.id, t.source, t.external_id, t.title, t.status, t.opened_at, t.closed_at, t.url, t.created_at, t.updated_at
|
||||
FROM ticket_links tl
|
||||
JOIN tickets t ON t.id = tl.ticket_id
|
||||
WHERE tl.asset_id = ?
|
||||
ORDER BY t.updated_at DESC, t.id DESC`,
|
||||
WHERE tl.machine_id = ?
|
||||
ORDER BY t.updated_at DESC, t.created_at DESC`,
|
||||
assetID,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -131,7 +152,7 @@ func (r *TicketRepository) ListAll(ctx context.Context, limit int) ([]domain.Tic
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT t.id, t.source, t.external_id, t.title, t.status, t.opened_at, t.closed_at, t.url, t.created_at, t.updated_at
|
||||
FROM tickets t
|
||||
ORDER BY t.updated_at DESC, t.id DESC
|
||||
ORDER BY t.updated_at DESC, t.created_at DESC
|
||||
LIMIT ?`,
|
||||
limit,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user