Sync branch magefile with main #104308
@ -62,6 +62,15 @@ func (db *DB) SaveWorkerTag(ctx context.Context, tag *WorkerTag) error {
|
||||
|
||||
// DeleteWorkerTag deletes the given tag, after unassigning all workers from it.
|
||||
func (db *DB) DeleteWorkerTag(ctx context.Context, uuid string) error {
|
||||
// As a safety measure, refuse to delete unless foreign key constraints are active.
|
||||
fkEnabled, err := db.areForeignKeysEnabled()
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking whether foreign keys are enabled: %w", err)
|
||||
}
|
||||
if !fkEnabled {
|
||||
return ErrDeletingWithoutFK
|
||||
}
|
||||
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
Where("uuid = ?", uuid).
|
||||
Delete(&WorkerTag{})
|
||||
|
@ -68,6 +68,31 @@ func TestFetchDeleteTags(t *testing.T) {
|
||||
assert.False(t, has, "expecting HasWorkerTags to return false")
|
||||
}
|
||||
|
||||
func TestDeleteTagsWithoutFK(t *testing.T) {
|
||||
f := workerTestFixtures(t, 1*time.Second)
|
||||
defer f.done()
|
||||
|
||||
// Single tag was created by fixture.
|
||||
has, err := f.db.HasWorkerTags(f.ctx)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, has, "expecting HasWorkerTags to return true")
|
||||
|
||||
secondTag := WorkerTag{
|
||||
UUID: uuid.New(),
|
||||
Name: "arbeiderskaartje",
|
||||
Description: "Worker tag in Dutch",
|
||||
}
|
||||
require.NoError(t, f.db.CreateWorkerTag(f.ctx, &secondTag))
|
||||
|
||||
// Try deleting with foreign key constraints disabled.
|
||||
require.NoError(t, f.db.pragmaForeignKeys(false))
|
||||
err = f.db.DeleteWorkerTag(f.ctx, f.tag.UUID)
|
||||
require.ErrorIs(t, err, ErrDeletingWithoutFK)
|
||||
|
||||
// Test the deletion did not happen.
|
||||
assertTagsMatch(t, f, f.tag.UUID, secondTag.UUID)
|
||||
}
|
||||
|
||||
func TestAssignUnassignWorkerTags(t *testing.T) {
|
||||
f := workerTestFixtures(t, 1*time.Second)
|
||||
defer f.done()
|
||||
|
Loading…
Reference in New Issue
Block a user