From 95bb2eb871da70803d36afae379280a1ba69c369 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sun, 19 Mar 2023 16:02:29 +0200 Subject: [PATCH] update automigrate templates to check collection indexes --- plugins/migratecmd/migratecmd_test.go | 32 ++++++++++++++++++++++++++ plugins/migratecmd/templates.go | 29 +++++++++++++++++++++++ tests/data/data.db | Bin 237568 -> 237568 bytes 3 files changed, 61 insertions(+) diff --git a/plugins/migratecmd/migratecmd_test.go b/plugins/migratecmd/migratecmd_test.go index 4f8e8ff3..e6dd0179 100644 --- a/plugins/migratecmd/migratecmd_test.go +++ b/plugins/migratecmd/migratecmd_test.go @@ -31,6 +31,9 @@ migrate((db) => { "type": "auth", "system": true, "schema": [], + "indexes": [ + "create index test on new_name (id)" + ], "listRule": "@request.auth.id != '' && created > 0 || 'backtick` + "`" + `test' = 0", "viewRule": "id = \"1\"", "createRule": null, @@ -81,6 +84,9 @@ func init() { "type": "auth", "system": true, "schema": [], + "indexes": [ + "create index test on new_name (id)" + ], "listRule": "@request.auth.id != '' && created > 0 || ` + "'backtick` + \"`\" + `test' = 0" + `", "viewRule": "id = \"1\"", "createRule": null, @@ -143,6 +149,7 @@ func init() { collection.Updated = collection.Created collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0") collection.ViewRule = types.Pointer(`id = "1"`) + collection.Indexes = types.JsonArray{"create index test on new_name (id)"} collection.SetOptions(models.CollectionAuthOptions{ ManageRule: types.Pointer("created > 0"), MinPasswordLength: 20, @@ -201,6 +208,9 @@ migrate((db) => { "type": "auth", "system": false, "schema": [], + "indexes": [ + "create index test on test456 (id)" + ], "listRule": "@request.auth.id != '' && created > 0 || 'backtick` + "`" + `test' = 0", "viewRule": "id = \"1\"", "createRule": null, @@ -255,6 +265,9 @@ func init() { "type": "auth", "system": false, "schema": [], + "indexes": [ + "create index test on test456 (id)" + ], "listRule": "@request.auth.id != '' && created > 0 || ` + "'backtick` + \"`\" + `test' = 0" + `", "viewRule": "id = \"1\"", "createRule": null, @@ -305,6 +318,7 @@ func init() { collection.Updated = collection.Created collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0") collection.ViewRule = types.Pointer(`id = "1"`) + collection.Indexes = types.JsonArray{"create index test on test456 (id)"} collection.SetOptions(models.CollectionAuthOptions{ ManageRule: types.Pointer("created > 0"), MinPasswordLength: 20, @@ -367,6 +381,9 @@ migrate((db) => { collection.listRule = null collection.deleteRule = "updated > 0 && @request.auth.id != ''" collection.options = {} + collection.indexes = [ + "create index test1 on test456_update (f1_name)" + ] // remove collection.schema.removeField("f3_id") @@ -419,6 +436,9 @@ migrate((db) => { "onlyEmailDomains": null, "requireEmail": false } + collection.indexes = [ + "create index test1 on test456 (f1_name)" + ] // add collection.schema.addField(new SchemaField({ @@ -488,6 +508,10 @@ func init() { json.Unmarshal([]byte(` + "`" + `{}` + "`" + `), &options) collection.SetOptions(options) + json.Unmarshal([]byte(` + "`" + `[ + "create index test1 on test456_update (f1_name)" + ]` + "`" + `), &collection.Indexes) + // remove collection.Schema.RemoveField("f3_id") @@ -554,6 +578,10 @@ func init() { }` + "`" + `), &options) collection.SetOptions(options) + json.Unmarshal([]byte(` + "`" + `[ + "create index test1 on test456 (f1_name)" + ]` + "`" + `), &collection.Indexes) + // add del_f3_name := &schema.SchemaField{} json.Unmarshal([]byte(` + "`" + `{ @@ -614,6 +642,7 @@ func init() { collection.Updated = collection.Created collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0") collection.ViewRule = types.Pointer(`id = "1"`) + collection.Indexes = types.JsonArray{"create index test1 on test456 (f1_name)"} collection.SetOptions(models.CollectionAuthOptions{ ManageRule: types.Pointer("created > 0"), MinPasswordLength: 20, @@ -652,6 +681,9 @@ func init() { collection.Type = models.CollectionTypeBase collection.DeleteRule = types.Pointer(`updated > 0 && @request.auth.id != ''`) collection.ListRule = nil + collection.Indexes = types.JsonArray{ + "create index test1 on test456_update (f1_name)", + } collection.NormalizeOptions() collection.Schema.RemoveField("f3_id") collection.Schema.AddField(&schema.SchemaField{ diff --git a/plugins/migratecmd/templates.go b/plugins/migratecmd/templates.go index 283973cd..f94dc989 100644 --- a/plugins/migratecmd/templates.go +++ b/plugins/migratecmd/templates.go @@ -196,6 +196,20 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection) downParts = append(downParts, fmt.Sprintf("%s.options = %s", varName, rawOldOptions)) } + // Indexes + rawNewIndexes, err := marhshalWithoutEscape(new.Indexes, " ", " ") + if err != nil { + return "", err + } + rawOldIndexes, err := marhshalWithoutEscape(old.Indexes, " ", " ") + if err != nil { + return "", err + } + if !bytes.Equal(rawNewIndexes, rawOldIndexes) { + upParts = append(upParts, fmt.Sprintf("%s.indexes = %s", varName, rawNewIndexes)) + downParts = append(downParts, fmt.Sprintf("%s.indexes = %s", varName, rawOldIndexes)) + } + // ensure new line between regular and collection fields if len(upParts) > 0 { upParts[len(upParts)-1] += "\n" @@ -571,6 +585,21 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection) downParts = append(downParts, fmt.Sprintf("%s.SetOptions(options)\n", varName)) } + // Indexes + rawNewIndexes, err := marhshalWithoutEscape(new.Indexes, "\t\t", "\t") + if err != nil { + return "", err + } + rawOldIndexes, err := marhshalWithoutEscape(old.Indexes, "\t\t", "\t") + if err != nil { + return "", err + } + if !bytes.Equal(rawNewIndexes, rawOldIndexes) { + upParts = append(upParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)\n", escapeBacktick(string(rawNewIndexes)), varName)) + // --- + downParts = append(downParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)\n", escapeBacktick(string(rawOldIndexes)), varName)) + } + // Schema // --------------------------------------------------------------- // deleted fields diff --git a/tests/data/data.db b/tests/data/data.db index 99125146ef3e6f0bfebe0652d0c68a2eaff44409..aa1d1f60cda113d5dcc60c53929298e00486060b 100644 GIT binary patch delta 381 zcmZoTz}IkqZ-TU-Bm)D36%;c9>0J|bj2R_2CM@BXLVT(!%)X3-jOT5R)Y~4Z$GDkXjm4Vk%Gs@U1ZRaF@~%{K>f`7o*LC?r4~pP=9s?C+=G z;_BuY>Jy@%6dkLi;O8Hr;1}xSqq#k9A)`Fg<{7LBjFb0r*lzy8c9fAxmuIpdzwBfV pF8j&9INnd5$5S+UBVX@k0p5j-(+{p^G@oq8-`uu=aoGl@4*;5@Zn*#e delta 287 zcmZoTz}IkqZ-TU-1Oo$u6%;c9>75gGj2R_1CM@BX;o%cu;D5@$oxhVmirWW zDZXWVO?>`*BAX2rzVNbI@-SzzO^(&m+w3m4ml3ETl7asf{}KLq{N?=N{O0`PeBb%5 z^KIwr;fvfX=wQj$9I3ZGQjc*(VE}V2gCJuZ1HT>rL0$oVe!d5M8@b$huJg>}_{Al` z`I7Ai$6hv9))|a(%(bj)ENfWQnAb4uXG~-8<^KV6Bq{(ZT|K@0E@K`SkYy<@F3#Ab zK3(b_W71@8Mm<*M6-+AP+ixsolxJe$|G^|ZxsX*I$Y5aEJdyDY%*%s>-4 OK&G~BU|hC==>q^ITu4R$