Skip to content

Commit

Permalink
cmd/worklog/api: remove deprecated DatabaseDir configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Oct 27, 2024
1 parent 8dffca5 commit a541950
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 202 deletions.
3 changes: 0 additions & 3 deletions cmd/worklog/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ type Config struct {
Hostname string `json:"hostname,omitempty"`
Heartbeat *rpc.Duration `json:"heartbeat,omitempty"`
Rules map[string]Rule `json:"rules,omitempty"`

// Deprecated: Use Database with sqlite scheme.
DatabaseDir string `json:"database_dir,omitempty"` // Relative to XDG_STATE_HOME.
} `json:"options,omitempty"`
}

Expand Down
16 changes: 3 additions & 13 deletions cmd/worklog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,8 @@ func (d *daemon) Handle(ctx context.Context, req *jsonrpc2.Request) (any, error)
return nil, rpc.NewError(rpc.ErrCodeInvalidMessage,
err.Error(),
map[string]any{
"type": rpc.ErrCodeParameters,
"database": m.Body.Options.Database,
"database_dir": m.Body.Options.DatabaseDir,
"type": rpc.ErrCodeParameters,
"database": m.Body.Options.Database,
},
)
}
Expand Down Expand Up @@ -484,10 +483,7 @@ func (d *daemon) Handle(ctx context.Context, req *jsonrpc2.Request) (any, error)
func dbDir(cfg worklog.Config) (scheme, dir string, err error) {
opt := cfg.Options
if opt.Database == "" {
if opt.DatabaseDir != "" {
scheme = "sqlite"
}
return scheme, opt.DatabaseDir, nil
return "", "", nil
}
u, err := url.Parse(opt.Database)
if err != nil {
Expand All @@ -497,17 +493,11 @@ func dbDir(cfg worklog.Config) (scheme, dir string, err error) {
case "":
return "", "", errors.New("missing scheme in database configuration")
case "sqlite":
if opt.DatabaseDir != "" && u.Opaque != opt.DatabaseDir {
return "", "", fmt.Errorf("inconsistent database directory configuration: (%s:)%s != %s", u.Scheme, u.Opaque, opt.DatabaseDir)
}
if u.Opaque == "" {
return "", "", fmt.Errorf("sqlite configuration missing opaque data: %s", opt.Database)
}
return u.Scheme, u.Opaque, nil
default:
if opt.DatabaseDir != "" {
return "", "", fmt.Errorf("inconsistent database configuration: both %s database and sqlite directory configured", u.Scheme)
}
return u.Scheme, "", nil
}
}
Expand Down
47 changes: 6 additions & 41 deletions cmd/worklog/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func TestDaemon(t *testing.T) {
Hostname string `json:"hostname,omitempty"`
Heartbeat *rpc.Duration `json:"heartbeat,omitempty"`
Rules map[string]worklog.Rule `json:"rules,omitempty"`
DatabaseDir string `json:"database_dir,omitempty"` // Relative to XDG_STATE_HOME.
}
err := conn.Call(ctx, "configure", rpc.NewMessage(uid, worklog.Config{
Options: options{
Expand Down Expand Up @@ -1732,62 +1731,28 @@ var dbDirTests = []struct {
name: "none",
},
{
name: "deprecated",
config: mkDBDirOptions("database_directory", ""),
name: "sqlite",
config: mkDBDirOptions("sqlite:database_directory"),
wantScheme: "sqlite",
wantDir: "database_directory",
},
{
name: "url_only_sqlite",
config: mkDBDirOptions("", "sqlite:database_directory"),
wantScheme: "sqlite",
wantDir: "database_directory",
},
{
name: "url_only_postgres",
config: mkDBDirOptions("", "postgres://username:password@localhost:5432/database_name"),
name: "postgres",
config: mkDBDirOptions("postgres://username:password@localhost:5432/database_name"),
wantScheme: "postgres",
wantDir: "",
},
{
name: "both_consistent",
config: mkDBDirOptions("database_directory", "sqlite:database_directory"),
wantScheme: "sqlite",
wantDir: "database_directory",
},
{
name: "missing_scheme",
config: mkDBDirOptions("database_dir", "database_directory"),
wantScheme: "",
wantDir: "",
wantErr: errors.New("missing scheme in database configuration"),
},
{
name: "both_inconsistent_sqlite",
config: mkDBDirOptions("database_dir", "sqlite:database_directory"),
wantScheme: "",
wantDir: "",
wantErr: errors.New("inconsistent database directory configuration: (sqlite:)database_directory != database_dir"),
},
{
name: "invalid_sqlite_url",
config: mkDBDirOptions("", "sqlite:/database_directory"),
config: mkDBDirOptions("sqlite:/database_directory"),
wantScheme: "",
wantDir: "",
wantErr: errors.New("sqlite configuration missing opaque data: sqlite:/database_directory"),
},
{
name: "both_inconsistent_postgres",
config: mkDBDirOptions("database_dir", "postgres://username:password@localhost:5432/database_name"),
wantScheme: "",
wantDir: "",
wantErr: errors.New("inconsistent database configuration: both postgres database and sqlite directory configured"),
},
}

func mkDBDirOptions(dir, url string) worklog.Config {
func mkDBDirOptions(url string) worklog.Config {
var cfg worklog.Config
cfg.Options.DatabaseDir = dir
cfg.Options.Database = url
return cfg
}
Expand Down
143 changes: 0 additions & 143 deletions testdata/worklog_load_deprecated.txt

This file was deleted.

4 changes: 2 additions & 2 deletions testdata/worklog_remote_summary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ log_level = "debug"
log_add_source = true

[module.worklog1.options]
database_dir = "worklog1"
database = "sqlite:worklog1"
hostname = "localhost1"
[module.worklog1.options.web]
addr = "localhost:7171"
Expand Down Expand Up @@ -70,7 +70,7 @@ log_level = "debug"
log_add_source = true

[module.worklog2.options]
database_dir = "worklog2"
database = "sqlite:worklog2"
hostname = "localhost2"
[module.worklog2.options.web]
addr = "localhost:7272"
Expand Down

0 comments on commit a541950

Please sign in to comment.