FileControl handles file control operations, specifically PRAGMA commands for time travel.
(op int, pragmaName string, pragmaValue *string)
| 2330 | |
| 2331 | // FileControl handles file control operations, specifically PRAGMA commands for time travel. |
| 2332 | func (f *VFSFile) FileControl(op int, pragmaName string, pragmaValue *string) (*string, error) { |
| 2333 | const SQLITE_FCNTL_PRAGMA = 14 |
| 2334 | |
| 2335 | if op != SQLITE_FCNTL_PRAGMA { |
| 2336 | return nil, fmt.Errorf("unsupported file control op: %d", op) |
| 2337 | } |
| 2338 | |
| 2339 | name := strings.ToLower(pragmaName) |
| 2340 | |
| 2341 | f.logger.Debug("file control", "pragma", name, "value", pragmaValue) |
| 2342 | |
| 2343 | switch name { |
| 2344 | case "litestream_txid": |
| 2345 | if pragmaValue != nil { |
| 2346 | return nil, fmt.Errorf("litestream_txid is read-only") |
| 2347 | } |
| 2348 | txid := f.Pos().TXID |
| 2349 | result := txid.String() |
| 2350 | return &result, nil |
| 2351 | |
| 2352 | case "litestream_lag": |
| 2353 | if pragmaValue != nil { |
| 2354 | return nil, fmt.Errorf("litestream_lag is read-only") |
| 2355 | } |
| 2356 | lastPoll := f.LastPollSuccess() |
| 2357 | if lastPoll.IsZero() { |
| 2358 | result := "-1" // Never polled successfully |
| 2359 | return &result, nil |
| 2360 | } |
| 2361 | lag := int64(time.Since(lastPoll).Seconds()) |
| 2362 | result := strconv.FormatInt(lag, 10) |
| 2363 | return &result, nil |
| 2364 | |
| 2365 | case "litestream_time": |
| 2366 | if pragmaValue == nil { |
| 2367 | result := f.currentTimeString() |
| 2368 | return &result, nil |
| 2369 | } |
| 2370 | |
| 2371 | if strings.EqualFold(*pragmaValue, "latest") { |
| 2372 | if err := f.ResetTime(context.Background()); err != nil { |
| 2373 | return nil, err |
| 2374 | } |
| 2375 | return nil, nil |
| 2376 | } |
| 2377 | |
| 2378 | t, err := parseTimeValue(*pragmaValue) |
| 2379 | if err != nil { |
| 2380 | return nil, err |
| 2381 | } |
| 2382 | if err := f.SetTargetTime(context.Background(), t); err != nil { |
| 2383 | return nil, err |
| 2384 | } |
| 2385 | return nil, nil |
| 2386 | |
| 2387 | case "litestream_hydration_progress": |
| 2388 | if pragmaValue != nil { |
| 2389 | return nil, fmt.Errorf("litestream_hydration_progress is read-only") |