Skip to content

Commit

Permalink
- remove "schemas" from model
Browse files Browse the repository at this point in the history
- add more tests
- support xregistry-json schema (w/o "/0.5")

Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
duglin committed Dec 24, 2024
1 parent 8149d3d commit c1ba48c
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 681 deletions.
17 changes: 13 additions & 4 deletions registry/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var AllowableFlags = ArrayToLower([]string{
var AllowableMutable = ArrayToLower([]string{
"capabilities", "entities", "model"})

var AllowableSchemas = ArrayToLower([]string{"xRegistry-json"})
var AllowableSchemas = ArrayToLower([]string{XREGSCHEMA + "/" + SPECVERSION})

var AllowableSpecVersions = ArrayToLower([]string{"0.5"})

Expand Down Expand Up @@ -62,6 +62,14 @@ func CleanArray(arr []string, full []string, text string) ([]string, error) {
// Lowercase evrything and look for "*"
for i, s := range arr {
s = strings.ToLower(s)

// Special case these
if text == "schemas" && s == strings.ToLower(XREGSCHEMA) {
// Allow just "xregistry-json", we'll add the spec version #
s = s + "/" + SPECVERSION
}
// End-of-special

arr[i] = s
if s == "*" {
if len(arr) != 1 {
Expand All @@ -70,6 +78,7 @@ func CleanArray(arr []string, full []string, text string) ([]string, error) {
}
return full, nil
}

}

sort.Strings(arr) // sort 'em
Expand Down Expand Up @@ -99,7 +108,7 @@ func (c *Capabilities) Validate() error {
var err error

if c.Schemas == nil {
c.Schemas = []string{XREGSCHEMA}
c.Schemas = []string{XREGSCHEMA + "/" + SPECVERSION}
}
if c.SpecVersions == nil {
c.SpecVersions = []string{SPECVERSION}
Expand All @@ -126,8 +135,8 @@ func (c *Capabilities) Validate() error {
return err
}

if !ArrayContains(c.Schemas, strings.ToLower(XREGSCHEMA)) {
return fmt.Errorf(`"schemas" must contain %q`, XREGSCHEMA)
if !ArrayContains(c.Schemas, strings.ToLower(XREGSCHEMA+"/"+SPECVERSION)) {
return fmt.Errorf(`"schemas" must contain %q`, XREGSCHEMA+"/"+SPECVERSION)
}
if !ArrayContains(c.SpecVersions, strings.ToLower(SPECVERSION)) {
return fmt.Errorf(`"specversions" must contain %q`, SPECVERSION)
Expand Down
9 changes: 0 additions & 9 deletions registry/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,8 @@ CREATE TRIGGER ModelsTrigger BEFORE DELETE ON Models
FOR EACH ROW
BEGIN
DELETE FROM ModelEntities WHERE RegistrySID=OLD.RegistrySID @
DELETE FROM "Schemas" WHERE RegistrySID=OLD.RegistrySID @
END ;

CREATE TABLE "Schemas" (
RegistrySID VARCHAR(64) NOT NULL,
"Schema" VARCHAR(255) NOT NULL,

PRIMARY KEY(RegistrySID, "Schema"),
INDEX (RegistrySID)
);

CREATE TABLE ModelEntities ( # Group or Resource (no parent=Group)
SID VARCHAR(64), # my System ID
RegistrySID VARCHAR(64),
Expand Down
114 changes: 2 additions & 112 deletions registry/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"maps"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -33,7 +32,6 @@ func IsValidMapKey(key string) bool {

type Model struct {
Registry *Registry `json:"-"`
Schemas []string `json:"schemas,omitempty"`
Attributes Attributes `json:"attributes,omitempty"`
Groups map[string]*GroupModel `json:"groups,omitempty"` // Plural
}
Expand Down Expand Up @@ -228,47 +226,6 @@ func (r *ResourceModel) UnmarshalJSON(data []byte) error {
return Unmarshal(data, (*tmpResourceModel)(r))
}

func (m *Model) AddSchema(schema string) error {
err := Do(m.Registry.tx,
`INSERT INTO "Schemas" (RegistrySID, "Schema") VALUES(?,?)`,
m.Registry.DbSID, schema)
if err != nil {
err = fmt.Errorf("Error inserting schema(%s): %s", schema, err)
log.Print(err)
return err
}

for _, s := range m.Schemas {
if s == schema {
// already there
return nil
}
}

m.Schemas = append(m.Schemas, schema)
sort.Strings(m.Schemas)
return nil
}

func (m *Model) DelSchema(schema string) error {
err := Do(m.Registry.tx,
`DELETE FROM "Schemas" WHERE RegistrySID=? AND "Schema"=?`,
m.Registry.DbSID, schema)
if err != nil {
err = fmt.Errorf("Error deleting schema(%s): %s", schema, err)
log.Print(err)
return err
}

for i, s := range m.Schemas {
if s == schema {
m.Schemas = append(m.Schemas[:i], m.Schemas[i+1:]...)
return nil
}
}
return nil
}

func (m *Model) SetPointers() {
PanicIf(m.Registry == nil, "Model.Registry can't be nil")

Expand Down Expand Up @@ -358,11 +315,6 @@ func (m *Model) VerifyAndSave() error {
}

func (m *Model) Save() error {
err := m.SetSchemas(m.Schemas)
if err != nil {
return err
}

// Create a temporary type so that we don't use the MarshalJSON func
// in model.go. That one will exclude "model" from the serialization and
// we don't want to do that when we're saving it in the DB. We only want
Expand All @@ -371,7 +323,7 @@ func (m *Model) Save() error {
buf, _ := json.Marshal((tmpAttributes)(m.Attributes))
attrs := string(buf)

err = DoZeroOne(m.Registry.tx,
err := DoZeroOne(m.Registry.tx,
`UPDATE Registries SET Attributes=? WHERE SID=?`,
attrs, m.Registry.DbSID)
if err != nil {
Expand All @@ -388,25 +340,6 @@ func (m *Model) Save() error {
return nil
}

func (m *Model) SetSchemas(schemas []string) error {
err := Do(m.Registry.tx,
`DELETE FROM "Schemas" WHERE RegistrySID=?`, m.Registry.DbSID)
if err != nil {
err = fmt.Errorf("Error deleting schemas: %s", err)
log.Print(err)
return err
}
m.Schemas = nil

for _, s := range schemas {
err = m.AddSchema(s)
if err != nil {
return err
}
}
return nil
}

func (m *Model) AddAttr(name, daType string) (*Attribute, error) {
return m.AddAttribute(&Attribute{Name: name, Type: daType})
}
Expand Down Expand Up @@ -685,23 +618,6 @@ func LoadModel(reg *Registry) *Model {
model.Attributes.SetRegistry(reg)
model.Attributes.SetSpecPropsFields("registry")

// Load Schemas
results, err = Query(reg.tx, `
SELECT RegistrySID, "Schema" FROM "Schemas"
WHERE RegistrySID=?
ORDER BY "Schema" ASC`, reg.DbSID)
defer results.Close()

if err != nil {
log.Printf("Error loading schemas(%s): %s", reg.UID, err)
return nil
}

for row := results.NextRow(); row != nil; row = results.NextRow() {
model.Schemas = append(model.Schemas, NotNilString(row[1]))
}
results.Close()

// Load Groups & Resources
results, err = Query(reg.tx, `
SELECT
Expand Down Expand Up @@ -795,20 +711,7 @@ func (m *Model) ApplyNewModel(newM *Model) error {
return err
}

// Delete old Schemas, then add new ones
m.Schemas = []string{XREGSCHEMA + "/" + SPECVERSION}
err := Do(m.Registry.tx,
`DELETE FROM "Schemas" WHERE RegistrySID=?`, m.Registry.DbSID)
if err != nil {
return err
}

for _, schema := range newM.Schemas {
if err = m.AddSchema(schema); err != nil {
return err
}
}

var err error
m.Attributes = newM.Attributes

// Find all old groups that need to be deleted
Expand Down Expand Up @@ -1448,19 +1351,6 @@ func EnsureAttrOK(userAttr *Attribute, specAttr *Attribute) error {
}

func (m *Model) Verify() error {
found := false
for _, s := range m.Schemas {
if strings.EqualFold(s, XREGSCHEMA+"/"+SPECVERSION) {
found = true
break
}
}
if !found {
m.Schemas = append([]string{XREGSCHEMA + "/" + SPECVERSION},
m.Schemas...)
}
sort.Strings(m.Schemas)

// First, make sure we have the xRegistry core/spec defined attributes
// in the list and they're not changed in an inappropriate way.
// This just checks the Registry.Attributes. Groups and Resources will
Expand Down
15 changes: 0 additions & 15 deletions registry/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,10 @@ func TestModelVerifySimple(t *testing.T) {
tests := []Test{
{"empty model", Model{}, ""},
{"empty model - 2", Model{
Schemas: []string{},
Attributes: map[string]*Attribute{},
Groups: map[string]*GroupModel{},
}, ""},

{"empty schemas", Model{
Schemas: []string{},
}, ""},
{"json schema", Model{
Schemas: []string{"jsonSchema"},
}, ""},
{"mulitple schemas", Model{
Schemas: []string{"jsonSchema", "jsonSchema/v1"},
}, ""},
{"schema + empty reg attrs", Model{
Schemas: []string{"xxx"},
Attributes: Attributes{},
}, ""},

{"reg 1 attr - full", Model{
Attributes: Attributes{
"myint": &Attribute{
Expand Down
1 change: 0 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func NewRegistry(tx *Tx, id string, regOpts ...RegOpt) (*Registry, error) {
reg.Capabilities = DefaultCapabilities
reg.Model = &Model{
Registry: reg,
Schemas: []string{XREGSCHEMA + "/" + SPECVERSION},
Groups: map[string]*GroupModel{},
}

Expand Down
Loading

0 comments on commit c1ba48c

Please sign in to comment.