Skip to content

Conversation

@jbrockopp
Copy link
Contributor

@jbrockopp jbrockopp commented Oct 7, 2022

Based off of #574, #663, #687 and #692

This change continues the refactor efforts initially introduced in the above PRs.

This adds a new hook package to the github.com/go-vela/server/database package.

This contains a HookService interface declaring all functions necessary for worker based interactions with the database:

// HookService represents the Vela interface for hook
// functions with the supported Database backends.
//
//nolint:revive // ignore name stutter
type HookService interface {
// Hook Data Definition Language Functions
//
// https://en.wikipedia.org/wiki/Data_definition_language
// CreateHookIndexes defines a function that creates the indexes for the hooks table.
CreateHookIndexes() error
// CreateHookTable defines a function that creates the hooks table.
CreateHookTable(string) error
// Hook Data Manipulation Language Functions
//
// https://en.wikipedia.org/wiki/Data_manipulation_language
// CountHooks defines a function that gets the count of all hooks.
CountHooks() (int64, error)
// CountHooksForRepo defines a function that gets the count of hooks by repo ID.
CountHooksForRepo(*library.Repo) (int64, error)
// CreateHook defines a function that creates a new hook.
CreateHook(*library.Hook) error
// DeleteHook defines a function that deletes an existing hook.
DeleteHook(*library.Hook) error
// GetHook defines a function that gets a hook by ID.
GetHook(int64) (*library.Hook, error)
// GetHookForRepo defines a function that gets a hook by repo ID and number.
GetHookForRepo(*library.Repo, int) (*library.Hook, error)
// LastHookForRepo defines a function that gets the last hook by repo ID.
LastHookForRepo(*library.Repo) (*library.Hook, error)
// ListHooks defines a function that gets a list of all hooks.
ListHooks() ([]*library.Hook, error)
// ListHooksForRepo defines a function that gets a list of hooks by repo ID.
ListHooksForRepo(*library.Repo, int, int) ([]*library.Hook, int64, error)
// UpdateHook defines a function that updates an existing hook.
UpdateHook(*library.Hook) error
}

This package also contains the engine which implements the above service interface:

// engine represents the hook functionality that implements the HookService interface.
engine struct {
// engine configuration settings used in hook functions
config *config
// gorm.io/gorm database client used in hook functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
client *gorm.DB
// sirupsen/logrus logger used in hook functions
//
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
logger *logrus.Entry
}

This engine contains no raw SQL queries for integrating with the hooks table.

Instead, we leverage our DB library's (https://gorm.io/) agnostic abstraction for integrating with that table.

jbrockopp and others added 30 commits February 23, 2022 13:45
@jbrockopp jbrockopp self-assigned this Oct 7, 2022
@codecov
Copy link

codecov bot commented Oct 8, 2022

Codecov Report

Merging #721 (6b87304) into main (74d2a68) will decrease coverage by 0.18%.
The diff coverage is 78.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #721      +/-   ##
==========================================
- Coverage   54.59%   54.40%   -0.19%     
==========================================
  Files         216      224       +8     
  Lines       16178    16168      -10     
==========================================
- Hits         8832     8797      -35     
- Misses       6962     6978      +16     
- Partials      384      393       +9     
Impacted Files Coverage Δ
api/hook.go 0.00% <0.00%> (ø)
api/webhook.go 0.00% <0.00%> (ø)
database/postgres/build.go 95.86% <ø> (ø)
database/sqlite/build.go 95.86% <ø> (ø)
database/hook/hook.go 51.72% <51.72%> (ø)
database/hook/last_repo.go 70.83% <70.83%> (ø)
database/hook/list.go 72.72% <72.72%> (ø)
database/sqlite/sqlite.go 64.70% <72.72%> (+1.26%) ⬆️
database/postgres/postgres.go 65.06% <76.92%> (+1.49%) ⬆️
database/hook/list_repo.go 79.06% <79.06%> (ø)
... and 13 more

@jbrockopp jbrockopp marked this pull request as ready for review December 21, 2022 20:19
@jbrockopp jbrockopp requested a review from a team as a code owner December 21, 2022 20:19
@jbrockopp jbrockopp added the enhancement Indicates an improvement to a feature label Jan 6, 2023
Copy link
Collaborator

@wass3r wass3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Copy link
Contributor

@ecrupper ecrupper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - lgtm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Indicates an improvement to a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants