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

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kwk committed Oct 24, 2018
1 parent f257b1a commit 69e24a6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 127 deletions.
2 changes: 1 addition & 1 deletion migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func MigrateToNextVersion(tx *sql.Tx, nextVersion *int64, m Migrations, catalog
// Apply all the updates of the next version
for j := range m[*nextVersion] {
if err := m[*nextVersion][j](tx); err != nil {
return errs.Errorf("failed to execute migration of step %d of version %d: %s\n", j, *nextVersion, err)
return errs.Errorf("failed to execute migration step %d of version %d: %s\n", j, *nextVersion, err)
}
}

Expand Down
82 changes: 36 additions & 46 deletions migration/migration_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,72 +1490,62 @@ func testMigration111CascadingSoftDelete(t *testing.T) {

t.Run("setup test data to migrate", func(t *testing.T) {
require.Nil(t, runSQLscript(sqlDB, "111-cascading-soft-delete.sql",
areaID,
areaDeletedID,
commentDeletedID,
commentID,
iterID,
iterDeletedID,
labelID,
labelDeletedID,
spaceID,
spaceDeletedID,
spaceTemplateID,
spaceTemplateDeletedID,
workItemID,
workItemDeletedID,
workItemLinkID,
workItemLinkDeletedID,
workItemLinkTypeID,
workItemLinkTypeDeletedID,
workItemTypeID,
workItemTypeDeletedID,
areaDeletedID.String(),
areaID.String(),
commentDeletedID.String(),
commentID.String(),
iterDeletedID.String(),
iterID.String(),
labelDeletedID.String(),
labelID.String(),
spaceDeletedID.String(),
spaceID.String(),
spaceTemplateDeletedID.String(),
spaceTemplateID.String(),
workItemDeletedID.String(),
workItemID.String(),
workItemLinkDeletedID.String(),
workItemLinkID.String(),
workItemLinkTypeDeletedID.String(),
workItemLinkTypeID.String(),
workItemTypeDeletedID.String(),
workItemTypeID.String(),
))
})

// Helper functions
exists := func(t *testing.T, table string, id uuid.UUID) bool {
q := fmt.Sprintf("SELECT 1 FROM %s WHERE id = '%s'", table, id)
row := sqlDB.QueryRow(q)
require.NotNil(t, row)
require.NotNil(t, row, "exists table: %s, id: %s", table, id)
var p int32
err := row.Scan(&p)
require.NoError(t, err, "%+v", err)
require.NoError(t, err, "exists table: %s, id: %s, err: %+v", table, id, err)
return p == 1
}
existsButIsDeleted := func(t *testing.T, table string, id uuid.UUID) bool {
q := fmt.Sprintf("SELECT 1 FROM %s WHERE id = '%s' AND deleted_at IS NOT NULL", table, id)
row := sqlDB.QueryRow(q)
require.NotNil(t, row)
require.NotNil(t, row, "existsButIsDeleted table: %s, id: %s", table, id)
var p int32
err := row.Scan(&p)
require.NoError(t, err, "%+v", err)
require.NoError(t, err, "existsButIsDeleted table: %s, id: %s (comment: %s, comment (deleted): %s), err: %+v", table, id, areaID, areaDeletedID, err)
return p == 1
}
checkEntitiesExist := func(t *testing.T, existFunc func(t *testing.T, table string, id uuid.UUID) bool) {
t.Run("check that all entities exist", func(t *testing.T) {
require.True(t, existFunc(t, "areas", areaID))
require.True(t, existsButIsDeleted(t, "areas", areaDeletedID))
require.True(t, existFunc(t, "work_item_comments", commentID))
require.True(t, existsButIsDeleted(t, "work_item_comments", commentDeletedID))
require.True(t, existFunc(t, "iterations", iterID))
require.True(t, existsButIsDeleted(t, "iterations", iterDeletedID))
require.True(t, existFunc(t, "labels", labelID))
require.True(t, existsButIsDeleted(t, "labels", labelDeletedID))
require.True(t, existFunc(t, "spaces", spaceID))
require.True(t, existsButIsDeleted(t, "spaces", spaceDeletedID))
require.True(t, existFunc(t, "space_templates", spaceTemplateID))
require.True(t, existsButIsDeleted(t, "space_templates", spaceTemplateDeletedID))
require.True(t, existFunc(t, "work_items", workItemID))
require.True(t, existsButIsDeleted(t, "work_items", workItemDeletedID))
require.True(t, existFunc(t, "work_item_links", workItemLinkID))
require.True(t, existsButIsDeleted(t, "work_item_links", workItemLinkDeletedID))
require.True(t, existFunc(t, "work_item_link_types", workItemLinkTypeID))
require.True(t, existsButIsDeleted(t, "work_item_link_types", workItemLinkTypeDeletedID))
require.True(t, existFunc(t, "work_item_types", workItemTypeID))
require.True(t, existsButIsDeleted(t, "work_item_types", workItemTypeDeletedID))
// require.True(t, existFunc(t, "areas", areaID), "area missing: %s", areaID)
// require.True(t, existFunc(t, "comments", commentID), "comment missing: %s", commentID)
// require.True(t, existFunc(t, "iterations", iterID), "iteration missing: %s", iterID)
// require.True(t, existFunc(t, "labels", labelID), "label missing: %s", labelID)
require.True(t, existFunc(t, "spaces", spaceID), "space missing: %s", spaceID)
require.True(t, existFunc(t, "space_templates", spaceTemplateID), "space template missing: %s", spaceTemplateID)
// require.True(t, existFunc(t, "work_items", workItemID), "work item missing: %s", workItemID)
// require.True(t, existFunc(t, "work_item_links", workItemLinkID), "work item link missing: %s", workItemLinkID)
// require.True(t, existFunc(t, "work_item_link_types", workItemLinkTypeID), "work item link type missing: %s", workItemLinkTypeID)
// require.True(t, existFunc(t, "work_item_types", workItemTypeID), "work item type missing: %s", workItemTypeID)
})
})
}

t.Run("before migration", func(t *testing.T) {
checkEntitiesExist(t, exists)
Expand All @@ -1565,7 +1555,7 @@ func testMigration111CascadingSoftDelete(t *testing.T) {
})
t.Run("after migration", func(t *testing.T) {
checkEntitiesExist(t, exists)
require.Nil(t, runSQLscript(sqlDB, "111-soft-delete-space-template.sql", spaceTemplateID))
require.Nil(t, runSQLscript(sqlDB, "111-soft-delete-space-template.sql", spaceTemplateID.String()))
checkEntitiesExist(t, existsButIsDeleted)
})

Expand Down
53 changes: 29 additions & 24 deletions migration/sql-files/111-cascading-soft-delete.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- Add missing foreign key constraint from comment to work item
-- ALTER TABLE comments ADD FOREIGN KEY (parent_id) REFERENCES work_items(id) ON DELETE CASCADE;

CREATE OR REPLACE FUNCTION archive_record()
-- archive_record() can be use used as the trigger function on all tables
-- that want to archive their data into a separate *_archive table after
Expand Down Expand Up @@ -53,37 +56,39 @@ END;
$$ LANGUAGE plpgsql;

-- Create archive tables
CREATE TABLE areas_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (areas);
CREATE TABLE comments_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (comments);
CREATE TABLE iterations_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (iterations);
CREATE TABLE labels_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (lables);
-- CREATE TABLE areas_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (areas);
-- CREATE TABLE comments_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (comments);
-- CREATE TABLE iterations_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (iterations);
-- CREATE TABLE labels_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (labels);
CREATE TABLE space_templates_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (space_templates);
CREATE TABLE spaces_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (spaces);
CREATE TABLE work_item_link_types_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_link_types);
CREATE TABLE work_item_links_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_links);
CREATE TABLE work_item_types_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_types);
CREATE TABLE work_items_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_items);
-- CREATE TABLE work_item_link_types_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_link_types);
-- CREATE TABLE work_item_links_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_links);
-- CREATE TABLE work_item_types_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_item_types);
-- CREATE TABLE work_items_archive (CHECK (deleted_at IS NOT NULL)) INHERITS (work_items);

-- Setup triggers
CREATE TRIGGER archive_areas AFTER UPDATE OF deleted_at OR DELETE ON areas FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_comments AFTER UPDATE OF deleted_at OR DELETE ON comments FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_iterations AFTER UPDATE OF deleted_at OR DELETE ON iterations FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_labels AFTER UPDATE OF deleted_at OR DELETE ON labels FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_areas AFTER UPDATE OF deleted_at OR DELETE ON areas FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_comments AFTER UPDATE OF deleted_at OR DELETE ON comments FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_iterations AFTER UPDATE OF deleted_at OR DELETE ON iterations FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_labels AFTER UPDATE OF deleted_at OR DELETE ON labels FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_space_templates AFTER UPDATE OF deleted_at OR DELETE ON space_templates FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_spaces AFTER UPDATE OF deleted_at OR DELETE ON spaces FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_work_item_link_types AFTER UPDATE OF deleted_at OR DELETE ON work_item_link_types FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_work_item_links AFTER UPDATE OF deleted_at OR DELETE ON work_item_links FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_work_item_types AFTER UPDATE OF deleted_at OR DELETE ON work_item_types FOR EACH ROW EXECUTE PROCEDURE archive_record();
CREATE TRIGGER archive_work_items AFTER UPDATE OF deleted_at OR DELETE ON work_items FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_work_item_link_types AFTER UPDATE OF deleted_at OR DELETE ON work_item_link_types FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_work_item_links AFTER UPDATE OF deleted_at OR DELETE ON work_item_links FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_work_item_types AFTER UPDATE OF deleted_at OR DELETE ON work_item_types FOR EACH ROW EXECUTE PROCEDURE archive_record();
-- CREATE TRIGGER archive_work_items AFTER UPDATE OF deleted_at OR DELETE ON work_items FOR EACH ROW EXECUTE PROCEDURE archive_record();



-- Archive all deleted records
DELETE FROM areas WHERE deleted_at IS NOT NULL;
DELETE FROM comments WHERE deleted_at IS NOT NULL;
DELETE FROM iterations WHERE deleted_at IS NOT NULL;
DELETE FROM labels WHERE deleted_at IS NOT NULL;
-- DELETE FROM areas WHERE deleted_at IS NOT NULL;
-- DELETE FROM comments WHERE deleted_at IS NOT NULL;
-- DELETE FROM iterations WHERE deleted_at IS NOT NULL;
-- DELETE FROM labels WHERE deleted_at IS NOT NULL;
DELETE FROM space_templates WHERE deleted_at IS NOT NULL;
DELETE FROM spaces WHERE deleted_at IS NOT NULL;
DELETE FROM work_item_link_types WHERE deleted_at IS NOT NULL;
DELETE FROM work_item_links WHERE deleted_at IS NOT NULL;
DELETE FROM work_item_types WHERE deleted_at IS NOT NULL;
DELETE FROM work_items WHERE deleted_at IS NOT NULL;
-- DELETE FROM work_item_link_types WHERE deleted_at IS NOT NULL;
-- DELETE FROM work_item_links WHERE deleted_at IS NOT NULL;
-- DELETE FROM work_item_types WHERE deleted_at IS NOT NULL;
-- DELETE FROM work_items WHERE deleted_at IS NOT NULL;
56 changes: 0 additions & 56 deletions migration/sql-test-files/111-cascading-soft-delete-update.sql

This file was deleted.

Loading

0 comments on commit 69e24a6

Please sign in to comment.