3
0

Tweak store logic and tests to support MSSQL

This commit is contained in:
Tomaž Jerman 2022-12-08 14:20:12 +01:00 committed by Jože Fortun
parent 27d03d2c99
commit 890c431156
13 changed files with 53 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import (
"database/sql"
"time"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
sqlx "github.com/jmoiron/sqlx/types"
@ -48,11 +49,15 @@ func (s Store) dailyMetrics(ctx context.Context, tbl exp.IdentifierExpression, e
)
return rval, func() (err error) {
daily := s.Functions.DATE(goqu.C(field))
daily, err := s.Dialect.AttributeCast(&dal.Attribute{Type: dal.TypeDate{}}, goqu.C(field))
if err != nil {
return
}
query := s.Dialect.GOQU().
Select(daily, goqu.COUNT(goqu.Star()).As("value")).
From(tbl).
Where(goqu.C(field).IsNotNull(), goqu.And(expr...)).
Where(exp.NewLiteralExpression("? IS NOT NULL", goqu.C(field)), goqu.And(expr...)).
GroupBy(daily).
Order(exp.NewOrderedExpression(daily, exp.AscDir, exp.NoNullsSortType))
@ -92,7 +97,7 @@ func timestampStatExpr(fields ...string) []interface{} {
sum = func(field string) goqu.Expression {
return goqu.COALESCE(
goqu.SUM(goqu.Case().When(goqu.C(field+"_at").IsNotNull(), lit1).Else(lit0)),
goqu.SUM(goqu.Case().When(exp.NewLiteralExpression("? IS NOT NULL", goqu.C(field+"_at")), lit1).Else(lit0)),
lit0,
).As(field)
}
@ -100,7 +105,7 @@ func timestampStatExpr(fields ...string) []interface{} {
for _, field := range fields {
ee = append(ee, sum(field))
valid = append(valid, goqu.C(field+"_at").IsNull())
valid = append(valid, exp.NewLiteralExpression("? IS NULL", goqu.C(field+"_at")))
}
return append(ee, goqu.SUM(goqu.Case().When(goqu.And(valid...), lit1).Else(lit0)).As("valid"))

View File

@ -320,12 +320,12 @@ func DefaultFilters() (f *extendedFilters) {
switch f.SubWorkflow {
case filter.StateExcluded:
ee = append(ee, goqu.Or(
exp.NewBooleanExpression(exp.EqOp, expr, false),
exp.NewBooleanExpression(exp.EqOp, expr, string(s.Dialect.DialectOptions().False)),
exp.NewLiteralExpression("? IS NULL", expr),
))
case filter.StateExclusive:
ee = append(ee,
exp.NewBooleanExpression(exp.EqOp, expr, true),
exp.NewBooleanExpression(exp.EqOp, expr, string(s.Dialect.DialectOptions().True)),
)
}
}

View File

@ -15,12 +15,14 @@ func testApigwRoutes(t *testing.T, s store.ApigwRoutes) {
var (
ctx = context.Background()
new = &types.ApigwRoute{
CreatedAt: *now(),
ID: 42,
Endpoint: "/foo",
Enabled: true,
CreatedBy: 1}
disabled = &types.ApigwRoute{
CreatedAt: *now(),
ID: 4242,
Endpoint: "/foo_disabled",
Enabled: false,

View File

@ -2,14 +2,15 @@ package tests
import (
"context"
"testing"
"time"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/rand"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func testAuthClients(t *testing.T, s store.AuthClients) {
@ -36,6 +37,7 @@ func testAuthClients(t *testing.T, s store.AuthClients) {
)
t.Run("create", func(t *testing.T) {
req.NoError(s.TruncateAuthClients(ctx))
authClient := makeNew("AuthClientCRUD")
req.NoError(s.CreateAuthClient(ctx, authClient))
})

View File

@ -2,13 +2,14 @@ package tests
import (
"context"
"testing"
"time"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/rand"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func testAuthOa2tokens(t *testing.T, s store.AuthOa2tokens) {
@ -21,6 +22,7 @@ func testAuthOa2tokens(t *testing.T, s store.AuthOa2tokens) {
return &types.AuthOa2token{
ID: id.Next(),
CreatedAt: time.Now(),
ExpiresAt: time.Now(),
Access: string(rand.Bytes(5)),
Code: string(rand.Bytes(5)),
Refresh: string(rand.Bytes(5)),

View File

@ -2,12 +2,13 @@ package tests
import (
"context"
"testing"
"time"
"github.com/cortezaproject/corteza/server/pkg/rand"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func testAuthSessions(t *testing.T, s store.AuthSessions) {
@ -20,6 +21,7 @@ func testAuthSessions(t *testing.T, s store.AuthSessions) {
return &types.AuthSession{
ID: handle,
CreatedAt: time.Now(),
ExpiresAt: time.Now(),
Data: []byte("..."),
}
}

View File

@ -2,14 +2,15 @@ package tests
import (
"context"
"testing"
"time"
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/rand"
"github.com/cortezaproject/corteza/server/store"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func testComposeNamespaces(t *testing.T, s store.ComposeNamespaces) {
@ -37,6 +38,7 @@ func testComposeNamespaces(t *testing.T, s store.ComposeNamespaces) {
)
t.Run("create", func(t *testing.T) {
req.NoError(s.TruncateComposeNamespaces(ctx))
composeNamespace := makeNew("ComposeNamespaceCRUD", "compose-namespace-crud")
req.NoError(s.CreateComposeNamespace(ctx, composeNamespace))
})

View File

@ -37,6 +37,7 @@ func testDalConnections(t *testing.T, s store.DalConnections) {
)
t.Run("create", func(t *testing.T) {
req.NoError(s.TruncateDalConnections(ctx))
dalConnection := makeNew("DalConnectionCRUD")
req.NoError(s.CreateDalConnection(ctx, dalConnection))
})

View File

@ -20,9 +20,10 @@ func testDataPrivacyRequests(t *testing.T, s store.DataPrivacyRequests) {
makeNew = func(handle string) *types.DataPrivacyRequest {
// minimum data set for new dataPrivacyRequest
return &types.DataPrivacyRequest{
ID: id.Next(),
CreatedAt: time.Now(),
Kind: types.RequestKindCorrect,
ID: id.Next(),
CreatedAt: time.Now(),
RequestedAt: time.Now(),
Kind: types.RequestKindCorrect,
}
}

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/mssql"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/mysql"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/postgres"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/sqlite"
@ -34,6 +35,18 @@ func Test_RDBMS_MYSQL(t *testing.T) {
testAllGenerated(t, setup(t, mysql.Connect))
}
func Test_RDBMS_SQLSERVER_2022(t *testing.T) {
testAllGenerated(t, setup(t, mssql.Connect))
}
func Test_RDBMS_SQLSERVER_2019(t *testing.T) {
testAllGenerated(t, setup(t, mssql.Connect))
}
func Test_RDBMS_SQLSERVER_2017(t *testing.T) {
testAllGenerated(t, setup(t, mssql.Connect))
}
func Test_RDBMS_PGSQL(t *testing.T) {
testAllGenerated(t, setup(t, postgres.Connect))
}

View File

@ -38,13 +38,13 @@ func testQueues(t *testing.T, s store.Queues) {
t.Run("upsert", func(t *testing.T) {
req := require.New(t)
req.NoError(s.TruncateQueues(ctx))
req.NoError(s.UpsertQueue(ctx, &types.Queue{ID: 42, Queue: "test"}))
req.NoError(s.UpsertQueue(ctx, &types.Queue{ID: 42, Queue: "test", CreatedAt: *now()}))
set, _, err := s.SearchQueues(ctx, types.QueueFilter{})
req.NoError(err)
req.Len(set, 1)
req.True(set[0].Queue == "test")
req.NoError(s.UpsertQueue(ctx, &types.Queue{ID: 42, Queue: "foobar"}))
req.NoError(s.UpsertQueue(ctx, &types.Queue{ID: 42, Queue: "foobar", CreatedAt: *now()}))
set, _, err = s.SearchQueues(ctx, types.QueueFilter{})
req.NoError(err)
req.Len(set, 1)

View File

@ -77,7 +77,7 @@ func testSettingValues(t *testing.T, s store.SettingValues) {
t.Run("new", func(t *testing.T) {
req := require.New(t)
req.NoError(s.UpsertSettingValue(ctx, &types.SettingValue{Name: "foo", Value: []byte(`"foo"`)}))
req.NoError(s.UpsertSettingValue(ctx, &types.SettingValue{Name: "foo", Value: []byte(`"foo"`), UpdatedAt: *now()}))
v, err := s.LookupSettingValueByNameOwnedBy(ctx, "foo", 0)
req.NoError(err)
req.NotNil(v)
@ -85,8 +85,8 @@ func testSettingValues(t *testing.T, s store.SettingValues) {
})
t.Run("existing", func(t *testing.T) {
req.NoError(s.CreateSettingValue(ctx, &types.SettingValue{Name: "baz", Value: []byte(`"created"`)}))
req.NoError(s.UpsertSettingValue(ctx, &types.SettingValue{Name: "baz", Value: []byte(`"updated"`)}))
req.NoError(s.CreateSettingValue(ctx, &types.SettingValue{Name: "baz", Value: []byte(`"created"`), UpdatedAt: *now()}))
req.NoError(s.UpsertSettingValue(ctx, &types.SettingValue{Name: "baz", Value: []byte(`"updated"`), UpdatedAt: *now()}))
v, err := s.LookupSettingValueByNameOwnedBy(ctx, "baz", 0)
req.NoError(err)
req.NotNil(v)

View File

@ -38,6 +38,7 @@ func testTemplates(t *testing.T, s store.Templates) {
)
t.Run("create", func(t *testing.T) {
req.NoError(s.TruncateTemplates(ctx))
template := makeNew("TemplateCRUD")
req.NoError(s.CreateTemplate(ctx, template))
})