Files
bwilliams 65e5bc6b26 Added database.change_batch_retention, deleting old change batches
Each save that records a batch also deletes, in the same
transaction, the batches older than database.change_batch_retention
(30 days by default; 0 or less keeps them for ever), with their
records of opportunities. A client still holding a deleted batch
gets 410 Gone from ?changed_after= and reloads the whole list, as
for any unknown batch.
2026-10-16 16:10:32 +01:00

57 lines
1.4 KiB
Go

package cli
import (
"context"
"errors"
"fmt"
"os"
"git.avsdev.uk/Go/job-search/internal/config"
"git.avsdev.uk/Go/job-search/internal/store"
)
// ErrNoDatabase is returned by OpenConfiguredStore when database.file isn't
// set.
var ErrNoDatabase = errors.New("database.file is not set")
// OpenConfiguredStore opens the store at database.file, or returns
// ErrNoDatabase if none is configured. A missing file or a database that
// needs migrating fails with a message saying how to create or migrate it; a
// missing file isn't created, as store.Open would. The caller must close the
// store.
func OpenConfiguredStore(
ctx context.Context, conf *config.JobSearchConfig,
) (*store.Store, error) {
file := conf.Database.File
if file == "" {
return nil, ErrNoDatabase
}
_, err := os.Stat(file)
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf(
"database %s does not exist: run `job-search migrate up --create`",
file,
)
}
if err != nil {
return nil, fmt.Errorf("database %s: %w", file, err)
}
opportunityStore, err := store.Open(
ctx,
file,
store.WithChangeBatchRetention(conf.Database.ChangeBatchRetention),
)
if errors.Is(err, store.ErrNeedsMigration) {
return nil, fmt.Errorf(
"database %s: %w: run `job-search migrate`", file, err,
)
}
if err != nil {
return nil, fmt.Errorf("database %s: %w", file, err)
}
return opportunityStore, nil
}