From bb4f27cfb5e8a6f07b98018511c19349320d9c47 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 25 Aug 2023 16:47:41 +0300 Subject: [PATCH] updated automigrate template test --- plugins/migratecmd/migratecmd_test.go | 27 +++++++++++--- plugins/migratecmd/templates.go | 52 +++++++++++++-------------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/plugins/migratecmd/migratecmd_test.go b/plugins/migratecmd/migratecmd_test.go index 853f849b..7574447d 100644 --- a/plugins/migratecmd/migratecmd_test.go +++ b/plugins/migratecmd/migratecmd_test.go @@ -382,7 +382,9 @@ migrate((db) => { collection.name = "test456_update" collection.type = "base" collection.listRule = "@request.auth.id != ''" - collection.deleteRule = "updated > 0 && @request.auth.id != ''" + collection.createRule = "id = \"nil_update\"" + collection.updateRule = "id = \"2_update\"" + collection.deleteRule = null collection.options = {} collection.indexes = [ "create index test1 on test456_update (f1_name)" @@ -428,7 +430,9 @@ migrate((db) => { collection.name = "test456" collection.type = "auth" collection.listRule = "@request.auth.id != '' && created > 0" - collection.deleteRule = null + collection.createRule = null + collection.updateRule = "id = \"2\"" + collection.deleteRule = "id = \"3\"" collection.options = { "allowEmailAuth": false, "allowOAuth2Auth": false, @@ -505,7 +509,11 @@ func init() { collection.ListRule = types.Pointer("@request.auth.id != ''") - collection.DeleteRule = types.Pointer("updated > 0 && @request.auth.id != ''") + collection.CreateRule = types.Pointer("id = \"nil_update\"") + + collection.UpdateRule = types.Pointer("id = \"2_update\"") + + collection.DeleteRule = nil options := map[string]any{} json.Unmarshal([]byte(` + "`" + `{}` + "`" + `), &options) @@ -566,7 +574,11 @@ func init() { collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0") - collection.DeleteRule = nil + collection.CreateRule = nil + + collection.UpdateRule = types.Pointer("id = \"2\"") + + collection.DeleteRule = types.Pointer("id = \"3\"") options := map[string]any{} json.Unmarshal([]byte(` + "`" + `{ @@ -645,6 +657,9 @@ func init() { collection.Updated = collection.Created collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0") collection.ViewRule = types.Pointer(`id = "1"`) + collection.UpdateRule = types.Pointer(`id = "2"`) + collection.CreateRule = nil + collection.DeleteRule = types.Pointer(`id = "3"`) collection.Indexes = types.JsonArray[string]{"create index test1 on test456 (f1_name)"} collection.SetOptions(models.CollectionAuthOptions{ ManageRule: types.Pointer("created > 0"), @@ -684,6 +699,10 @@ func init() { collection.Type = models.CollectionTypeBase collection.DeleteRule = types.Pointer(`updated > 0 && @request.auth.id != ''`) collection.ListRule = types.Pointer("@request.auth.id != ''") + collection.ViewRule = types.Pointer(`id = "1"`) // no change + collection.UpdateRule = types.Pointer(`id = "2_update"`) + collection.CreateRule = types.Pointer(`id = "nil_update"`) + collection.DeleteRule = nil collection.Indexes = types.JsonArray[string]{ "create index test1 on test456_update (f1_name)", } diff --git a/plugins/migratecmd/templates.go b/plugins/migratecmd/templates.go index 6c979fe8..c21664e6 100644 --- a/plugins/migratecmd/templates.go +++ b/plugins/migratecmd/templates.go @@ -137,17 +137,17 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) // note: strconv.Quote is used because %q converts the rule operators in unicode char codes // --- - formatRule := func(typ string, rule *string) string { + formatRule := func(prop string, rule *string) string { if rule == nil { - return fmt.Sprintf("%s.%sRule = null", varName, typ) + return fmt.Sprintf("%s.%s = null", varName, prop) } - return fmt.Sprintf("%s.%sRule = %s", varName, typ, strconv.Quote(*rule)) + return fmt.Sprintf("%s.%s = %s", varName, prop, strconv.Quote(*rule)) } if old.ListRule != new.ListRule { - oldRule := formatRule("list", old.ListRule) - newRule := formatRule("list", new.ListRule) + oldRule := formatRule("listRule", old.ListRule) + newRule := formatRule("listRule", new.ListRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -156,8 +156,8 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) } if old.ViewRule != new.ViewRule { - oldRule := formatRule("view", old.ViewRule) - newRule := formatRule("view", new.ViewRule) + oldRule := formatRule("viewRule", old.ViewRule) + newRule := formatRule("viewRule", new.ViewRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -166,8 +166,8 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) } if old.CreateRule != new.CreateRule { - oldRule := formatRule("create", old.CreateRule) - newRule := formatRule("create", new.CreateRule) + oldRule := formatRule("createRule", old.CreateRule) + newRule := formatRule("createRule", new.CreateRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -176,8 +176,8 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) } if old.UpdateRule != new.UpdateRule { - oldRule := formatRule("update", old.UpdateRule) - newRule := formatRule("update", new.UpdateRule) + oldRule := formatRule("updateRule", old.UpdateRule) + newRule := formatRule("updateRule", new.UpdateRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -186,8 +186,8 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) } if old.DeleteRule != new.DeleteRule { - oldRule := formatRule("delete", old.DeleteRule) - newRule := formatRule("delete", new.DeleteRule) + oldRule := formatRule("deleteRule", old.DeleteRule) + newRule := formatRule("deleteRule", new.DeleteRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -529,17 +529,17 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) // note: strconv.Quote is used because %q converts the rule operators in unicode char codes // --- - formatRule := func(typ string, rule *string) string { + formatRule := func(prop string, rule *string) string { if rule == nil { - return fmt.Sprintf("%s.%sRule = nil\n", varName, typ) + return fmt.Sprintf("%s.%s = nil\n", varName, prop) } - return fmt.Sprintf("%s.%sRule = types.Pointer(%s)\n", varName, typ, strconv.Quote(*rule)) + return fmt.Sprintf("%s.%s = types.Pointer(%s)\n", varName, prop, strconv.Quote(*rule)) } if old.ListRule != new.ListRule { - oldRule := formatRule("List", old.ListRule) - newRule := formatRule("List", new.ListRule) + oldRule := formatRule("ListRule", old.ListRule) + newRule := formatRule("ListRule", new.ListRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -548,8 +548,8 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) } if old.ViewRule != new.ViewRule { - oldRule := formatRule("View", old.ViewRule) - newRule := formatRule("View", new.ViewRule) + oldRule := formatRule("ViewRule", old.ViewRule) + newRule := formatRule("ViewRule", new.ViewRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -558,8 +558,8 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) } if old.CreateRule != new.CreateRule { - oldRule := formatRule("Create", old.CreateRule) - newRule := formatRule("Create", new.CreateRule) + oldRule := formatRule("CreateRule", old.CreateRule) + newRule := formatRule("CreateRule", new.CreateRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -568,8 +568,8 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) } if old.UpdateRule != new.UpdateRule { - oldRule := formatRule("Update", old.UpdateRule) - newRule := formatRule("Update", new.UpdateRule) + oldRule := formatRule("UpdateRule", old.UpdateRule) + newRule := formatRule("UpdateRule", new.UpdateRule) if oldRule != newRule { upParts = append(upParts, newRule) @@ -578,8 +578,8 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) } if old.DeleteRule != new.DeleteRule { - oldRule := formatRule("Delete", old.DeleteRule) - newRule := formatRule("Delete", new.DeleteRule) + oldRule := formatRule("DeleteRule", old.DeleteRule) + newRule := formatRule("DeleteRule", new.DeleteRule) if oldRule != newRule { upParts = append(upParts, newRule)