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

Commit

Permalink
Adds work_item_type_id to existing tracker_queries; Adds tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
DhritiShikhar committed Dec 3, 2018
1 parent 936cfc9 commit 68498a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
22 changes: 21 additions & 1 deletion migration/migration_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestMigrations(t *testing.T) {
t.Run("TestMigration108", testMigration108NumberColumnForArea)
t.Run("TestMigration109", testMigration109NumberColumnForIteration)
t.Run("TestMigration110", testMigration110TrackerQueryID)

t.Run("TestMigration111", testMigration111WITinTrackerQuery)
// Perform the migration
err = migration.Migrate(sqlDB, databaseName)
require.NoError(t, err, "failed to execute database migration")
Expand Down Expand Up @@ -1401,6 +1401,26 @@ func testMigration110TrackerQueryID(t *testing.T) {
}
require.True(t, checkTqConstraint(t, "tracker_queries", "PRIMARY KEY"))
}
func testMigration111WITinTrackerQuery(t *testing.T) {
migrateToVersion(t, sqlDB, migrations[:112], 111)
require.True(t, dialect.HasColumn("tracker_queries", "work_item_type_id"))

// check foreign key to work_item_types(id) exists
checkTqConstraint := func(t *testing.T, table string, constraintName string) bool {
q := fmt.Sprintf("select constraint_name from information_schema.table_constraints where table_name = '%s' and constraint_name = '%s';", table, constraintName)

This comment has been minimized.

Copy link
@kwk

kwk Dec 4, 2018

Collaborator

Why not simply use dialect.Has... instead?

This comment has been minimized.

Copy link
@DhritiShikhar

DhritiShikhar Dec 4, 2018

Author Contributor

Thanks for pointing out 6f48605

row := sqlDB.QueryRow(q)
require.NotNil(t, row)

var tqConstraint string
err := row.Scan(&tqConstraint)
require.NoError(t, err, "%+v", err)
if tqConstraint == "tracker_queries_work_item_type_id_fkey" {
return true
}
return false
}
require.True(t, checkTqConstraint(t, "tracker_queries", "tracker_queries_work_item_type_id_fkey"))
}

// runSQLscript loads the given filename from the packaged SQL test files and
// executes it on the given database. Golang text/template module is used
Expand Down
27 changes: 26 additions & 1 deletion migration/sql-files/111-add-wit-to-trackerquery.sql
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
ALTER TABLE tracker_queries ADD COLUMN work_item_type_id uuid REFERENCES work_item_types(id);
CREATE OR REPLACE FUNCTION adds_wit() RETURNS void as $$
-- adds_wit() function adds work_item_type to existing tracker_queries in database

This comment has been minimized.

Copy link
@kwk

kwk Dec 4, 2018

Collaborator

Please put the comment inside the BEGIN END . Only then it will be listed when you print the function on psql.

This comment has been minimized.

Copy link
@DhritiShikhar

DhritiShikhar Dec 4, 2018

Author Contributor

@kwk as per discussion, I have removed this function 97f2247

DECLARE
r RECORD;
c CURSOR FOR SELECT id, space_id, work_item_type_id from tracker_queries;
BEGIN
open c;
FOR r in FETCH ALL FROM c LOOP
UPDATE tracker_queries as tq
SET work_item_type_id = wit.id
FROM work_item_types as wit, spaces as sp
WHERE
tq.space_id = sp.id
AND sp.space_template_id = wit.space_template_id
AND wit.can_construct = true;
END LOOP;
close c;
END $$ LANGUAGE plpgsql;

DO $$ BEGIN
ALTER TABLE tracker_queries ADD COLUMN work_item_type_id uuid REFERENCES work_item_types(id) ON DELETE CASCADE;
PERFORM adds_wit();
DROP FUNCTION adds_wit();
ALTER TABLE tracker_queries ALTER COLUMN work_item_type_id set not null;
END $$;

0 comments on commit 68498a6

Please sign in to comment.