Ejemplo n.º 1
0
func (r *JobRepo) Add(job *ct.Job) error {
	// TODO: actually validate
	err := r.db.QueryRow(
		"job_insert",
		job.ID,
		job.UUID,
		job.HostID,
		job.AppID,
		job.ReleaseID,
		job.Type,
		string(job.State),
		job.Meta,
		job.ExitStatus,
		job.HostError,
		job.RunAt,
		job.Restarts,
		job.Args,
	).Scan(&job.CreatedAt, &job.UpdatedAt)
	if postgres.IsPostgresCode(err, postgres.CheckViolation) {
		return ct.ValidationError{Field: "state", Message: err.Error()}
	}
	if err != nil {
		return err
	}

	// create a job event, ignoring possible duplications
	uniqueID := strings.Join([]string{job.UUID, string(job.State)}, "|")
	err = r.db.Exec("event_insert_unique", job.AppID, job.UUID, uniqueID, string(ct.EventTypeJob), job)
	return err
}
Ejemplo n.º 2
0
// TestMigrateJobStates checks that migrating to ID 9 does not break existing
// job records
func (MigrateSuite) TestMigrateJobStates(c *C) {
	db := setupTestDB(c, "controllertest_migrate_job_states")
	m := &testMigrator{c: c, db: db}

	// start from ID 7
	m.migrateTo(7)

	// insert a job
	hostID := "host1"
	uuid := random.UUID()
	jobID := cluster.GenerateJobID(hostID, uuid)
	appID := random.UUID()
	releaseID := random.UUID()
	c.Assert(db.Exec(`INSERT INTO apps (app_id, name) VALUES ($1, $2)`, appID, "migrate-app"), IsNil)
	c.Assert(db.Exec(`INSERT INTO releases (release_id) VALUES ($1)`, releaseID), IsNil)
	c.Assert(db.Exec(`INSERT INTO job_cache (job_id, app_id, release_id, state) VALUES ($1, $2, $3, $4)`, jobID, appID, releaseID, "up"), IsNil)

	// migrate to 8 and check job states are still constrained
	m.migrateTo(8)
	err := db.Exec(`UPDATE job_cache SET state = 'foo' WHERE job_id = $1`, jobID)
	c.Assert(err, NotNil)
	if !postgres.IsPostgresCode(err, postgres.ForeignKeyViolation) {
		c.Fatalf("expected postgres foreign key violation, got %s", err)
	}

	// migrate to 9 and check job IDs are correct, pending state is valid
	m.migrateTo(9)
	var clusterID, dbUUID, dbHostID string
	c.Assert(db.QueryRow("SELECT cluster_id, job_id, host_id FROM job_cache WHERE cluster_id = $1", jobID).Scan(&clusterID, &dbUUID, &dbHostID), IsNil)
	c.Assert(clusterID, Equals, jobID)
	c.Assert(dbUUID, Equals, uuid)
	c.Assert(dbHostID, Equals, hostID)
	c.Assert(db.Exec(`UPDATE job_cache SET state = 'pending' WHERE job_id = $1`, uuid), IsNil)
}
Ejemplo n.º 3
0
func (d *pgDataStore) Add(r *router.Route) (err error) {
	switch d.tableName {
	case tableNameHTTP:
		err = d.pgx.QueryRow(
			sqlAddRouteHTTP,
			r.ParentRef,
			r.Service,
			r.Leader,
			r.Domain,
			r.TLSCert,
			r.TLSKey,
			r.Sticky,
			r.Path,
		).Scan(&r.ID, &r.CreatedAt, &r.UpdatedAt)
	case tableNameTCP:
		err = d.pgx.QueryRow(
			sqlAddRouteTCP,
			r.ParentRef,
			r.Service,
			r.Leader,
			r.Port,
		).Scan(&r.ID, &r.CreatedAt, &r.UpdatedAt)
	}
	r.Type = d.routeType
	if postgres.IsUniquenessError(err, "") {
		err = ErrConflict
	} else if postgres.IsPostgresCode(err, postgres.RaiseException) {
		err = ErrInvalid
	}
	return err
}
Ejemplo n.º 4
0
func (d *pgDataStore) Remove(id string) error {
	_, err := d.pgx.Exec(fmt.Sprintf(sqlRemoveRoute, d.tableName), id)
	if postgres.IsPostgresCode(err, postgres.RaiseException) {
		err = ErrInvalid
	}
	return err
}
Ejemplo n.º 5
0
func (d *pgDataStore) Remove(id string) error {
	var query string
	switch d.tableName {
	case tableNameTCP:
		query = "delete_tcp_route"
	case tableNameHTTP:
		query = "delete_http_route"
	}
	_, err := d.pgx.Exec(query, id)
	if postgres.IsPostgresCode(err, postgres.RaiseException) {
		err = ErrInvalid
	}
	return err
}
Ejemplo n.º 6
0
func (c *controllerAPI) DeleteRelease(ctx context.Context, w http.ResponseWriter, req *http.Request) {
	app := c.getApp(ctx)
	release, err := c.getRelease(ctx)
	if err != nil {
		respondWithError(w, err)
		return
	}
	if err := c.releaseRepo.Delete(app, release); err != nil {
		if postgres.IsPostgresCode(err, postgres.CheckViolation) {
			err = ct.ValidationError{
				Message: "cannot delete current app release",
			}
		}
		respondWithError(w, err)
		return
	}
	w.WriteHeader(200)
}
Ejemplo n.º 7
0
func (d *pgDataStore) Add(r *router.Route) (err error) {
	switch d.tableName {
	case tableNameHTTP:
		err = d.addHTTP(r)
	case tableNameTCP:
		err = d.addTCP(r)
	}
	r.Type = d.routeType
	if err != nil {
		if postgres.IsUniquenessError(err, "") {
			err = ErrConflict
		} else if postgres.IsPostgresCode(err, postgres.RaiseException) {
			err = ErrInvalid
		}
		return err
	}
	return nil
}