SQLite's backup API preserves hidden row IDs (unlike VACUUM INTO), including those used by external-content FTS indexes. Its destination is a fresh staged file, so live WAL mode, page size and corruption cannot impede the copy.
(srcPath, dstPath string)
| 167 | // those used by external-content FTS indexes. Its destination is a fresh staged |
| 168 | // file, so live WAL mode, page size and corruption cannot impede the copy. |
| 169 | func normalizeSQLiteRestore(srcPath, dstPath string) (retErr error) { |
| 170 | dsn, err := sqliteFileURI(srcPath) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | db, err := sql.Open("sqlite", dsn) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | defer func() { retErr = errors.Join(retErr, db.Close()) }() |
| 179 | conn, err := db.Conn(context.Background()) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | defer func() { retErr = errors.Join(retErr, conn.Close()) }() |
| 184 | dstURI, err := sqliteFileURI(dstPath) |
| 185 | if err != nil { |
| 186 | return err |
| 187 | } |
| 188 | return conn.Raw(func(raw any) error { |
| 189 | backup, err := raw.(interface { |
| 190 | NewBackup(string) (*sqlite.Backup, error) |
| 191 | }).NewBackup(dstURI) |
| 192 | if err != nil { |
| 193 | return err |
| 194 | } |
| 195 | more, stepErr := backup.Step(-1) |
| 196 | if more && stepErr == nil { |
| 197 | stepErr = errors.New("SQLite backup did not finish") |
| 198 | } |
| 199 | // Finalize even on failure to roll back the private destination, and |
| 200 | // close it explicitly: Backup.Finish discards its Close error. |
| 201 | dst, finishErr := backup.Commit() |
| 202 | if dst != nil { |
| 203 | finishErr = errors.Join(finishErr, dst.Close()) |
| 204 | } |
| 205 | return errors.Join(stepErr, finishErr) |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | func checkSQLiteRestoreDatabase(path string) error { |
| 210 | dsn, err := sqliteFileURI(path) |
no test coverage detected