package updates import ( "github.com/go-gormigrate/gormigrate/v2" "gorm.io/gorm" ) // MakeCourseIDNullable returns the 20261002234500_make_course_id_nullable // gormigrate entry: class sessions and readings may have no course while // the course form that creates them is not saved yet. The relation managers // on the course form hold such rows in deferred_bindings, and the first save // of the course sets course_id; deferred:purge deletes abandoned ones. func MakeCourseIDNullable() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20261002234500_make_course_id_nullable", Migrate: func(tx *gorm.DB) error { return execAll(tx, `ALTER TABLE jz_bm_class_sessions ALTER COLUMN course_id DROP NOT NULL; ALTER TABLE jz_bm_readings ALTER COLUMN course_id DROP NOT NULL`) }, Rollback: func(tx *gorm.DB) error { return execAll(tx, `DELETE FROM jz_bm_class_sessions WHERE course_id IS NULL; DELETE FROM jz_bm_readings WHERE course_id IS NULL; ALTER TABLE jz_bm_class_sessions ALTER COLUMN course_id SET NOT NULL; ALTER TABLE jz_bm_readings ALTER COLUMN course_id SET NOT NULL`) }, } }