github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/worker_artifact.go (about)

     1  package db
     2  
     3  import (
     4  	"database/sql"
     5  	"errors"
     6  	"time"
     7  
     8  	sq "github.com/Masterminds/squirrel"
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	"github.com/lib/pq"
    11  )
    12  
    13  //go:generate counterfeiter . WorkerArtifact
    14  
    15  // TODO-L Can this be consolidated with atc/runtime/types.go -> Artifact OR Alternatively, there shouldn't be a volume reference here
    16  type WorkerArtifact interface {
    17  	ID() int
    18  	Name() string
    19  	BuildID() int
    20  	CreatedAt() time.Time
    21  	Volume(teamID int) (CreatedVolume, bool, error)
    22  }
    23  
    24  type artifact struct {
    25  	conn Conn
    26  
    27  	id        int
    28  	name      string
    29  	buildID   int
    30  	createdAt time.Time
    31  }
    32  
    33  func (a *artifact) ID() int              { return a.id }
    34  func (a *artifact) Name() string         { return a.name }
    35  func (a *artifact) BuildID() int         { return a.buildID }
    36  func (a *artifact) CreatedAt() time.Time { return a.createdAt }
    37  
    38  func (a *artifact) Volume(teamID int) (CreatedVolume, bool, error) {
    39  	where := map[string]interface{}{
    40  		"v.team_id":            teamID,
    41  		"v.worker_artifact_id": a.id,
    42  	}
    43  
    44  	_, created, err := getVolume(a.conn, where)
    45  	if err != nil {
    46  		return nil, false, err
    47  	}
    48  
    49  	if created == nil {
    50  		return nil, false, nil
    51  	}
    52  
    53  	return created, true, nil
    54  }
    55  
    56  func saveWorkerArtifact(tx Tx, conn Conn, atcArtifact atc.WorkerArtifact) (WorkerArtifact, error) {
    57  
    58  	var artifactID int
    59  
    60  	values := map[string]interface{}{
    61  		"name": atcArtifact.Name,
    62  	}
    63  
    64  	if atcArtifact.BuildID != 0 {
    65  		values["build_id"] = atcArtifact.BuildID
    66  	}
    67  
    68  	err := psql.Insert("worker_artifacts").
    69  		SetMap(values).
    70  		Suffix("RETURNING id").
    71  		RunWith(tx).
    72  		QueryRow().
    73  		Scan(&artifactID)
    74  
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	artifact, found, err := getWorkerArtifact(tx, conn, artifactID)
    80  
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	if !found {
    86  		return nil, errors.New("Not found")
    87  	}
    88  
    89  	return artifact, nil
    90  }
    91  
    92  func getWorkerArtifact(tx Tx, conn Conn, id int) (WorkerArtifact, bool, error) {
    93  	var (
    94  		createdAtTime pq.NullTime
    95  		buildID       sql.NullInt64
    96  	)
    97  
    98  	artifact := &artifact{conn: conn}
    99  
   100  	err := psql.Select("id", "created_at", "name", "build_id").
   101  		From("worker_artifacts").
   102  		Where(sq.Eq{
   103  			"id": id,
   104  		}).
   105  		RunWith(tx).
   106  		QueryRow().
   107  		Scan(&artifact.id, &createdAtTime, &artifact.name, &buildID)
   108  	if err != nil {
   109  		if err == sql.ErrNoRows {
   110  			return nil, false, nil
   111  		}
   112  
   113  		return nil, false, err
   114  	}
   115  
   116  	artifact.createdAt = createdAtTime.Time
   117  	artifact.buildID = int(buildID.Int64)
   118  
   119  	return artifact, true, nil
   120  }