MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / normalizeSQLiteRestore

Function normalizeSQLiteRestore

cmd/pad/db_restore_sqlite.go:169–207  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
169func 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
209func checkSQLiteRestoreDatabase(path string) error {
210 dsn, err := sqliteFileURI(path)

Callers 1

restoreSQLiteFunction · 0.85

Calls 5

sqliteFileURIFunction · 0.85
JoinMethod · 0.80
CloseMethod · 0.65
OpenMethod · 0.45
CommitMethod · 0.45

Tested by

no test coverage detected