Remove operations from models and connections
For now, operations will be defined on a DB driver.
This commit is contained in:
+2
-2
@@ -3,13 +3,14 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/id"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
"github.com/cortezaproject/corteza-server/system/service"
|
||||
"github.com/cortezaproject/corteza-server/system/types"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (app *CortezaApp) initDAL(ctx context.Context, log *zap.Logger) (err error) {
|
||||
@@ -74,7 +75,6 @@ func provisionPrimaryDalConnection(ctx context.Context, s store.DalConnections)
|
||||
Config: types.ConnectionConfig{
|
||||
DAL: &types.ConnectionConfigDAL{
|
||||
ModelIdent: "compose_record",
|
||||
Operations: dal.FullOperations(),
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
@@ -248,15 +248,15 @@ func recToGetters(rr ...*types.Record) (out []dal.ValueGetter) {
|
||||
}
|
||||
|
||||
func recCreateOperations(m *types.Module) (out dal.OperationSet) {
|
||||
return dal.CreateOperations(m.Config.DAL.Operations...)
|
||||
return dal.CreateOperations()
|
||||
}
|
||||
|
||||
func recUpdateOperations(m *types.Module) (out dal.OperationSet) {
|
||||
return dal.UpdateOperations(m.Config.DAL.Operations...)
|
||||
return dal.UpdateOperations()
|
||||
}
|
||||
|
||||
func recDeleteOperations(m *types.Module) (out dal.OperationSet) {
|
||||
return dal.DeleteOperations(m.Config.DAL.Operations...)
|
||||
return dal.DeleteOperations()
|
||||
}
|
||||
|
||||
func recFilterOperations(f types.RecordFilter) (out dal.OperationSet) {
|
||||
@@ -280,10 +280,10 @@ func recFilterOperations(f types.RecordFilter) (out dal.OperationSet) {
|
||||
}
|
||||
|
||||
func recSearchOperations(m *types.Module, f types.RecordFilter) (out dal.OperationSet) {
|
||||
return dal.SearchOperations(m.Config.DAL.Operations...).
|
||||
return dal.SearchOperations().
|
||||
Union(recFilterOperations(f))
|
||||
}
|
||||
|
||||
func recLookupOperations(m *types.Module) (out dal.OperationSet) {
|
||||
return dal.LookupOperations(m.Config.DAL.Operations...)
|
||||
return dal.LookupOperations()
|
||||
}
|
||||
|
||||
@@ -1298,7 +1298,6 @@ func ModuleToModel(ns *types.Namespace, mod *types.Module, inhIdent string) (mod
|
||||
ResourceID: mod.ID,
|
||||
ResourceType: types.ModuleResourceType,
|
||||
SensitivityLevelID: mod.Config.Privacy.SensitivityLevelID,
|
||||
Operations: mod.Config.DAL.Operations,
|
||||
}
|
||||
|
||||
if model.Ident = mod.Config.DAL.Ident; model.Ident == "" {
|
||||
|
||||
@@ -82,8 +82,7 @@ type (
|
||||
}
|
||||
|
||||
ModuleConfigDAL struct {
|
||||
ConnectionID uint64 `json:"connectionID,string"`
|
||||
Operations dal.OperationSet `json:"operations"`
|
||||
ConnectionID uint64 `json:"connectionID,string"`
|
||||
|
||||
Constraints map[string][]any `json:"constraints"`
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ type (
|
||||
connection Connection
|
||||
params ConnectionParams
|
||||
Config ConnectionConfig
|
||||
operations OperationSet
|
||||
}
|
||||
|
||||
ConnectionConfig struct {
|
||||
|
||||
+3
-3
@@ -100,7 +100,7 @@ type (
|
||||
SetValue(string, uint, any) error
|
||||
}
|
||||
|
||||
ConnectorFn func(ctx context.Context, dsn string, oo ...Operation) (Connection, error)
|
||||
ConnectorFn func(ctx context.Context, dsn string) (Connection, error)
|
||||
|
||||
DriverConnectionParam struct {
|
||||
Key string `json:"key"`
|
||||
@@ -156,7 +156,7 @@ func RegisterDriver(d Driver) {
|
||||
}
|
||||
|
||||
// connect opens a new StoreConnection for the given CRS
|
||||
func connect(ctx context.Context, log *zap.Logger, isDevelopment bool, cp ConnectionParams, operations ...Operation) (Connection, error) {
|
||||
func connect(ctx context.Context, log *zap.Logger, isDevelopment bool, cp ConnectionParams) (Connection, error) {
|
||||
if cp.Type != "corteza::dal:connection:dsn" {
|
||||
return nil, fmt.Errorf("cannot open connection: only DSN connections supported (got: %q)", cp.Type)
|
||||
}
|
||||
@@ -180,7 +180,7 @@ func connect(ctx context.Context, log *zap.Logger, isDevelopment bool, cp Connec
|
||||
}
|
||||
|
||||
if conn, ok := registeredConnectors[storeType]; ok {
|
||||
return conn(ctx, dsn, operations...)
|
||||
return conn(ctx, dsn)
|
||||
} else {
|
||||
return nil, fmt.Errorf("unknown store type used: %q (check your database configuration)", storeType)
|
||||
}
|
||||
|
||||
@@ -39,8 +39,6 @@ type (
|
||||
|
||||
Attributes AttributeSet
|
||||
|
||||
Operations OperationSet
|
||||
|
||||
Constraints map[string][]any
|
||||
Indexes IndexSet
|
||||
}
|
||||
|
||||
+7
-14
@@ -233,14 +233,13 @@ func (svc *service) RemoveSensitivityLevel(levelIDs ...uint64) (err error) {
|
||||
// Connection management
|
||||
|
||||
// MakeConnection makes and returns a new connection (wrap)
|
||||
func MakeConnection(ID uint64, conn Connection, p ConnectionParams, c ConnectionConfig, oo ...Operation) *ConnectionWrap {
|
||||
func MakeConnection(ID uint64, conn Connection, p ConnectionParams, c ConnectionConfig) *ConnectionWrap {
|
||||
return &ConnectionWrap{
|
||||
ID: ID,
|
||||
connection: conn,
|
||||
|
||||
params: p,
|
||||
Config: c,
|
||||
operations: oo,
|
||||
params: p,
|
||||
Config: c,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,12 +294,6 @@ func (svc *service) ReplaceConnection(ctx context.Context, conn *ConnectionWrap,
|
||||
for _, model := range svc.models[ID] {
|
||||
log.Debug("validating model before connection is updated", zap.String("ident", model.Ident))
|
||||
|
||||
// - operations
|
||||
if !model.Operations.IsSubset(conn.operations...) {
|
||||
issues.addConnectionIssue(ID, fmt.Errorf("cannot update connection %d: new connection does not support existing models", ID))
|
||||
errored = true
|
||||
}
|
||||
|
||||
// - sensitivity levels
|
||||
if !svc.sensitivityLevels.isSubset(model.SensitivityLevelID, conn.Config.SensitivityLevelID) {
|
||||
issues.addConnectionIssue(ID, fmt.Errorf("cannot update connection %d: new connection sensitivity level does not support model %d", ID, model.ResourceID))
|
||||
@@ -328,7 +321,7 @@ func (svc *service) ReplaceConnection(ctx context.Context, conn *ConnectionWrap,
|
||||
}
|
||||
|
||||
if conn.connection == nil {
|
||||
conn.connection, err = connect(ctx, svc.logger, svc.inDev, conn.params, conn.operations...)
|
||||
conn.connection, err = connect(ctx, svc.logger, svc.inDev, conn.params)
|
||||
if err != nil {
|
||||
log.Warn("could not connect", zap.Error(err))
|
||||
issues.addConnectionIssue(ID, err)
|
||||
@@ -995,7 +988,7 @@ func (svc *service) GetConnectionByID(connectionID uint64) (cw *ConnectionWrap)
|
||||
return svc.connections[connectionID]
|
||||
}
|
||||
|
||||
func (svc *service) getConnection(connectionID uint64, cc ...Operation) (cw *ConnectionWrap, can OperationSet, err error) {
|
||||
func (svc *service) getConnection(connectionID uint64, oo ...Operation) (cw *ConnectionWrap, can OperationSet, err error) {
|
||||
err = func() error {
|
||||
// get the requested connection
|
||||
cw = svc.GetConnectionByID(connectionID)
|
||||
@@ -1004,8 +997,8 @@ func (svc *service) getConnection(connectionID uint64, cc ...Operation) (cw *Con
|
||||
}
|
||||
|
||||
// check if connection supports requested operations
|
||||
if !cw.connection.Can(cc...) {
|
||||
return fmt.Errorf("connection %d does not support requested operations %v", connectionID, OperationSet(cc).Diff(cw.connection.Operations()))
|
||||
if !cw.connection.Can(oo...) {
|
||||
return fmt.Errorf("connection %d does not support requested operations %v", connectionID, OperationSet(oo).Diff(cw.connection.Operations()))
|
||||
}
|
||||
can = cw.connection.Operations()
|
||||
return nil
|
||||
|
||||
@@ -30,10 +30,5 @@ func Model() *dal.Model {
|
||||
&dal.Attribute{Ident: "delta", Store: &dal.CodecPlain{}, Type: &dal.TypeJSON{}},
|
||||
&dal.Attribute{Ident: "comment", Store: &dal.CodecPlain{}, Type: &dal.TypeText{}},
|
||||
},
|
||||
|
||||
Operations: dal.OperationSet{
|
||||
dal.Create,
|
||||
dal.Search,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,10 @@ package dal
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/ddl"
|
||||
"sync"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
@@ -18,9 +19,9 @@ type (
|
||||
//
|
||||
// In other words: this allows Corteza to read Records from the supported SQL databases
|
||||
connection struct {
|
||||
mux sync.RWMutex
|
||||
models map[string]*model
|
||||
operations dal.OperationSet
|
||||
mux sync.RWMutex
|
||||
models map[string]*model
|
||||
driver dal.Driver
|
||||
|
||||
db sqlx.ExtContext
|
||||
dialect drivers.Dialect
|
||||
@@ -29,21 +30,26 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
dalDriver dal.Driver
|
||||
)
|
||||
|
||||
func init() {
|
||||
dal.RegisterDriver(dal.Driver{
|
||||
dalDriver = dal.Driver{
|
||||
Type: "corteza::dal:driver:rdbms",
|
||||
Operations: dal.FullOperations(),
|
||||
Connection: dal.NewDSNDriverConnectionConfig(),
|
||||
})
|
||||
}
|
||||
dal.RegisterDriver(dalDriver)
|
||||
}
|
||||
|
||||
func Connection(db sqlx.ExtContext, dialect drivers.Dialect, dd ddl.DataDefiner, cc ...dal.Operation) *connection {
|
||||
func Connection(db sqlx.ExtContext, dialect drivers.Dialect, dd ddl.DataDefiner) *connection {
|
||||
return &connection{
|
||||
db: db,
|
||||
dialect: dialect,
|
||||
dataDefiner: dd,
|
||||
models: make(map[string]*model),
|
||||
operations: cc,
|
||||
driver: dalDriver,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,11 +74,11 @@ func (c *connection) withModel(m *dal.Model, fn func(m *model) error) error {
|
||||
}
|
||||
|
||||
func (c *connection) Operations() dal.OperationSet {
|
||||
return c.operations
|
||||
return c.driver.Operations
|
||||
}
|
||||
|
||||
func (c *connection) Can(operations ...dal.Operation) bool {
|
||||
return c.operations.IsSuperset(operations...)
|
||||
return c.Operations().IsSuperset(operations...)
|
||||
}
|
||||
|
||||
func (c *connection) Create(ctx context.Context, m *dal.Model, rr ...dal.ValueGetter) (err error) {
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/dal"
|
||||
"strings"
|
||||
|
||||
pkgdal "github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/dal"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
@@ -45,7 +45,7 @@ func Connect(ctx context.Context, dsn string) (_ store.Storer, err error) {
|
||||
s := &rdbms.Store{
|
||||
DB: db,
|
||||
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), pkgdal.FullOperations()...),
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)),
|
||||
|
||||
Dialect: goquDialectWrapper,
|
||||
TxRetryErrHandler: txRetryErrHandler,
|
||||
|
||||
@@ -11,7 +11,7 @@ func init() {
|
||||
dal.RegisterConnector(dalConnector, SCHEMA, debugSchema)
|
||||
}
|
||||
|
||||
func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.Connection, err error) {
|
||||
func dalConnector(ctx context.Context, dsn string) (_ dal.Connection, err error) {
|
||||
cfg, err := NewConfig(dsn)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -27,5 +27,5 @@ func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.C
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), cc...), nil
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)), nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
pkgdal "github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/dal"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
@@ -50,7 +49,7 @@ func Connect(ctx context.Context, dsn string) (_ store.Storer, err error) {
|
||||
s := &rdbms.Store{
|
||||
DB: db,
|
||||
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), pkgdal.FullOperations()...),
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)),
|
||||
|
||||
Dialect: goquDialectWrapper,
|
||||
ErrorHandler: errorHandler,
|
||||
|
||||
@@ -14,7 +14,7 @@ func init() {
|
||||
dal.RegisterConnector(dalConnector, SCHEMA, debugSchema)
|
||||
}
|
||||
|
||||
func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.Connection, err error) {
|
||||
func dalConnector(ctx context.Context, dsn string) (_ dal.Connection, err error) {
|
||||
var (
|
||||
db *sqlx.DB
|
||||
cfg *rdbms.ConnConfig
|
||||
@@ -33,5 +33,5 @@ func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.C
|
||||
return
|
||||
}
|
||||
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), cc...), nil
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)), nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
pkgdal "github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/store/adapters/rdbms/dal"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
@@ -70,7 +69,7 @@ func Connect(ctx context.Context, dsn string) (_ store.Storer, err error) {
|
||||
s := &rdbms.Store{
|
||||
DB: db,
|
||||
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), pkgdal.FullOperations()...),
|
||||
DAL: dal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)),
|
||||
|
||||
Dialect: goquDialectWrapper,
|
||||
ErrorHandler: errorHandler,
|
||||
|
||||
@@ -14,7 +14,7 @@ func init() {
|
||||
dal.RegisterConnector(dalConnector, SCHEMA, altSchema, debugSchema)
|
||||
}
|
||||
|
||||
func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.Connection, err error) {
|
||||
func dalConnector(ctx context.Context, dsn string) (_ dal.Connection, err error) {
|
||||
var (
|
||||
db *sqlx.DB
|
||||
cfg *rdbms.ConnConfig
|
||||
@@ -33,5 +33,5 @@ func dalConnector(ctx context.Context, dsn string, cc ...dal.Operation) (_ dal.C
|
||||
return
|
||||
}
|
||||
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db), cc...), nil
|
||||
return rdbmsdal.Connection(db, Dialect(), DataDefiner(cfg.DBName, db)), nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
||||
@@ -365,14 +365,12 @@ func MakeDalConnection(c *types.DalConnection, existing dal.Connection) (cw *dal
|
||||
SensitivityLevelID: c.Config.Privacy.SensitivityLevelID,
|
||||
Label: c.Handle,
|
||||
}
|
||||
connParams dal.ConnectionParams
|
||||
connOperations dal.OperationSet
|
||||
connParams dal.ConnectionParams
|
||||
)
|
||||
|
||||
if c.Config.DAL != nil {
|
||||
connConfig.ModelIdent = c.Config.DAL.ModelIdent
|
||||
|
||||
connOperations = c.Config.DAL.Operations
|
||||
connParams = dal.ConnectionParams{
|
||||
Type: c.Config.DAL.Type,
|
||||
Params: c.Config.DAL.Params,
|
||||
@@ -393,7 +391,6 @@ func MakeDalConnection(c *types.DalConnection, existing dal.Connection) (cw *dal
|
||||
existing,
|
||||
connParams,
|
||||
connConfig,
|
||||
connOperations...,
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/geolocation"
|
||||
"github.com/cortezaproject/corteza-server/pkg/sql"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/dal"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
)
|
||||
|
||||
@@ -86,10 +85,6 @@ type (
|
||||
// parameters for th connection
|
||||
Params map[string]any `json:"params"`
|
||||
|
||||
// @note operations, for now, will only be available on connections
|
||||
// with a fallback on modules
|
||||
Operations dal.OperationSet `json:"operations"`
|
||||
|
||||
// ident to be used when generating models from modules using this connection
|
||||
// it can use {{module}} and {{namespace}} as placeholders
|
||||
ModelIdent string `json:"modelIdent"`
|
||||
|
||||
Reference in New Issue
Block a user