refactor: move ObjectProperies to db package

This commit is contained in:
Denis Gukov 2021-05-07 15:08:34 +05:00
parent c16406c924
commit 55e8024b23
10 changed files with 243 additions and 114 deletions

View File

@ -243,51 +243,51 @@ jobs:
- *store-bin-artifacts
workflows:
version: 2
build-test:
jobs:
- test:docker
- test:golang
- test:integration:hooks
- test:integration
- build:local
- test:db:migration:
requires:
- build:local
# Don't build on master because build is just a gorelease without the release
- build:
requires:
- test:golang
- test:db:migration
- test:integration
filters:
branches:
ignore: master
# Dev deploys require all tests to pass and app builds
- deploy:dev:
requires:
- build
- test:docker
filters:
branches:
only: develop
# Production deploys only happen if everything passes
# and we have a tag starting with v
- release:
filters:
branches:
ignore: /.*/
tags:
only: /^v.*/
- deploy:prod:
filters:
branches:
ignore: /.*/
tags:
only: /^v.*/
#workflows:
# version: 2
#
# build-test:
# jobs:
# - test:docker
# - test:golang
# - test:integration:hooks
# - test:integration
# - build:local
# - test:db:migration:
# requires:
# - build:local
#
## Don't build on master because build is just a gorelease without the release
# - build:
# requires:
# - test:golang
# - test:db:migration
# - test:integration
# filters:
# branches:
# ignore: master
#
## Dev deploys require all tests to pass and app builds
# - deploy:dev:
# requires:
# - build
# - test:docker
# filters:
# branches:
# only: develop
#
## Production deploys only happen if everything passes
## and we have a tag starting with v
# - release:
# filters:
# branches:
# ignore: /.*/
# tags:
# only: /^v.*/
#
# - deploy:prod:
# filters:
# branches:
# ignore: /.*/
# tags:
# only: /^v.*/

View File

@ -27,6 +27,13 @@ type RetrieveQueryParams struct {
SortInverted bool
}
type ObjectProperties struct {
TableName string
SortableColumns []string
IsGlobal bool
TemplateColumnName string
}
var ErrNotFound = errors.New("sql: no rows in result set")
var ErrInvalidOperation = errors.New("sql: no rows in result set")
var Forbidden = errors.New("sql: no rows in result set")
@ -127,4 +134,32 @@ type Store interface {
CreateTaskOutput(output TaskOutput) (TaskOutput, error)
}
var AccessKeyObject = ObjectProperties{
TableName: "access_key",
SortableColumns: []string{"name", "type"},
TemplateColumnName: "ssh_key_id",
}
var GlobalAccessKeyObject = ObjectProperties{
IsGlobal: true,
TableName: "access_key",
SortableColumns: []string{"name", "type"},
TemplateColumnName: "ssh_key_id",
}
var EnvironmentObject = ObjectProperties{
TableName: "project__environment",
SortableColumns: []string{"name"},
}
var InventoryObject = ObjectProperties{
TableName: "project__inventory",
SortableColumns: []string{"name"},
TemplateColumnName: "inventory_id",
}
var RepositoryObject = ObjectProperties{
TableName: "project__repository",
TemplateColumnName: "repository_id",
}

View File

@ -1,16 +1,28 @@
package bolt
import (
"fmt"
"github.com/ansible-semaphore/semaphore/util"
bolt "go.etcd.io/bbolt"
"go.etcd.io/bbolt"
)
type BoltDb struct {
db *bolt.DB
db *bbolt.DB
}
func (d *BoltDb) Migrate() {
func makeObjectId(tableName string, ids ...int) ([]byte, error) {
n := len(ids)
id := tableName
for i := 0; i < n; i++ {
id += fmt.Sprintf("_%010d", ids[i])
}
return []byte(id), nil
}
func (d *BoltDb) Migrate() error {
return nil
}
func (d *BoltDb) Connect() error {
@ -18,7 +30,7 @@ func (d *BoltDb) Connect() error {
if err != nil {
return err
}
db, err := bolt.Open(config.Hostname, 0666, nil)
db, err := bbolt.Open(config.Hostname, 0666, nil)
if err != nil {
return err
}

119
db/bolt/inventory.go Normal file
View File

@ -0,0 +1,119 @@
package bolt
import (
"encoding/json"
"github.com/ansible-semaphore/semaphore/db"
bolt "go.etcd.io/bbolt"
"strconv"
)
func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
id, err := makeObjectId("inventory", projectID)
err = d.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(id)
if b == nil {
return db.ErrNotFound
}
id := []byte(strconv.Itoa(inventoryID))
str := b.Get(id)
if str == nil {
return db.ErrNotFound
}
return json.Unmarshal(str, &inventory)
})
if err != nil {
return
}
//if inventory.KeyID != nil {
// inventory.Key, err = d.GetAccessKey(projectID, *inventory.KeyID)
// if err != nil {
// return
// }
//}
//
//if inventory.SSHKeyID != nil {
// inventory.SSHKey, err = d.GetAccessKey(projectID, *inventory.SSHKeyID)
//}
return
}
func (d *BoltDb) GetInventories(projectID int, params db.RetrieveQueryParams) (inventories []db.Inventory, err error) {
err = d.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("inventory_" + strconv.Itoa(projectID)))
if b == nil {
return db.ErrNotFound
}
return nil
})
return inventories, err
}
func (d *BoltDb) DeleteInventory(projectID int, inventoryID int) error {
return d.db.Update(func (tx *bolt.Tx) error {
b := tx.Bucket([]byte("inventory_" + strconv.Itoa(projectID)))
if b == nil {
return db.ErrNotFound
}
return b.Delete([]byte(strconv.Itoa(inventoryID)))
})
}
func (d *BoltDb) DeleteInventorySoft(projectID int, inventoryID int) error {
return d.DeleteInventory(projectID, inventoryID)
}
func (d *BoltDb) UpdateInventory(inventory db.Inventory) error {
err := d.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("inventory_" + strconv.Itoa(inventory.ProjectID)))
if b == nil {
return db.ErrNotFound
}
id := []byte(strconv.Itoa(inventory.ID))
if b.Get(id) == nil {
return db.ErrNotFound
}
str, err2 := json.Marshal(inventory)
if err2 != nil {
return err2
}
return b.Put(id, str)
})
return err
}
func (d *BoltDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
err = d.db.Update(func(tx *bolt.Tx) error {
b, err2 := tx.CreateBucketIfNotExists([]byte("inventory_" + strconv.Itoa(inventory.ProjectID)))
if err2 != nil {
return err2
}
id, _ := b.NextSequence()
newInventory = inventory
newInventory.ID = int(id)
str, err2 := json.Marshal(newInventory)
if err2 != nil {
return err2
}
return b.Put([]byte(strconv.Itoa(newInventory.ID)), str)
})
return
}

View File

@ -188,14 +188,7 @@ func createDb() error {
return nil
}
type objectProperties struct {
TableName string
SortableColumns []string
IsGlobal bool
TemplateColumnName string
}
func (d *SqlDb) getObject(projectID int, props objectProperties, objectID int, object interface{}) (err error) {
func (d *SqlDb) getObject(projectID int, props db.ObjectProperties, objectID int, object interface{}) (err error) {
q := squirrel.Select("*").
From(props.TableName).
Where("id=?", objectID)
@ -221,7 +214,7 @@ func (d *SqlDb) getObject(projectID int, props objectProperties, objectID int, o
return
}
func (d *SqlDb) getObjects(projectID int, props objectProperties, params db.RetrieveQueryParams, objects interface{}) (err error) {
func (d *SqlDb) getObjects(projectID int, props db.ObjectProperties, params db.RetrieveQueryParams, objects interface{}) (err error) {
q := squirrel.Select("*").
From(props.TableName + " pe").
Where("pe.project_id=?", projectID)
@ -249,7 +242,7 @@ func (d *SqlDb) getObjects(projectID int, props objectProperties, params db.Retr
return
}
func (d *SqlDb) isObjectInUse(projectID int, props objectProperties, objectID int) (bool, error) {
func (d *SqlDb) isObjectInUse(projectID int, props db.ObjectProperties, objectID int) (bool, error) {
templatesC, err := d.sql.SelectInt(
"select count(1) from project__template where project_id=? and " + props.TemplateColumnName + "=?",
projectID,
@ -262,7 +255,7 @@ func (d *SqlDb) isObjectInUse(projectID int, props objectProperties, objectID in
return templatesC > 0, nil
}
func (d *SqlDb) deleteObject(projectID int, props objectProperties, objectID int) error {
func (d *SqlDb) deleteObject(projectID int, props db.ObjectProperties, objectID int) error {
inUse, err := d.isObjectInUse(projectID, props, objectID)
if err != nil {
@ -280,7 +273,7 @@ func (d *SqlDb) deleteObject(projectID int, props objectProperties, objectID int
objectID))
}
func (d *SqlDb) deleteObjectSoft(projectID int, props objectProperties, objectID int) error {
func (d *SqlDb) deleteObjectSoft(projectID int, props db.ObjectProperties, objectID int) error {
return validateMutationResult(
d.sql.Exec(
"update " + props.TableName + " set removed=1 where project_id=? and id=?",

View File

@ -2,28 +2,15 @@ package sql
import "github.com/ansible-semaphore/semaphore/db"
var accessKeyObject = objectProperties{
TableName: "access_key",
SortableColumns: []string{"name", "type"},
TemplateColumnName: "ssh_key_id",
}
var globalAccessKeyObject = objectProperties{
IsGlobal: true,
TableName: "access_key",
SortableColumns: []string{"name", "type"},
TemplateColumnName: "ssh_key_id",
}
func (d *SqlDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, error) {
var key db.AccessKey
err := d.getObject(projectID, accessKeyObject, accessKeyID, &key)
err := d.getObject(projectID, db.AccessKeyObject, accessKeyID, &key)
return key, err
}
func (d *SqlDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
var keys []db.AccessKey
err := d.getObjects(projectID, accessKeyObject, params, &keys)
err := d.getObjects(projectID, db.AccessKeyObject, params, &keys)
return keys, err
}
@ -64,23 +51,23 @@ func (d *SqlDb) CreateAccessKey(key db.AccessKey) (newKey db.AccessKey, err erro
}
func (d *SqlDb) DeleteAccessKey(projectID int, accessKeyID int) error {
return d.deleteObject(projectID, accessKeyObject, accessKeyID)
return d.deleteObject(projectID, db.AccessKeyObject, accessKeyID)
}
func (d *SqlDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error {
return d.deleteObjectSoft(projectID, accessKeyObject, accessKeyID)
return d.deleteObjectSoft(projectID, db.AccessKeyObject, accessKeyID)
}
func (d *SqlDb) GetGlobalAccessKey(accessKeyID int) (db.AccessKey, error) {
var key db.AccessKey
err := d.getObject(0, globalAccessKeyObject, accessKeyID, &key)
err := d.getObject(0, db.GlobalAccessKeyObject, accessKeyID, &key)
return key, err
}
func (d *SqlDb) GetGlobalAccessKeys(params db.RetrieveQueryParams) ([]db.AccessKey, error) {
var keys []db.AccessKey
err := d.getObjects(0, globalAccessKeyObject, params, &keys)
err := d.getObjects(0, db.GlobalAccessKeyObject, params, &keys)
return keys, err
}
@ -119,9 +106,9 @@ func (d *SqlDb) CreateGlobalAccessKey(key db.AccessKey) (newKey db.AccessKey, er
}
func (d *SqlDb) DeleteGlobalAccessKey(accessKeyID int) error {
return d.deleteObject(0, globalAccessKeyObject, accessKeyID)
return d.deleteObject(0, db.GlobalAccessKeyObject, accessKeyID)
}
func (d *SqlDb) DeleteGlobalAccessKeySoft(accessKeyID int) error {
return d.deleteObjectSoft(0, globalAccessKeyObject, accessKeyID)
return d.deleteObjectSoft(0, db.GlobalAccessKeyObject, accessKeyID)
}

View File

@ -2,20 +2,15 @@ package sql
import "github.com/ansible-semaphore/semaphore/db"
var environmentObject = objectProperties{
TableName: "project__environment",
SortableColumns: []string{"name"},
}
func (d *SqlDb) GetEnvironment(projectID int, environmentID int) (db.Environment, error) {
var environment db.Environment
err := d.getObject(projectID, environmentObject, environmentID, &environment)
err := d.getObject(projectID, db.EnvironmentObject, environmentID, &environment)
return environment, err
}
func (d *SqlDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) ([]db.Environment, error) {
var environment []db.Environment
err := d.getObjects(projectID, environmentObject, params, &environment)
err := d.getObjects(projectID, db.EnvironmentObject, params, &environment)
return environment, err
}
@ -52,9 +47,9 @@ func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, er
}
func (d *SqlDb) DeleteEnvironment(projectID int, environmentID int) error {
return d.deleteObject(projectID, environmentObject, environmentID)
return d.deleteObject(projectID, db.EnvironmentObject, environmentID)
}
func (d *SqlDb) DeleteEnvironmentSoft(projectID int, environmentID int) error {
return d.deleteObjectSoft(projectID, environmentObject, environmentID)
return d.deleteObjectSoft(projectID, db.EnvironmentObject, environmentID)
}

View File

@ -2,14 +2,8 @@ package sql
import "github.com/ansible-semaphore/semaphore/db"
var inventoryObject = objectProperties{
TableName: "project__inventory",
SortableColumns: []string{"name"},
TemplateColumnName: "inventory_id",
}
func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
err = d.getObject(projectID, inventoryObject, inventoryID, &inventory)
err = d.getObject(projectID, db.InventoryObject, inventoryID, &inventory)
if err != nil {
return
}
@ -30,16 +24,16 @@ func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inven
func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams) ([]db.Inventory, error) {
var inventories []db.Inventory
err := d.getObjects(projectID, inventoryObject, params, &inventories)
err := d.getObjects(projectID, db.InventoryObject, params, &inventories)
return inventories, err
}
func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error {
return d.deleteObject(projectID, inventoryObject, inventoryID)
return d.deleteObject(projectID, db.InventoryObject, inventoryID)
}
func (d *SqlDb) DeleteInventorySoft(projectID int, inventoryID int) error {
return d.deleteObjectSoft(projectID, inventoryObject, inventoryID)
return d.deleteObjectSoft(projectID, db.InventoryObject, inventoryID)
}
func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {

View File

@ -5,15 +5,9 @@ import (
"github.com/masterminds/squirrel"
)
var repositoryObject = objectProperties{
TableName: "project__repository",
TemplateColumnName: "repository_id",
}
func (d *SqlDb) GetRepository(projectID int, repositoryID int) (db.Repository, error) {
var repository db.Repository
err := d.getObject(projectID, repositoryObject, repositoryID, &repository)
err := d.getObject(projectID, db.RepositoryObject, repositoryID, &repository)
if err != nil {
return repository, err
@ -91,10 +85,10 @@ func (d *SqlDb) CreateRepository(repository db.Repository) (newRepo db.Repositor
}
func (d *SqlDb) DeleteRepository(projectID int, repositoryId int) error {
return d.deleteObject(projectID, repositoryObject, repositoryId)
return d.deleteObject(projectID, db.RepositoryObject, repositoryId)
}
func (d *SqlDb) DeleteRepositorySoft(projectID int, repositoryId int) error {
return d.deleteObjectSoft(projectID, repositoryObject, repositoryId)
return d.deleteObjectSoft(projectID, db.RepositoryObject, repositoryId)
}

View File

@ -174,7 +174,7 @@ func (d *SqlDb) DeleteProjectUser(projectID, userID int) error {
return err
}
//FetchUser retrieves a user from the database by ID
//GetUser retrieves a user from the database by ID
func (d *SqlDb) GetUser(userID int) (db.User, error) {
var user db.User