Skip to content

Commit

Permalink
add analytics to sqlite store
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasHiltl committed Nov 3, 2024
1 parent 7e47691 commit 7e69f22
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/go-github/v62 v62.0.0
github.com/gosimple/slug v1.14.0
github.com/guregu/null/v5 v5.0.0
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241013200840-98b510d4c198
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241103181214-07f2518d74c8
github.com/mattn/go-sqlite3 v1.14.16
github.com/naveensrinivasan/httpcache v1.2.1
github.com/olivere/ndjson v1.0.1
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/guregu/null/v5 v5.0.0/go.mod h1:SjupzNy+sCPtwQTKWhUCqjhVCO69hpsl2QsZr
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241013200840-98b510d4c198 h1:YZfwqDUZX191LR891bJ27xN8Fqna3hs0biPnzAfvjyY=
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241013200840-98b510d4c198/go.mod h1:xnbFN3Wrw2B1F/D7pvjjwoeXSt+njRI31ccPFno0BM0=
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241103181214-07f2518d74c8 h1:HzF4o06P65v3jx4ouPtXnPhWrKY/hf9We5X27rZ731k=
github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241103181214-07f2518d74c8/go.mod h1:1C1oY27qUCTAqi0SLZRCBGA6QoMTHkWtGlMMwh8pZxk=
github.com/kr/http v0.0.0-20150505212737-77bd98b60462 h1:b07ir+udaggBgCywmX2B8jIvNJJaEYXLjnLzIHm/uVg=
github.com/kr/http v0.0.0-20150505212737-77bd98b60462/go.mod h1:cM5SqzKqF3qr2a/EhVFFGK1+3Co6d40ogrTacbrLzvA=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down Expand Up @@ -107,8 +107,9 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
Expand Down
3 changes: 3 additions & 0 deletions internal/handler/rest/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func changelogToApiType(cl store.Changelog) apitypes.Changelog {
CreatedAt: cl.CreatedAt,
Protected: cl.Protected,
HasPassword: cl.PasswordHash != "",
Analytics: cl.Analytics,
}

if cl.GHSource.Valid {
Expand Down Expand Up @@ -83,6 +84,7 @@ func createChangelog(e *env, w http.ResponseWriter, r *http.Request) error {
LogoWidth: req.Logo.Width,
HidePoweredBy: req.HidePoweredBy,
Protected: req.Protected,
Analytics: req.Analytics,
}

if req.ColorScheme == "" {
Expand Down Expand Up @@ -158,6 +160,7 @@ func updateChangelog(e *env, w http.ResponseWriter, r *http.Request) error {
HidePoweredBy: req.HidePoweredBy,
Protected: req.Protected,
PasswordHash: hashedPassword,
Analytics: req.Analytics,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/store/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/store/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ INSERT INTO changelogs (
color_scheme,
hide_powered_by,
protected,
analytics,
password_hash
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING *;

-- name: deleteChangelog :exec
Expand Down Expand Up @@ -93,6 +94,7 @@ SET
logo_width = CASE WHEN cast(@set_logo_width as bool) THEN @logo_width ELSE logo_width END,
color_scheme = CASE WHEN cast(@set_color_scheme as bool) THEN @color_scheme ELSE color_scheme END,
protected = coalesce(sqlc.narg(protected), protected),
analytics = coalesce(sqlc.narg(analytics), analytics),
password_hash = CASE WHEN cast(@set_password_hash as bool) THEN @password_hash ELSE password_hash END
WHERE workspace_id = sqlc.arg(workspace_id) AND id = sqlc.arg(id)
RETURNING *;
Expand Down
27 changes: 19 additions & 8 deletions internal/store/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion internal/store/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (cl changelog) toExported(source changelogSource) Changelog {
ColorScheme: cl.ColorScheme,
HidePoweredBy: cl.HidePoweredBy == 1,
Protected: cl.Protected == 1,
Analytics: cl.Analytics == 1,
PasswordHash: cl.PasswordHash.V(),
CreatedAt: time.Unix(cl.CreatedAt, 0),
GHSource: null.NewValue(GHSource{}, false),
Expand Down Expand Up @@ -94,6 +95,7 @@ func (s *sqlite) CreateChangelog(ctx context.Context, cl Changelog) (Changelog,
ColorScheme: cl.ColorScheme,
HidePoweredBy: boolToInt(cl.HidePoweredBy),
Protected: boolToInt(cl.Protected),
Analytics: boolToInt(cl.Analytics),
PasswordHash: apitypes.NewString(cl.PasswordHash),
})
if err != nil {
Expand Down Expand Up @@ -197,10 +199,14 @@ func (s *sqlite) UpdateChangelog(ctx context.Context, wID WorkspaceID, cID Chang
SetLogoHeight: !args.LogoHeight.IsZero(),
LogoWidth: args.LogoWidth,
SetLogoWidth: !args.LogoWidth.IsZero(),
Protected: sql.NullInt64{ // update if Protected != nil
Protected: sql.NullInt64{ // update if args.Protected != nil
Int64: saveDerefToInt(args.Protected),
Valid: args.Protected != nil,
},
Analytics: sql.NullInt64{ // update if args.Analytics != nil
Int64: saveDerefToInt(args.Analytics),
Valid: args.Analytics != nil,
},
PasswordHash: args.PasswordHash,
SetPasswordHash: !args.PasswordHash.IsZero(),
})
Expand Down
1 change: 1 addition & 0 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type UpdateChangelogArgs struct {
ColorScheme ColorScheme
HidePoweredBy *bool
Protected *bool
Analytics *bool
PasswordHash apitypes.NullString
}

Expand Down
9 changes: 9 additions & 0 deletions migrations/20241103174950_changelog_analytics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE changelogs ADD analytics INTEGER NOT NULL DEFAULT 0 check (analytics in (0, 1));
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE changelogs DROP analytics;
-- +goose StatementEnd

0 comments on commit 7e69f22

Please sign in to comment.