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

Commit

Permalink
Remove unknown link types and link categories (#1730)
Browse files Browse the repository at this point in the history
The following link types exist in the current production database but they are not known to the code and therefore we re-assign all links associated to those link types with their appropriate *known* link type and later remove these unknown link types.

aad2a4ad-d601-4104-9804-2c977ca2e0c1
355b647b-adc5-46b3-b297-cc54bc0554e6
7479a9b9-8607-46fa-9535-d448fa8768ab

There also exist two unknown link categories [see here](https://api.openshift.io/api/workitemlinkcategories):

04b89525-84ff-406c-8e18-d990936bdb74
6329fbb3-399b-4a78-ae3b-6a6faa8b1084

Those will be removed as well.

After this PR is merged, we should only have three link types to choose from in the UI and the duplicates should be gone as well. Without the duplicate entries of `parent of` and `child of` you then can no longer link a WI to two parents. Without this PR you still can link a child WI to two parent WIs when you choose from two different link types.

To double check, what link types exist, try this command:

```sh
curl --silent https://api.openshift.io/api/spaces/020f756e-b51a-4b43-b113-45cec16b9ce9/workitemlinktypes \
| jq ".data[] | .id, .attributes.name"
```

It should output the id and name of each link  type in the current production database:
```json
"aad2a4ad-d601-4104-9804-2c977ca2e0c1"
"Bug blocker"
"355b647b-adc5-46b3-b297-cc54bc0554e6"
"Related planner item"
"7479a9b9-8607-46fa-9535-d448fa8768ab"
"Parent child item"
"2cea3c79-3b79-423b-90f4-1e59174c8f43"
"Bug blocker"
"9b631885-83b1-4abb-a340-3a9ede8493fa"
"Related planner item"
"25c326a7-6d03-4f5a-b23b-86a9ee4171e9"
"Parent child item"
```

In 90c595e I have introduced fixed IDs for link types and link categories and that must have been the time when the old ones weren't deleted.

As part of this PR I've replaced many of the `panic` calls within the migration tests with normal test failures. I've also wrapped errors and added filenames of executed SQL files to the error output to increase *debuggability* (is that a word?). 

This relates to #1729  and fabric8-ui/fabric8-planner#2291 (comment)
  • Loading branch information
kwk authored Oct 31, 2017
1 parent 21fa583 commit 9709b5c
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 123 deletions.
14 changes: 14 additions & 0 deletions gormsupport/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
errCheckViolation = "23514"
errUniqueViolation = "23505"
errForeignKeyViolation = "23503"
errInvalidCatalogName = "3D000"
)

// IsCheckViolation returns true if the error is a violation of the given check
Expand All @@ -20,6 +21,19 @@ func IsCheckViolation(err error, constraintName string) bool {
return pqError.Code == errCheckViolation && pqError.Constraint == constraintName
}

// IsInvalidCatalogName returns true if the given error says that the catalog
// is ivalid (e.g. database does not exist)
func IsInvalidCatalogName(err error) bool {
if err == nil {
return false
}
pqError, ok := err.(*pq.Error)
if !ok {
return false
}
return pqError.Code == errInvalidCatalogName
}

// IsUniqueViolation returns true if the error is a violation of the given unique index
func IsUniqueViolation(err error, indexName string) bool {
if err == nil {
Expand Down
17 changes: 13 additions & 4 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ func GetMigrations() Migrations {
// Version 79
m = append(m, steps{ExecuteSQLFile("079-assignee-and-label-empty-value.sql", workitem.SystemAssignees, workitem.SystemLabels)})

// Version 80
m = append(m, steps{ExecuteSQLFile("080-remove-unknown-link-types.sql",
link.SystemWorkItemLinkTypeBugBlockerID.String(),
link.SystemWorkItemLinkPlannerItemRelatedID.String(),
link.SystemWorkItemLinkTypeParentChildID.String(),
link.SystemWorkItemLinkCategorySystemID.String(),
link.SystemWorkItemLinkCategoryUserID.String(),
)})

// Version N
//
// In order to add an upgrade, simply append an array of MigrationFunc to the
Expand Down Expand Up @@ -401,24 +410,24 @@ func ExecuteSQLFile(filename string, args ...string) fn {
if len(args) > 0 {
tmpl, err := template.New("sql").Parse(string(data))
if err != nil {
return errs.Wrap(err, "failed to parse SQL template")
return errs.Wrapf(err, "failed to parse SQL template in file %s", filename)
}
var sqlScript bytes.Buffer
writer := bufio.NewWriter(&sqlScript)
err = tmpl.Execute(writer, args)
if err != nil {
return errs.Wrap(err, "failed to execute SQL template")
return errs.Wrapf(err, "failed to execute SQL template in file %s", filename)
}
// We need to flush the content of the writer
writer.Flush()
_, err = db.Exec(sqlScript.String())
if err != nil {
log.Error(context.Background(), map[string]interface{}{}, "failed to execute this query: \n\n%s\n\n", sqlScript.String())
log.Error(context.Background(), map[string]interface{}{"err": err}, "failed to execute this query in file %s: \n\n%s\n\n", filename, sqlScript.String())
}
} else {
_, err = db.Exec(string(data))
if err != nil {
log.Error(context.Background(), map[string]interface{}{}, "failed to execute this query: \n\n%s\n\n", string(data))
log.Error(context.Background(), map[string]interface{}{"err": err}, "failed to execute this query in file: %s \n\n%s\n\n", filename, string(data))
}
}

Expand Down
Loading

0 comments on commit 9709b5c

Please sign in to comment.