Manager: replace gorm.Model with our own persistence.Model struct

`persistence.Model` contains the common database fields for most model
structs. It is a copy of `gorm.Model`, but without the `DeletedAt`
field (which triggers Gorm's soft deletion).

Soft deletion is not used by Flamenco. If it ever becomes necessary to
support soft-deletion, see https://gorm.io/docs/delete.html#Soft-Delete
This commit is contained in:
Sybren A. Stüvel 2022-06-13 15:11:24 +02:00
parent ec5b3aac52
commit 02bc03ae2b
8 changed files with 20 additions and 17 deletions

View File

@ -10,7 +10,6 @@ import (
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/gorm"
) )
func TestFetchTask(t *testing.T) { func TestFetchTask(t *testing.T) {
@ -24,7 +23,7 @@ func TestFetchTask(t *testing.T) {
jobUUID := "8b179118-0189-478a-b463-73798409898c" jobUUID := "8b179118-0189-478a-b463-73798409898c"
dbTask := persistence.Task{ dbTask := persistence.Task{
Model: gorm.Model{ Model: persistence.Model{
ID: 327, ID: 327,
CreatedAt: mf.clock.Now().Add(-30 * time.Second), CreatedAt: mf.clock.Now().Add(-30 * time.Second),
UpdatedAt: mf.clock.Now(), UpdatedAt: mf.clock.Now(),

View File

@ -16,7 +16,6 @@ import (
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/gorm"
"git.blender.org/flamenco/internal/manager/api_impl/mocks" "git.blender.org/flamenco/internal/manager/api_impl/mocks"
"git.blender.org/flamenco/internal/manager/persistence" "git.blender.org/flamenco/internal/manager/persistence"
@ -143,7 +142,7 @@ func assertResponseEmpty(t *testing.T, echoCtx echo.Context) {
func testWorker() persistence.Worker { func testWorker() persistence.Worker {
return persistence.Worker{ return persistence.Worker{
Model: gorm.Model{ID: 1}, Model: persistence.Model{ID: 1},
UUID: "e7632d62-c3b8-4af0-9e78-01752928952c", UUID: "e7632d62-c3b8-4af0-9e78-01752928952c",
Name: "дрон", Name: "дрон",
Address: "fe80::5054:ff:fede:2ad7", Address: "fe80::5054:ff:fede:2ad7",

View File

@ -21,6 +21,16 @@ type DB struct {
gormDB *gorm.DB gormDB *gorm.DB
} }
// Model contains the common database fields for most model structs.
// It is a copy of the gorm.Model struct, but without the `DeletedAt` field.
// Soft deletion is not used by Flamenco. If it ever becomes necessary to
// support soft-deletion, see https://gorm.io/docs/delete.html#Soft-Delete
type Model struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
}
func OpenDB(ctx context.Context, dsn string) (*DB, error) { func OpenDB(ctx context.Context, dsn string) (*DB, error) {
log.Info().Str("dsn", dsn).Msg("opening database") log.Info().Str("dsn", dsn).Msg("opening database")

View File

@ -16,7 +16,7 @@ import (
) )
type Job struct { type Job struct {
gorm.Model Model
UUID string `gorm:"type:char(36);default:'';unique;index"` UUID string `gorm:"type:char(36);default:'';unique;index"`
Name string `gorm:"type:varchar(64);default:''"` Name string `gorm:"type:varchar(64);default:''"`
@ -33,7 +33,7 @@ type StringInterfaceMap map[string]interface{}
type StringStringMap map[string]string type StringStringMap map[string]string
type Task struct { type Task struct {
gorm.Model Model
UUID string `gorm:"type:char(36);default:'';unique;index"` UUID string `gorm:"type:char(36);default:'';unique;index"`
Name string `gorm:"type:varchar(64);default:''"` Name string `gorm:"type:varchar(64);default:''"`

View File

@ -8,13 +8,11 @@ import (
"strings" "strings"
"time" "time"
"gorm.io/gorm"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
) )
type Worker struct { type Worker struct {
gorm.Model Model
UUID string `gorm:"type:char(36);default:'';unique;index;default:''"` UUID string `gorm:"type:char(36);default:'';unique;index;default:''"`
Secret string `gorm:"type:varchar(255);default:''"` Secret string `gorm:"type:varchar(255);default:''"`
Name string `gorm:"type:varchar(64);default:''"` Name string `gorm:"type:varchar(64);default:''"`

View File

@ -10,7 +10,6 @@ import (
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/gorm"
"git.blender.org/flamenco/internal/manager/persistence" "git.blender.org/flamenco/internal/manager/persistence"
"git.blender.org/flamenco/internal/manager/task_state_machine/mocks" "git.blender.org/flamenco/internal/manager/task_state_machine/mocks"
@ -442,13 +441,13 @@ func (m *StateMachineMocks) expectBroadcastTaskChange(
/* taskWithStatus() creates a task of a certain status, with a job of a certain status. */ /* taskWithStatus() creates a task of a certain status, with a job of a certain status. */
func taskWithStatus(jobStatus api.JobStatus, taskStatus api.TaskStatus) *persistence.Task { func taskWithStatus(jobStatus api.JobStatus, taskStatus api.TaskStatus) *persistence.Task {
job := persistence.Job{ job := persistence.Job{
Model: gorm.Model{ID: 47}, Model: persistence.Model{ID: 47},
UUID: "test-job-f3f5-4cef-9cd7-e67eb28eaf3e", UUID: "test-job-f3f5-4cef-9cd7-e67eb28eaf3e",
Status: jobStatus, Status: jobStatus,
} }
task := persistence.Task{ task := persistence.Task{
Model: gorm.Model{ID: 327}, Model: persistence.Model{ID: 327},
UUID: "testtask-0001-4e28-aeea-8cbaf2fc96a5", UUID: "testtask-0001-4e28-aeea-8cbaf2fc96a5",
JobID: job.ID, JobID: job.ID,
@ -464,7 +463,7 @@ func taskWithStatus(jobStatus api.JobStatus, taskStatus api.TaskStatus) *persist
func taskOfSameJob(task *persistence.Task, taskStatus api.TaskStatus) *persistence.Task { func taskOfSameJob(task *persistence.Task, taskStatus api.TaskStatus) *persistence.Task {
newTaskID := task.ID + 1 newTaskID := task.ID + 1
return &persistence.Task{ return &persistence.Task{
Model: gorm.Model{ID: newTaskID}, Model: persistence.Model{ID: newTaskID},
UUID: fmt.Sprintf("testtask-%04d-4e28-aeea-8cbaf2fc96a5", newTaskID), UUID: fmt.Sprintf("testtask-%04d-4e28-aeea-8cbaf2fc96a5", newTaskID),
JobID: task.JobID, JobID: task.JobID,
Job: task.Job, Job: task.Job,

View File

@ -10,7 +10,6 @@ import (
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/gorm"
"git.blender.org/flamenco/internal/manager/persistence" "git.blender.org/flamenco/internal/manager/persistence"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
@ -114,7 +113,7 @@ func TestTaskTimeout(t *testing.T) {
worker := persistence.Worker{ worker := persistence.Worker{
UUID: "WORKER-UUID", UUID: "WORKER-UUID",
Name: "Tester", Name: "Tester",
Model: gorm.Model{ID: 47}, Model: persistence.Model{ID: 47},
} }
taskUnassigned := persistence.Task{ taskUnassigned := persistence.Task{
UUID: "TASK-UUID-UNASSIGNED", UUID: "TASK-UUID-UNASSIGNED",

View File

@ -9,7 +9,6 @@ import (
"git.blender.org/flamenco/internal/manager/persistence" "git.blender.org/flamenco/internal/manager/persistence"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"gorm.io/gorm"
) )
const workerTimeout = 20 * time.Minute const workerTimeout = 20 * time.Minute
@ -31,7 +30,7 @@ func TestWorkerTimeout(t *testing.T) {
worker := persistence.Worker{ worker := persistence.Worker{
UUID: "WORKER-UUID", UUID: "WORKER-UUID",
Name: "Tester", Name: "Tester",
Model: gorm.Model{ID: 47}, Model: persistence.Model{ID: 47},
LastSeenAt: lastSeenAt, LastSeenAt: lastSeenAt,
Status: api.WorkerStatusAsleep, Status: api.WorkerStatusAsleep,
StatusRequested: api.WorkerStatusAwake, StatusRequested: api.WorkerStatusAwake,