Sync branch magefile with main #104308

Merged
Sybren A. Stüvel merged 85 commits from abelli/flamenco:magefile into magefile 2024-05-13 16:26:32 +02:00
2 changed files with 34 additions and 0 deletions
Showing only changes of commit 6c28db780f - Show all commits

View File

@ -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{})

View File

@ -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()