Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Add resource_spaces
Browse files Browse the repository at this point in the history
This is a revert of a revert 232945e.

concourse/concourse#1764

Signed-off-by: Topher Bullock <cbullock@pivotal.io>
  • Loading branch information
mhuangpivotal committed Apr 30, 2018
1 parent 310a311 commit 712398e
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 133 deletions.
36 changes: 20 additions & 16 deletions db/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,24 +801,25 @@ func (b *build) Resources() ([]BuildInput, []BuildOutput, error) {
outputs := []BuildOutput{}

rows, err := b.conn.Query(`
SELECT i.name, r.name, v.type, v.version, v.metadata,
SELECT i.name, r.name, vr.type, vr.version, vr.metadata,
NOT EXISTS (
SELECT 1
FROM build_inputs ci, builds cb
WHERE versioned_resource_id = v.id
WHERE versioned_resource_id = vr.id
AND cb.job_id = b.job_id
AND ci.build_id = cb.id
AND ci.build_id < b.id
)
FROM versioned_resources v, build_inputs i, builds b, resources r
FROM versioned_resources vr, build_inputs i, builds b, resource_spaces rs, resources r
WHERE b.id = $1
AND i.build_id = b.id
AND i.versioned_resource_id = v.id
AND r.id = v.resource_id
AND i.versioned_resource_id = vr.id
AND rs.id = vr.resource_space_id
AND r.id = rs.resource_id
AND NOT EXISTS (
SELECT 1
FROM build_outputs o
WHERE o.versioned_resource_id = v.id
WHERE o.versioned_resource_id = vr.id
AND o.build_id = i.build_id
)
`, b.id)
Expand Down Expand Up @@ -857,12 +858,13 @@ func (b *build) Resources() ([]BuildInput, []BuildOutput, error) {
}

rows, err = b.conn.Query(`
SELECT r.name, v.type, v.version, v.metadata
FROM versioned_resources v, build_outputs o, builds b, resources r
SELECT r.name, vr.type, vr.version, vr.metadata
FROM versioned_resources vr, build_outputs o, builds b, resource_spaces rs, resources r
WHERE b.id = $1
AND o.build_id = b.id
AND o.versioned_resource_id = v.id
AND r.id = v.resource_id
AND o.versioned_resource_id = vr.id
AND rs.id = vr.resource_space_id
AND r.id = rs.resource_id
`, b.id)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -907,10 +909,11 @@ func (b *build) GetVersionedResources() (SavedVersionedResources, error) {
r.name,
vr.modified_time
FROM builds b
INNER JOIN jobs j ON b.job_id = j.id
INNER JOIN jobs j ON j.id = b.job_id
INNER JOIN build_inputs bi ON bi.build_id = b.id
INNER JOIN versioned_resources vr ON bi.versioned_resource_id = vr.id
INNER JOIN resources r ON vr.resource_id = r.id
INNER JOIN versioned_resources vr ON vr.id = bi.versioned_resource_id
INNER JOIN resource_spaces rs ON rs.id = vr.resource_space_id
INNER JOIN resources r ON r.id = rs.resource_id
WHERE b.id = $1
UNION ALL
Expand All @@ -923,10 +926,11 @@ func (b *build) GetVersionedResources() (SavedVersionedResources, error) {
r.name,
vr.modified_time
FROM builds b
INNER JOIN jobs j ON b.job_id = j.id
INNER JOIN jobs j ON j.id = b.job_id
INNER JOIN build_outputs bo ON bo.build_id = b.id
INNER JOIN versioned_resources vr ON bo.versioned_resource_id = vr.id
INNER JOIN resources r ON vr.resource_id = r.id
INNER JOIN versioned_resources vr ON vr.id = bo.versioned_resource_id
INNER JOIN resource_spaces rs ON rs.id = vr.resource_space_id
INNER JOIN resources r ON r.id = rs.resource_id
WHERE b.id = $1
`)
}
Expand Down
7 changes: 4 additions & 3 deletions db/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,12 @@ func (j *job) updatePausedJob(pause bool) error {
}

func (j *job) getBuildInputs(table string) ([]BuildInput, error) {
rows, err := psql.Select("i.input_name, i.first_occurrence, r.name, v.type, v.version, v.metadata").
rows, err := psql.Select("i.input_name, i.first_occurrence, r.name, vr.type, vr.version, vr.metadata").
From(table + " i").
Join("jobs j ON i.job_id = j.id").
Join("versioned_resources v ON v.id = i.version_id").
Join("resources r ON r.id = v.resource_id").
Join("versioned_resources vr ON vr.id = i.version_id").
Join("resource_spaces rs ON rs.id = vr.resource_space_id").
Join("resources r ON r.id = rs.resource_id").
Where(sq.Eq{
"j.name": j.name,
"j.pipeline_id": j.pipelineID,
Expand Down
102 changes: 74 additions & 28 deletions db/migration/bindata.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions db/migration/migrations/1511810746_create_resource_spaces.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN;

ALTER INDEX versioned_resources_resource_space_id_idx RENAME TO versioned_resources_resource_id_idx ;

ALTER INDEX versioned_resources_resource_space_id_type_version RENAME TO versioned_resources_resource_id_type_version;

ALTER TABLE versioned_resources DROP CONSTRAINT resource_space_id_fkey;

ALTER TABLE versioned_resources RENAME resource_space_id TO resource_id;

ALTER TABLE versioned_resources ADD CONSTRAINT fkey_resource_id FOREIGN KEY (resource_id) REFERENCES resources(id) ON DELETE CASCADE;

DROP TABLE resource_spaces;

COMMIT;
24 changes: 24 additions & 0 deletions db/migration/migrations/1511810746_create_resource_spaces.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BEGIN;

CREATE TABLE resource_spaces (
id serial PRIMARY KEY,
resource_id int REFERENCES resources (id) ON DELETE CASCADE,
name text NOT NULL,
UNIQUE (resource_id, name)
);

INSERT INTO resource_spaces(id, resource_id, name) SELECT id, id, 'default' from resources;

SELECT setval('resource_spaces_id_seq', (SELECT max(id) from resource_spaces));

ALTER TABLE versioned_resources RENAME resource_id TO resource_space_id;

ALTER TABLE versioned_resources DROP CONSTRAINT fkey_resource_id;

ALTER TABLE versioned_resources ADD CONSTRAINT resource_space_id_fkey FOREIGN KEY (resource_space_id) REFERENCES resource_spaces (id) ON DELETE CASCADE;

ALTER INDEX versioned_resources_resource_id_type_version RENAME TO versioned_resources_resource_space_id_type_version;

ALTER INDEX versioned_resources_resource_id_idx RENAME TO versioned_resources_resource_space_id_idx;

COMMIT;
Loading

0 comments on commit 712398e

Please sign in to comment.