With WithStore, the Find a Tender and mock workers save each run's opportunities with SaveOpportunities and log what changed; a failed save fails the run, so the checkpoint stays put and the next run retries. Without a store nothing is persisted. The mock's opportunities are saved with Source "FTSMock" rather than the converter's "FindATender". fetcher service and job-search serve open the store at database.file (cli.OpenConfiguredStore), or warn and run without one. A missing file isn't created, and a database needing migration says to run job-search migrate. Fixed upsertOpportunity, which never wrote the source column, so every saved opportunity had an empty Source.
53 lines
1.3 KiB
Go
53 lines
1.3 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)
|
|
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
|
|
}
|