Skip to content

Commit

Permalink
fix(core): get settings should return default values for null settings (
Browse files Browse the repository at this point in the history
#116)

* fix(core): get settings should return default values for null settings

* fix: clean up migration messages
  • Loading branch information
ayuhito authored Aug 5, 2024
1 parent 83398a9 commit 2da33cd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions core/db/sqlite/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (c *Client) GetSetting(ctx context.Context, key model.SettingsKey) (string,
func (c *Client) GetSettings(ctx context.Context) (*model.UserSettings, error) {
query := `--sql
SELECT
JSON_EXTRACT(settings, '$.language') AS language,
JSON_EXTRACT(settings, '$.script_type') AS script_type
COALESCE(JSON_EXTRACT(settings, '$.language'), 'en') AS language,
COALESCE(JSON_EXTRACT(settings, '$.script_type'), 'default') AS script_type
FROM users LIMIT 1`

settings := model.NewDefaultSettings()
Expand Down
2 changes: 0 additions & 2 deletions core/migrations/0001_sqlite_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func Up0001(c *sqlite.Client) error {
return err
}

// Moved user creation to 0006_sqlite_settings.go

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions core/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *Service) AutoMigrate(ctx context.Context) error {
id := typeid.String()

// Hash default password
auth, err := util.NewAuthService(context.Background(), false)
auth, err := util.NewAuthService(ctx, false)
if err != nil {
return err
}
Expand All @@ -193,7 +193,7 @@ func (s *Service) AutoMigrate(ctx context.Context) error {

dateCreated := time.Now().Unix()
dateUpdated := dateCreated
err = s.sqlite.CreateUser(context.Background(), model.NewUser(id, "admin", pwdHash, model.NewDefaultSettings(), dateCreated, dateUpdated))
err = s.sqlite.CreateUser(ctx, model.NewUser(id, "admin", pwdHash, model.NewDefaultSettings(), dateCreated, dateUpdated))
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions core/model/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ type UserSettings struct {
}

type WebsiteSettings struct{}

// NewSettings returns a new instance of Settings with default values.
func NewDefaultSettings() *UserSettings {
return &UserSettings{
Language: "en",
ScriptType: "default",
}
}
8 changes: 0 additions & 8 deletions core/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,3 @@ func NewUser(id string, username string, password string, settings *UserSettings
DateUpdated: dateUpdated,
}
}

// NewSettings returns a new instance of Settings with default values.
func NewDefaultSettings() *UserSettings {
return &UserSettings{
Language: "en",
ScriptType: "default",
}
}

0 comments on commit 2da33cd

Please sign in to comment.