github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/tests/backup_test.go (about) 1 package tests 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/ncruces/go-sqlite3" 8 _ "github.com/ncruces/go-sqlite3/embed" 9 _ "github.com/ncruces/go-sqlite3/tests/testcfg" 10 "github.com/ncruces/go-sqlite3/vfs" 11 ) 12 13 func TestBackup(t *testing.T) { 14 if !vfs.SupportsFileLocking { 15 t.Skip("skipping without locks") 16 } 17 t.Parallel() 18 19 backupName := filepath.Join(t.TempDir(), "backup.db") 20 21 func() { // Create backup. 22 db, err := sqlite3.Open(":memory:") 23 if err != nil { 24 t.Fatal(err) 25 } 26 defer db.Close() 27 28 err = db.Exec(`CREATE TABLE users (id INT, name VARCHAR(10))`) 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 err = db.Exec(`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 err = db.Backup("main", backupName) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 err = db.Close() 44 if err != nil { 45 t.Fatal(err) 46 } 47 }() 48 49 func() { // Restore backup. 50 db, err := sqlite3.Open(":memory:") 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer db.Close() 55 56 err = db.Restore("main", backupName) 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 stmt, _, err := db.Prepare(`SELECT id, name FROM users`) 62 if err != nil { 63 t.Fatal(err) 64 } 65 defer stmt.Close() 66 67 row := 0 68 ids := []int{0, 1, 2} 69 names := []string{"go", "zig", "whatever"} 70 for ; stmt.Step(); row++ { 71 id := stmt.ColumnInt(0) 72 name := stmt.ColumnText(1) 73 74 if id != ids[row] { 75 t.Errorf("got %d, want %d", id, ids[row]) 76 } 77 if name != names[row] { 78 t.Errorf("got %q, want %q", name, names[row]) 79 } 80 } 81 if row != 3 { 82 t.Errorf("got %d, want %d", row, len(ids)) 83 } 84 85 if err := stmt.Err(); err != nil { 86 t.Fatal(err) 87 } 88 89 err = stmt.Close() 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 err = db.Close() 95 if err != nil { 96 t.Fatal(err) 97 } 98 }() 99 100 func() { // Errors. 101 db, err := sqlite3.Open(backupName) 102 if err != nil { 103 t.Fatal(err) 104 } 105 defer db.Close() 106 107 tx, err := db.BeginExclusive() 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 err = db.Restore("main", backupName) 113 if err == nil { 114 t.Fatal("want error") 115 } 116 117 err = tx.Rollback() 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 err = db.Restore("main", backupName) 123 if err == nil { 124 t.Fatal("want error") 125 } 126 127 err = db.Close() 128 if err != nil { 129 t.Fatal(err) 130 } 131 }() 132 133 func() { // Incremental. 134 db, err := sqlite3.Open(backupName) 135 if err != nil { 136 t.Fatal(err) 137 } 138 defer db.Close() 139 140 b, err := db.BackupInit("main", ":memory:") 141 if err != nil { 142 t.Fatal(err) 143 } 144 defer b.Close() 145 146 done, err := b.Step(1) 147 if done { 148 t.Error("want false") 149 } 150 if err != nil { 151 t.Error(err) 152 } 153 154 n := b.Remaining() 155 if n != 1 { 156 t.Errorf("got %d", n) 157 } 158 159 n = b.PageCount() 160 if n != 2 { 161 t.Errorf("got %d", n) 162 } 163 164 err = b.Close() 165 if err != nil { 166 t.Fatal(err) 167 } 168 169 err = db.Close() 170 if err != nil { 171 t.Fatal(err) 172 } 173 }() 174 }