Skip to content

Commit

Permalink
refactor(database): use agnostic engine (#873)
Browse files Browse the repository at this point in the history
* feat(database): add agnostic engine

* feat(database): add config.Validate()

* feat(database): add engine.Close()

* feat(database): add engine.Driver()

* chore: minor fixes

* feat(database): add engine.Ping()

* feat(database): add engine.NewResources()

* refactor(database): use agnostic engine

* feat(database): add FromCLIContext()

* chore: use database.FromCLIContext()

* chore(database): remove unused code

* chore: misc updates

* chore: fix typos

---------

Co-authored-by: Easton Crupper <[email protected]>
  • Loading branch information
jbrockopp and ecrupper authored Jun 8, 2023
1 parent da73438 commit 470791d
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 535 deletions.
35 changes: 0 additions & 35 deletions cmd/vela-server/database.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router"
"github.com/go-vela/server/router/middleware"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -61,7 +62,7 @@ func server(c *cli.Context) error {
return err
}

database, err := setupDatabase(c)
database, err := database.FromCLIContext(c)
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion database/context.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package database

import (
"context"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

const key = "database"
Expand Down Expand Up @@ -35,3 +38,19 @@ func FromContext(c context.Context) Interface {
func ToContext(c Setter, d Interface) {
c.Set(key, d)
}

// FromCLIContext creates and returns a database engine from the urfave/cli context.
func FromCLIContext(c *cli.Context) (Interface, error) {
logrus.Debug("creating database engine from CLI configuration")

return New(&Config{
Address: c.String("database.addr"),
CompressionLevel: c.Int("database.compression.level"),
ConnectionLife: c.Duration("database.connection.life"),
ConnectionIdle: c.Int("database.connection.idle"),
ConnectionOpen: c.Int("database.connection.open"),
Driver: c.String("database.driver"),
EncryptionKey: c.String("database.encryption.key"),
SkipCreation: c.Bool("database.skip_creation"),
})
}
177 changes: 119 additions & 58 deletions database/context_test.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,152 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package database

import (
"flag"
"reflect"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database/sqlite"
"github.com/urfave/cli/v2"
)

func TestDatabase_FromContext(t *testing.T) {
// setup types
want, _ := sqlite.NewTest()
_postgres, _ := testPostgres(t)
defer _postgres.Close()

defer func() { _sql, _ := want.Sqlite.DB(); _sql.Close() }()

// setup context
gin.SetMode(gin.TestMode)
context, _ := gin.CreateTestContext(nil)
context.Set(key, want)

// run test
got := FromContext(context)

if got != want {
t.Errorf("FromContext is %v, want %v", got, want)
ctx, _ := gin.CreateTestContext(nil)
ctx.Set(key, _postgres)

typeCtx, _ := gin.CreateTestContext(nil)
typeCtx.Set(key, nil)

nilCtx, _ := gin.CreateTestContext(nil)
nilCtx.Set(key, nil)

// setup tests
tests := []struct {
name string
context *gin.Context
want Interface
}{
{
name: "success",
context: ctx,
want: _postgres,
},
{
name: "failure with nil",
context: nilCtx,
want: nil,
},
{
name: "failure with wrong type",
context: typeCtx,
want: nil,
},
}
}

func TestDatabase_FromContext_Bad(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
context, _ := gin.CreateTestContext(nil)
context.Set(key, nil)

// run test
got := FromContext(context)

if got != nil {
t.Errorf("FromContext is %v, want nil", got)
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := FromContext(test.context)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("FromContext for %s is %v, want %v", test.name, got, test.want)
}
})
}
}

func TestDatabase_FromContext_WrongType(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
func TestDatabase_ToContext(t *testing.T) {
context, _ := gin.CreateTestContext(nil)
context.Set(key, 1)

// run test
got := FromContext(context)

if got != nil {
t.Errorf("FromContext is %v, want nil", got)
_postgres, _ := testPostgres(t)
defer _postgres.Close()

_sqlite := testSqlite(t)
defer _sqlite.Close()

// setup tests
tests := []struct {
name string
database *engine
want *engine
}{
{
name: "success with postgres",
database: _postgres,
want: _postgres,
},
{
name: "success with sqlite3",
database: _sqlite,
want: _sqlite,
},
}
}

func TestDatabase_FromContext_Empty(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
context, _ := gin.CreateTestContext(nil)

// run test
got := FromContext(context)
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ToContext(context, test.want)

if got != nil {
t.Errorf("FromContext is %v, want nil", got)
got := context.Value(key)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("ToContext for %s is %v, want %v", test.name, got, test.want)
}
})
}
}

func TestDatabase_ToContext(t *testing.T) {
// setup types
want, _ := sqlite.NewTest()
func TestDatabase_FromCLIContext(t *testing.T) {
flags := flag.NewFlagSet("test", 0)
flags.String("database.driver", "sqlite3", "doc")
flags.String("database.addr", "file::memory:?cache=shared", "doc")
flags.Int("database.compression.level", 3, "doc")
flags.Duration("database.connection.life", 10*time.Second, "doc")
flags.Int("database.connection.idle", 5, "doc")
flags.Int("database.connection.open", 20, "doc")
flags.String("database.encryption.key", "A1B2C3D4E5G6H7I8J9K0LMNOPQRSTUVW", "doc")
flags.Bool("database.skip_creation", true, "doc")

// setup tests
tests := []struct {
name string
failure bool
context *cli.Context
}{
{
name: "success",
failure: false,
context: cli.NewContext(&cli.App{Name: "vela"}, flags, nil),
},
{
name: "failure",
failure: true,
context: cli.NewContext(&cli.App{Name: "vela"}, flag.NewFlagSet("test", 0), nil),
},
}

defer func() { _sql, _ := want.Sqlite.DB(); _sql.Close() }()
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := FromCLIContext(test.context)

// setup context
gin.SetMode(gin.TestMode)
context, _ := gin.CreateTestContext(nil)
ToContext(context, want)
if test.failure {
if err == nil {
t.Errorf("FromCLIContext for %s should have returned err", test.name)
}

// run test
got := context.Value(key)
return
}

if got != want {
t.Errorf("ToContext is %v, want %v", got, want)
if err != nil {
t.Errorf("FromCLIContext for %s returned err: %v", test.name, err)
}
})
}
}
Loading

0 comments on commit 470791d

Please sign in to comment.