MCPcopy Index your code
hub / github.com/dnote/dnote / TestMigrate_idempotency

Function TestMigrate_idempotency

pkg/server/database/migrate_test.go:71–111  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

69}
70
71func TestMigrate_idempotency(t *testing.T) {
72 db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
73 if err != nil {
74 t.Fatalf("failed to open database: %v", err)
75 }
76
77 // Set up table before migration
78 if err := db.Exec("CREATE TABLE counter (value INTEGER)").Error; err != nil {
79 t.Fatalf("failed to create table: %v", err)
80 }
81
82 // Create migration that inserts a row
83 migrationsFs := fstest.MapFS{
84 "001-insert-data.sql": &fstest.MapFile{
85 Data: []byte("INSERT INTO counter (value) VALUES (100);"),
86 },
87 }
88
89 // Run migration first time
90 if err := migrate(db, migrationsFs); err != nil {
91 t.Fatalf("first migration failed: %v", err)
92 }
93 var count int64
94 if err := db.Raw("SELECT COUNT(*) FROM counter").Scan(&count).Error; err != nil {
95 t.Fatalf("failed to count rows: %v", err)
96 }
97 if count != 1 {
98 t.Errorf("expected 1 row, got %d", count)
99 }
100
101 // Run migration second time - it should not run the SQL again
102 if err := migrate(db, migrationsFs); err != nil {
103 t.Fatalf("second migration failed: %v", err)
104 }
105 if err := db.Raw("SELECT COUNT(*) FROM counter").Scan(&count).Error; err != nil {
106 t.Fatalf("failed to count rows: %v", err)
107 }
108 if count != 1 {
109 t.Errorf("migration ran twice: expected 1 row, got %d", count)
110 }
111}
112
113func TestMigrate_ordering(t *testing.T) {
114 db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})

Callers

nothing calls this directly

Calls 3

migrateFunction · 0.85
OpenMethod · 0.80
ExecMethod · 0.65

Tested by

no test coverage detected