Skip to content

Commit

Permalink
Use gorm Unscoped to force a hard delete instead of raw execing some SQL
Browse files Browse the repository at this point in the history
Signed-off-by: Ying Li <[email protected]>
  • Loading branch information
cyli committed Jul 23, 2016
1 parent 6953360 commit c31aa99
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
1 change: 0 additions & 1 deletion server/storage/sql_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type TUFFile struct {

// TableName sets a specific table name for TUFFile
func (g TUFFile) TableName() string {
// NOTE: if this value changes, please also change it in SQLStorage.Delete
return "tuf_files"
}

Expand Down
7 changes: 3 additions & 4 deletions server/storage/sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,10 @@ func isReadErr(q *gorm.DB, row TUFFile) error {
return nil
}

// Delete deletes all the records for a specific GUN - we have to do a raw execute, because
// otherwise GORM will do a soft delete, because we have CreatedAt, ModifiedAt, and DeletedAt
// columns (and we actually use CreatedAt and ModifiedAt)
// Delete deletes all the records for a specific GUN - we have to do a hard delete using Unscoped
// otherwise we can't insert for that GUN again
func (db *SQLStorage) Delete(gun string) error {
return db.Exec("DELETE FROM tuf_files WHERE gun = ?", gun).Error
return db.Unscoped().Where(&TUFFile{Gun: gun}).Delete(TUFFile{}).Error
}

// GetKey returns the Public Key data for a gun+role
Expand Down
2 changes: 1 addition & 1 deletion server/storage/sqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestSQLDBCheckHealthTableMissing(t *testing.T) {
require.Error(t, err, "Cannot access table:")
}

// TestSQLDBCheckHealthDBCOnnection asserts that if the DB is not connectable, the
// TestSQLDBCheckHealthDBConnection asserts that if the DB is not connectable, the
// health check fails.
func TestSQLDBCheckHealthDBConnectionFail(t *testing.T) {
dbStore, cleanup := sqldbSetup(t)
Expand Down

0 comments on commit c31aa99

Please sign in to comment.