Manager: Fix DB migration error of not-null columns

Where the PostgreSQL DB migration code could handle `NOT NULL` columns just
fine, SQLite has less table-altering functionality. As a result, migrations
have to copy entire database tables, which doesn't play well with
not-nullable columns.
This commit is contained in:
Sybren A. Stüvel 2022-03-03 12:06:22 +01:00
parent bf4cc9b056
commit 9643bf768e
3 changed files with 31 additions and 23 deletions

View File

@ -32,3 +32,11 @@ Flamenco Manager has a SwaggerUI interface at http://localhost:8080/api/swagger-
## Database ## Database
Flamenco Manager includes a copy of https://github.com/go-gorm/sqlite.git, adjusted to use the pure-Go SQLite from https://modernc.org/sqlite. Flamenco Manager includes a copy of https://github.com/go-gorm/sqlite.git, adjusted to use the pure-Go SQLite from https://modernc.org/sqlite.
Flamenco Manager and Worker use SQLite as database, and Gorm as
object-relational mapper.
Since SQLite has limited support for altering table schemas, migration requires
copying old data to a temporary table with the new schema, then swap out the
tables. Because of this, avoid `NOT NULL` columns, as they will be problematic
in this process.

View File

@ -35,12 +35,12 @@ import (
type Job struct { type Job struct {
gorm.Model gorm.Model
UUID string `gorm:"type:char(36);not null;unique;index"` UUID string `gorm:"type:char(36);default:'';unique;index"`
Name string `gorm:"type:varchar(64);not null"` Name string `gorm:"type:varchar(64);default:''"`
JobType string `gorm:"type:varchar(32);not null"` JobType string `gorm:"type:varchar(32);default:''"`
Priority int `gorm:"type:smallint;not null"` Priority int `gorm:"type:smallint;default:0"`
Status api.JobStatus `gorm:"type:varchar(32);not null"` Status api.JobStatus `gorm:"type:varchar(32);default:''"`
Settings StringInterfaceMap `gorm:"type:jsonb"` Settings StringInterfaceMap `gorm:"type:jsonb"`
Metadata StringStringMap `gorm:"type:jsonb"` Metadata StringStringMap `gorm:"type:jsonb"`
@ -51,14 +51,14 @@ type StringStringMap map[string]string
type Task struct { type Task struct {
gorm.Model gorm.Model
UUID string `gorm:"type:char(36);not null;unique;index"` UUID string `gorm:"type:char(36);default:'';unique;index"`
Name string `gorm:"type:varchar(64);not null"` Name string `gorm:"type:varchar(64);default:''"`
Type string `gorm:"type:varchar(32);not null"` Type string `gorm:"type:varchar(32);default:''"`
JobID uint `gorm:"not null"` JobID uint `gorm:"default:0"`
Job *Job `gorm:"foreignkey:JobID;references:ID;constraint:OnDelete:CASCADE;not null"` Job *Job `gorm:"foreignkey:JobID;references:ID;constraint:OnDelete:CASCADE"`
Priority int `gorm:"type:smallint;not null"` Priority int `gorm:"type:smallint;default:50"`
Status api.TaskStatus `gorm:"type:varchar(16);not null"` Status api.TaskStatus `gorm:"type:varchar(16);default:''"`
// Which worker is/was working on this. // Which worker is/was working on this.
WorkerID *uint WorkerID *uint
@ -68,7 +68,7 @@ type Task struct {
Dependencies []*Task `gorm:"many2many:task_dependencies;constraint:OnDelete:CASCADE"` Dependencies []*Task `gorm:"many2many:task_dependencies;constraint:OnDelete:CASCADE"`
Commands Commands `gorm:"type:jsonb"` Commands Commands `gorm:"type:jsonb"`
Activity string `gorm:"type:varchar(255);not null;default:\"\""` Activity string `gorm:"type:varchar(255);default:''"`
} }
type Commands []Command type Commands []Command

View File

@ -32,18 +32,18 @@ import (
type Worker struct { type Worker struct {
gorm.Model gorm.Model
UUID string `gorm:"type:char(36);not null;unique;index"` UUID string `gorm:"type:char(36);default:'';unique;index;default:''"`
Secret string `gorm:"type:varchar(255);not null"` Secret string `gorm:"type:varchar(255);default:''"`
Name string `gorm:"type:varchar(64);not null"` Name string `gorm:"type:varchar(64);default:''"`
Address string `gorm:"type:varchar(39);not null;index"` // 39 = max length of IPv6 address. Address string `gorm:"type:varchar(39);default:'';index"` // 39 = max length of IPv6 address.
LastActivity string `gorm:"type:varchar(255);not null"` LastActivity string `gorm:"type:varchar(255);default:''"`
Platform string `gorm:"type:varchar(16);not null"` Platform string `gorm:"type:varchar(16);default:''"`
Software string `gorm:"type:varchar(32);not null"` Software string `gorm:"type:varchar(32);default:''"`
Status api.WorkerStatus `gorm:"type:varchar(16);not null"` Status api.WorkerStatus `gorm:"type:varchar(16);default:''"`
StatusRequested api.WorkerStatus `gorm:"type:varchar(16);not null;default:''"` StatusRequested api.WorkerStatus `gorm:"type:varchar(16);default:''"`
SupportedTaskTypes string `gorm:"type:varchar(255);not null"` // comma-separated list of task types. SupportedTaskTypes string `gorm:"type:varchar(255);default:''"` // comma-separated list of task types.
} }
// TaskTypes returns the worker's supported task types as list of strings. // TaskTypes returns the worker's supported task types as list of strings.