Test disposable fixtures and migrations
Test disposable fixtures and migrations
A migration changes schema or data over time. Test from the previous supported schema with synthetic rows in a disposable database. Verify constraints, preserved data, compatibility and recovery. Never use production credentials. Here an in-memory schema model illustrates assertions, not DDL execution.
package main
import ( "errors" "fmt")
// Model only: schema assertions, not SQL execution.type Schema struct { version int columns map[string]bool}
func migrate(s *Schema) error { if s.version != 1 { return errors.New("expected version 1") } if !s.columns["id"] || !s.columns["name"] { return errors.New("incompatible base schema") } s.columns["edition"] = true s.version = 2 return nil}func compatible(s Schema) bool { return s.columns["id"] && s.columns["name"] }func main() { s := Schema{1, map[string]bool{"id": true, "name": true}} err := migrate(&s) fmt.Println(err == nil, s.version, s.columns["edition"], compatible(s))}The model checks preservation of required old columns. Real tests must execute actual migration SQL and check old rows, defaults, nullability, indexes and constraints. An added column may still break strict clients or lock large tables.
Expected output:
true 2 true trueReject an unexpected baseline
package main
import ( "errors" "fmt")
// Model only: schema assertions, not SQL execution.type Schema struct { version int columns map[string]bool}
func migrate(s *Schema) error { if s.version != 1 { return errors.New("expected version 1") } if !s.columns["id"] || !s.columns["name"] { return errors.New("incompatible base schema") } s.columns["edition"] = true s.version = 2 return nil}func compatible(s Schema) bool { return s.columns["id"] && s.columns["name"] }func main() { s := Schema{0, map[string]bool{"id": true, "name": true}} fmt.Println(migrate(&s) != nil, s.version, s.columns["edition"])}The model refuses a wrong starting version before modifying state. Real tools require durable version tracking and concurrency control. DDL transaction support varies. Test interruption and restoration on the actual engine; a down migration cannot necessarily recover deleted data.
Expected output:
true 0 falseTry it locally
Create a version-1 model missing name; verify migration fails, version stays 1 and edition remains absent.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Database 08: Test disposable fixtures and migrations
Check the lesson objective before moving on.
What does a model test establish?
Where should real migration tests run?