Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add setting #211

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Serve(serverConfig ServerConfig) error {
e := echo.New()
e.HideBanner = true
e.HidePort = true
// e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
e.Use(echo.MiddlewareFunc(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
log.Debug().
Expand Down
8 changes: 8 additions & 0 deletions pkg/consts/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package consts

const (
// SettingSignPrivateKey is the private key for signing
SettingSignPrivateKey = "signing.private_key"
// SettingSignPublicKey is the public key for signing
SettingSignPublicKey = "signing.public_key"
)
1 change: 1 addition & 0 deletions pkg/dal/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func main() {
models.WorkQueue{},
models.Locker{},
models.Cache{},
models.Setting{},
)

g.ApplyInterface(func(models.CacheQuery) {}, models.Cache{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/dao/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (f *cacheServiceFactory) New(txs ...*query.Query) CacheService {
}
}

// Create creates a new work queue record in the database
// Create creates a new cache record in the database
func (s cacheService) Save(ctx context.Context, key string, val []byte, size int64, threshold float64) error {
total, err := s.tx.Cache.WithContext(ctx).Count()
if err != nil {
Expand Down
83 changes: 83 additions & 0 deletions pkg/dal/dao/mocks/setting.go

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

58 changes: 58 additions & 0 deletions pkg/dal/dao/mocks/setting_factory.go

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

73 changes: 73 additions & 0 deletions pkg/dal/dao/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dao

import (
"context"

"gorm.io/gorm"
"gorm.io/gorm/clause"

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
)

//go:generate mockgen -destination=mocks/setting.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao SettingService
//go:generate mockgen -destination=mocks/setting_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao SettingServiceFactory

// SettingService is the interface that provides methods to operate on setting model
type SettingService interface {
// Save save a new cache record in the database
Save(ctx context.Context, key string, val []byte) error
// Delete get a cache record
Delete(ctx context.Context, key string) error
// Get get a cache record
Get(ctx context.Context, key string) (*models.Setting, error)
}

type settingService struct {
tx *query.Query
}

// SettingServiceFactory is the interface that provides the setting service factory methods.
type SettingServiceFactory interface {
New(txs ...*query.Query) SettingService
}

type settingServiceFactory struct{}

// NewSettingServiceFactory creates a new setting service factory.
func NewSettingServiceFactory() SettingServiceFactory {
return &settingServiceFactory{}

Check warning on line 39 in pkg/dal/dao/setting.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/setting.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}

func (f *settingServiceFactory) New(txs ...*query.Query) SettingService {
tx := query.Q
if len(txs) > 0 {
tx = txs[0]
}
return &settingService{
tx: tx,
}

Check warning on line 49 in pkg/dal/dao/setting.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/setting.go#L42-L49

Added lines #L42 - L49 were not covered by tests
}

// Create creates a new setting record in the database
func (s settingService) Save(ctx context.Context, key string, val []byte) error {
var setting = models.Setting{Key: key, Val: val}
return s.tx.Setting.WithContext(ctx).Clauses(clause.OnConflict{UpdateAll: true}).Create(&setting)

Check warning on line 55 in pkg/dal/dao/setting.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/setting.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}

// Delete get a cache record
func (s settingService) Delete(ctx context.Context, key string) error {
matched, err := s.tx.Setting.WithContext(ctx).Unscoped().Where(s.tx.Setting.Key.Eq(key)).Delete()
if err != nil {
return err
}
if matched.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil

Check warning on line 67 in pkg/dal/dao/setting.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/setting.go#L59-L67

Added lines #L59 - L67 were not covered by tests
}

// Get get a cache record
func (s settingService) Get(ctx context.Context, key string) (*models.Setting, error) {
return s.tx.Setting.WithContext(ctx).Where(s.tx.Setting.Key.Eq(key)).First()

Check warning on line 72 in pkg/dal/dao/setting.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/setting.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}
6 changes: 6 additions & 0 deletions pkg/dal/migrations/mysql/0001_initialize.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ DROP TABLE IF EXISTS `code_repositories`;

DROP TABLE IF EXISTS `user_recover_codes`;

DROP TABLE IF EXISTS `work_queues`;

DROP TABLE IF EXISTS `caches`;

DROP TABLE IF EXISTS `settings`;

14 changes: 11 additions & 3 deletions pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ CREATE TABLE IF NOT EXISTS `tags` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`repository_id` bigint NOT NULL,
`artifact_id` bigint NOT NULL,
`name` varchar(64) NOT NULL,
`name` varchar(128) NOT NULL,
`pushed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_pull` timestamp,
`pull_times` bigint NOT NULL DEFAULT 0,
Expand Down Expand Up @@ -423,9 +423,17 @@ CREATE TABLE IF NOT EXISTS `caches` (
`val` BLOB NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` bigint NOT NULL DEFAULT 0,
CONSTRAINT `caches_unique_with_key` UNIQUE (`key`, `deleted_at`)
`deleted_at` bigint NOT NULL DEFAULT 0
);

CREATE INDEX `idx_created_at` ON `caches` (`created_at`);

CREATE TABLE IF NOT EXISTS `settings` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`key` varchar(256) NOT NULL UNIQUE,
`val` BLOB,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` bigint NOT NULL DEFAULT 0
);

6 changes: 6 additions & 0 deletions pkg/dal/migrations/postgresql/0001_initialize.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ DROP TABLE IF EXISTS "code_repositories";

DROP TABLE IF EXISTS "user_recover_codes";

DROP TABLE IF EXISTS "work_queues";

DROP TABLE IF EXISTS "caches";

DROP TABLE IF EXISTS "settings";

14 changes: 11 additions & 3 deletions pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ CREATE TABLE IF NOT EXISTS "tags" (
"id" bigserial PRIMARY KEY,
"repository_id" bigint NOT NULL,
"artifact_id" bigint NOT NULL,
"name" varchar(64) NOT NULL,
"name" varchar(128) NOT NULL,
"pushed_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"last_pull" timestamp,
"pull_times" bigint NOT NULL DEFAULT 0,
Expand Down Expand Up @@ -494,9 +494,17 @@ CREATE TABLE IF NOT EXISTS "caches" (
"val" bytea NOT NULL,
"created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted_at" integer NOT NULL DEFAULT 0,
CONSTRAINT "caches_unique_with_key" UNIQUE ("key", "deleted_at")
"deleted_at" integer NOT NULL DEFAULT 0
);

CREATE INDEX "idx_created_at" ON "caches" ("created_at");

CREATE TABLE IF NOT EXISTS "settings" (
"id" bigserial PRIMARY KEY,
"key" varchar(256) NOT NULL UNIQUE,
"val" bytea,
"created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted_at" bigint NOT NULL DEFAULT 0
);

6 changes: 6 additions & 0 deletions pkg/dal/migrations/sqlite3/0001_initialize.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ DROP TABLE IF EXISTS `code_repositories`;

DROP TABLE IF EXISTS `user_recover_codes`;

DROP TABLE IF EXISTS `work_queues`;

DROP TABLE IF EXISTS `caches`;

DROP TABLE IF EXISTS `settings`;

14 changes: 11 additions & 3 deletions pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ CREATE TABLE IF NOT EXISTS `tags` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`repository_id` integer NOT NULL,
`artifact_id` integer NOT NULL,
`name` varchar(64) NOT NULL,
`name` varchar(128) NOT NULL,
`pushed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_pull` timestamp,
`pull_times` integer NOT NULL DEFAULT 0,
Expand Down Expand Up @@ -421,9 +421,17 @@ CREATE TABLE IF NOT EXISTS `caches` (
`val` BLOB NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` integer NOT NULL DEFAULT 0,
CONSTRAINT `caches_unique_with_key` UNIQUE (`key`, `deleted_at`)
`deleted_at` integer NOT NULL DEFAULT 0
);

CREATE INDEX `idx_created_at` ON `caches` (`created_at`);

CREATE TABLE IF NOT EXISTS `settings` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`key` varchar(256) NOT NULL UNIQUE,
`val` BLOB,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` integer NOT NULL DEFAULT 0
);

18 changes: 18 additions & 0 deletions pkg/dal/models/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package models

import (
"time"

"gorm.io/plugin/soft_delete"
)

// Setting setting
type Setting struct {
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"`
ID int64 `gorm:"primaryKey"`

Key string `gorm:"uniqueIndex,size:256"`
Val []byte
}
Loading
Loading