1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-02-06 13:30:00 +00:00

add modified fields with order in the generated migration

This commit is contained in:
Gani Georgiev 2024-11-24 15:02:28 +02:00
parent 0efca0f936
commit 5936fc3ac3
2 changed files with 14 additions and 8 deletions

View File

@ -937,7 +937,7 @@ migrate((app) => {
}))
// update field
collection.fields.add(new Field({
collection.fields.addAt(7, new Field({
"hidden": false,
"id": "f2_id",
"max": null,
@ -989,7 +989,7 @@ migrate((app) => {
collection.fields.removeById("f4_id")
// update field
collection.fields.add(new Field({
collection.fields.addAt(7, new Field({
"hidden": false,
"id": "f2_id",
"max": null,
@ -1070,7 +1070,7 @@ func init() {
}
// update field
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
if err := collection.Fields.AddMarshaledJSONAt(7, []byte(` + "`" + `{
"hidden": false,
"id": "f2_id",
"max": null,
@ -1131,7 +1131,7 @@ func init() {
collection.Fields.RemoveById("f4_id")
// update field
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
if err := collection.Fields.AddMarshaledJSONAt(7, []byte(` + "`" + `{
"hidden": false,
"id": "f2_id",
"max": null,

View File

@ -239,12 +239,15 @@ func (p *plugin) jsDiffTemplate(new *core.Collection, old *core.Collection) (str
return "", err
}
var oldFieldIndex int
for j, oldField := range old.Fields {
if oldField.GetId() == newField.GetId() {
rawOldField, err = marhshalWithoutEscape(oldFieldsSlice[j], " ", " ")
if err != nil {
return "", err
}
oldFieldIndex = j
break
}
}
@ -254,10 +257,10 @@ func (p *plugin) jsDiffTemplate(new *core.Collection, old *core.Collection) (str
}
upParts = append(upParts, "// update field")
upParts = append(upParts, fmt.Sprintf("%s.fields.add(new Field(%s))\n", varName, rawNewField))
upParts = append(upParts, fmt.Sprintf("%s.fields.addAt(%d, new Field(%s))\n", varName, i, rawNewField))
downParts = append(downParts, "// update field")
downParts = append(downParts, fmt.Sprintf("%s.fields.add(new Field(%s))\n", varName, rawOldField))
downParts = append(downParts, fmt.Sprintf("%s.fields.addAt(%d, new Field(%s))\n", varName, oldFieldIndex, rawOldField))
}
// -----------------------------------------------------------------
@ -578,12 +581,15 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
return "", err
}
var oldFieldIndex int
for j, oldField := range old.Fields {
if oldField.GetId() == newField.GetId() {
rawOldField, err = marhshalWithoutEscape(oldFieldsSlice[j], "\t\t", "\t")
if err != nil {
return "", err
}
oldFieldIndex = j
break
}
}
@ -593,10 +599,10 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
}
upParts = append(upParts, "// update field")
upParts = append(upParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawNewField)))))
upParts = append(upParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSONAt(%d, []byte(`%s`))", varName, i, escapeBacktick(string(rawNewField)))))
downParts = append(downParts, "// update field")
downParts = append(downParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawOldField)))))
downParts = append(downParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSONAt(%d, []byte(`%s`))", varName, oldFieldIndex, escapeBacktick(string(rawOldField)))))
}
// ---------------------------------------------------------------