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 27, 2016
1 parent e3f5b57 commit 4b6eb60
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion buildscripts/dbtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function cleanup {
fi
}

clientCmd="make TESTOPTS='-v' test"
clientCmd="make TESTOPTS='-p 1' test"
if [[ -z "${CIRCLECI}" ]]; then
BUILDOPTS="--force-rm"
else
Expand Down
2 changes: 1 addition & 1 deletion server/storage/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
if i == 29 {
logrus.Fatalf("Unable to connect to %s after 60 seconds", dburl)
}
time.Sleep(2)
time.Sleep(2 * time.Second)
}

sqldbSetup = func(t *testing.T) (*SQLStorage, func()) {
Expand Down
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
2 changes: 1 addition & 1 deletion signer/keydbstore/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
if i == 29 {
logrus.Fatalf("Unable to connect to %s after 60 seconds", dburl)
}
time.Sleep(2)
time.Sleep(2 * time.Second)
}

sqldbSetup = func(t *testing.T) (*SQLKeyDBStore, func()) {
Expand Down

0 comments on commit 4b6eb60

Please sign in to comment.