Skip to content

Commit

Permalink
chore: retire webhook state
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Jan 10, 2025
1 parent 2a861ea commit c1498a1
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 179 deletions.
6 changes: 2 additions & 4 deletions proto/api/v1/webhook_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ message Webhook {

google.protobuf.Timestamp update_time = 4;

State state = 5;
string name = 5;

string name = 6;

string url = 7;
string url = 6;
}

message CreateWebhookRequest {
Expand Down
237 changes: 112 additions & 125 deletions proto/gen/api/v1/webhook_service.pb.go

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions proto/gen/apidocs.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,6 @@ paths:
updateTime:
type: string
format: date-time
state:
$ref: '#/definitions/v1State'
name:
type: string
url:
Expand Down Expand Up @@ -3056,8 +3054,6 @@ definitions:
updateTime:
type: string
format: date-time
state:
$ref: '#/definitions/v1State'
name:
type: string
url:
Expand Down
1 change: 0 additions & 1 deletion server/router/api/v1/webhook_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func convertWebhookFromStore(webhook *store.Webhook) *v1pb.Webhook {
Id: webhook.ID,
CreateTime: timestamppb.New(time.Unix(webhook.CreatedTs, 0)),
UpdateTime: timestamppb.New(time.Unix(webhook.UpdatedTs, 0)),
State: convertStateFromStore(webhook.RowStatus),
CreatorId: webhook.CreatorID,
Name: webhook.Name,
Url: webhook.URL,
Expand Down
8 changes: 1 addition & 7 deletions store/db/mysql/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
where, args = append(where, "`creator_id` = ?"), append(args, *find.CreatorID)
}

rows, err := d.db.QueryContext(ctx, "SELECT `id`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `row_status`, `creator_id`, `name`, `url` FROM `webhook` WHERE "+strings.Join(where, " AND ")+" ORDER BY `id` DESC",
rows, err := d.db.QueryContext(ctx, "SELECT `id`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `creator_id`, `name`, `url` FROM `webhook` WHERE "+strings.Join(where, " AND ")+" ORDER BY `id` DESC",
args...,
)
if err != nil {
Expand All @@ -47,19 +47,16 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
list := []*store.Webhook{}
for rows.Next() {
webhook := &store.Webhook{}
var rowStatus string
if err := rows.Scan(
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorID,
&webhook.Name,
&webhook.URL,
); err != nil {
return nil, err
}
webhook.RowStatus = store.RowStatus(rowStatus)
list = append(list, webhook)
}

Expand All @@ -83,9 +80,6 @@ func (d *DB) GetWebhook(ctx context.Context, find *store.FindWebhook) (*store.We

func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
set, args := []string{}, []any{}
if update.RowStatus != nil {
set, args = append(set, "`row_status` = ?"), append(args, update.RowStatus.String())
}
if update.Name != nil {
set, args = append(set, "`name` = ?"), append(args, *update.Name)
}
Expand Down
18 changes: 2 additions & 16 deletions store/db/postgres/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import (
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
fields := []string{"name", "url", "creator_id"}
args := []any{create.Name, create.URL, create.CreatorID}
stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts, row_status"
var rowStatus string
stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&rowStatus,
); err != nil {
return nil, err
}

create.RowStatus = store.RowStatus(rowStatus)
webhook := create
return webhook, nil
}
Expand All @@ -40,7 +36,6 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
id,
created_ts,
updated_ts,
row_status,
creator_id,
name,
url
Expand All @@ -57,19 +52,16 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
list := []*store.Webhook{}
for rows.Next() {
webhook := &store.Webhook{}
var rowStatus string
if err := rows.Scan(
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorID,
&webhook.Name,
&webhook.URL,
); err != nil {
return nil, err
}
webhook.RowStatus = store.RowStatus(rowStatus)
list = append(list, webhook)
}

Expand All @@ -82,32 +74,26 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor

func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
set, args := []string{}, []any{}
if update.RowStatus != nil {
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
}
if update.Name != nil {
set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
}
if update.URL != nil {
set, args = append(set, "url = "+placeholder(len(args)+1)), append(args, *update.URL)
}

stmt := "UPDATE webhook SET " + strings.Join(set, ", ") + " WHERE id = " + placeholder(len(args)+1) + " RETURNING id, created_ts, updated_ts, row_status, creator_id, name, url"
stmt := "UPDATE webhook SET " + strings.Join(set, ", ") + " WHERE id = " + placeholder(len(args)+1) + " RETURNING id, created_ts, updated_ts, creator_id, name, url"
args = append(args, update.ID)
webhook := &store.Webhook{}
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorID,
&webhook.Name,
&webhook.URL,
); err != nil {
return nil, err
}
webhook.RowStatus = store.RowStatus(rowStatus)
return webhook, nil
}

Expand Down
18 changes: 2 additions & 16 deletions store/db/sqlite/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.W
fields := []string{"`name`", "`url`", "`creator_id`"}
placeholder := []string{"?", "?", "?"}
args := []any{create.Name, create.URL, create.CreatorID}
stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`"
var rowStatus string
stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&rowStatus,
); err != nil {
return nil, err
}

create.RowStatus = store.RowStatus(rowStatus)
webhook := create
return webhook, nil
}
Expand All @@ -41,7 +37,6 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
id,
created_ts,
updated_ts,
row_status,
creator_id,
name,
url
Expand All @@ -58,19 +53,16 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
list := []*store.Webhook{}
for rows.Next() {
webhook := &store.Webhook{}
var rowStatus string
if err := rows.Scan(
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorID,
&webhook.Name,
&webhook.URL,
); err != nil {
return nil, err
}
webhook.RowStatus = store.RowStatus(rowStatus)
list = append(list, webhook)
}

Expand All @@ -83,9 +75,6 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor

func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
set, args := []string{}, []any{}
if update.RowStatus != nil {
set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String())
}
if update.Name != nil {
set, args = append(set, "name = ?"), append(args, *update.Name)
}
Expand All @@ -94,21 +83,18 @@ func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*s
}
args = append(args, update.ID)

stmt := "UPDATE `webhook` SET " + strings.Join(set, ", ") + " WHERE `id` = ? RETURNING `id`, `created_ts`, `updated_ts`, `row_status`, `creator_id`, `name`, `url`"
stmt := "UPDATE `webhook` SET " + strings.Join(set, ", ") + " WHERE `id` = ? RETURNING `id`, `created_ts`, `updated_ts`, `creator_id`, `name`, `url`"
webhook := &store.Webhook{}
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorID,
&webhook.Name,
&webhook.URL,
); err != nil {
return nil, err
}
webhook.RowStatus = store.RowStatus(rowStatus)
return webhook, nil
}

Expand Down
8 changes: 3 additions & 5 deletions store/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Webhook struct {
CreatedTs int64
UpdatedTs int64
CreatorID int32
RowStatus RowStatus
Name string
URL string
}
Expand All @@ -20,10 +19,9 @@ type FindWebhook struct {
}

type UpdateWebhook struct {
ID int32
RowStatus *RowStatus
Name *string
URL *string
ID int32
Name *string
URL *string
}

type DeleteWebhook struct {
Expand Down
1 change: 0 additions & 1 deletion test/store/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestWebhookStore(t *testing.T) {
CreatorID: user.ID,
Name: "test_webhook",
URL: "https://example.com",
RowStatus: store.Normal,
})
require.NoError(t, err)
require.Equal(t, "test_webhook", webhook.Name)
Expand Down

0 comments on commit c1498a1

Please sign in to comment.