1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-21 13:35:49 +02:00

[#4448] added error checks to the autogenerated Go migrations

This commit is contained in:
Gani Georgiev 2024-02-29 04:17:59 +02:00
parent b845d3dbea
commit 9babca5f77
3 changed files with 45 additions and 23 deletions

View File

@ -2,6 +2,8 @@
- Initialize `RecordAuthWithOAuth2Event.IsNewRecord` for the `OnRecordBeforeAuthWithOAuth2Request` hook ([#4437](https://github.com/pocketbase/pocketbase/discussions/4437)).
- Added error checks to the autogenerated Go migrations ([#4448](https://github.com/pocketbase/pocketbase/issues/4448)).
## v0.22.0

View File

@ -532,19 +532,23 @@ func init() {
collection.DeleteRule = nil
options := map[string]any{}
json.Unmarshal([]byte(` + "`" + `{}` + "`" + `), &options)
if err := json.Unmarshal([]byte(` + "`" + `{}` + "`" + `), &options); err != nil {
return err
}
collection.SetOptions(options)
json.Unmarshal([]byte(` + "`" + `[
if err := json.Unmarshal([]byte(` + "`" + `[
"create index test1 on test456_update (f1_name)"
]` + "`" + `), &collection.Indexes)
]` + "`" + `), &collection.Indexes); err != nil {
return err
}
// remove
collection.Schema.RemoveField("f3_id")
// add
new_f4_name := &schema.SchemaField{}
json.Unmarshal([]byte(` + "`" + `{
if err := json.Unmarshal([]byte(` + "`" + `{
"system": false,
"id": "f4_id",
"name": "f4_name",
@ -557,12 +561,14 @@ func init() {
"max": null,
"pattern": ` + "\"` + \"`\" + `test backtick` + \"`\" + `123\"" + `
}
}` + "`" + `), new_f4_name)
}` + "`" + `), new_f4_name); err != nil {
return err
}
collection.Schema.AddField(new_f4_name)
// update
edit_f2_name_new := &schema.SchemaField{}
json.Unmarshal([]byte(` + "`" + `{
if err := json.Unmarshal([]byte(` + "`" + `{
"system": false,
"id": "f2_id",
"name": "f2_name_new",
@ -575,7 +581,9 @@ func init() {
"max": null,
"noDecimal": false
}
}` + "`" + `), edit_f2_name_new)
}` + "`" + `), edit_f2_name_new); err != nil {
return err
}
collection.Schema.AddField(edit_f2_name_new)
return dao.SaveCollection(collection)
@ -600,7 +608,7 @@ func init() {
collection.DeleteRule = types.Pointer("id = \"3\"")
options := map[string]any{}
json.Unmarshal([]byte(` + "`" + `{
if err := json.Unmarshal([]byte(` + "`" + `{
"allowEmailAuth": false,
"allowOAuth2Auth": false,
"allowUsernameAuth": false,
@ -610,16 +618,20 @@ func init() {
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}` + "`" + `), &options)
}` + "`" + `), &options); err != nil {
return err
}
collection.SetOptions(options)
json.Unmarshal([]byte(` + "`" + `[
if err := json.Unmarshal([]byte(` + "`" + `[
"create index test1 on test456 (f1_name)"
]` + "`" + `), &collection.Indexes)
]` + "`" + `), &collection.Indexes); err != nil {
return err
}
// add
del_f3_name := &schema.SchemaField{}
json.Unmarshal([]byte(` + "`" + `{
if err := json.Unmarshal([]byte(` + "`" + `{
"system": false,
"id": "f3_id",
"name": "f3_name",
@ -628,7 +640,9 @@ func init() {
"presentable": false,
"unique": false,
"options": {}
}` + "`" + `), del_f3_name)
}` + "`" + `), del_f3_name); err != nil {
return err
}
collection.Schema.AddField(del_f3_name)
// remove
@ -636,7 +650,7 @@ func init() {
// update
edit_f2_name_new := &schema.SchemaField{}
json.Unmarshal([]byte(` + "`" + `{
if err := json.Unmarshal([]byte(` + "`" + `{
"system": false,
"id": "f2_id",
"name": "f2_name",
@ -649,7 +663,9 @@ func init() {
"max": null,
"noDecimal": false
}
}` + "`" + `), edit_f2_name_new)
}` + "`" + `), edit_f2_name_new); err != nil {
return err
}
collection.Schema.AddField(edit_f2_name_new)
return dao.SaveCollection(collection)

View File

@ -598,11 +598,11 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
}
if !bytes.Equal(rawNewOptions, rawOldOptions) {
upParts = append(upParts, "options := map[string]any{}")
upParts = append(upParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &options)", escapeBacktick(string(rawNewOptions))))
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), &options)", escapeBacktick(string(rawNewOptions)))))
upParts = append(upParts, fmt.Sprintf("%s.SetOptions(options)\n", varName))
// ---
downParts = append(downParts, "options := map[string]any{}")
downParts = append(downParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &options)", escapeBacktick(string(rawOldOptions))))
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), &options)", escapeBacktick(string(rawOldOptions)))))
downParts = append(downParts, fmt.Sprintf("%s.SetOptions(options)\n", varName))
}
@ -616,9 +616,9 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
return "", err
}
if !bytes.Equal(rawNewIndexes, rawOldIndexes) {
upParts = append(upParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)\n", escapeBacktick(string(rawNewIndexes)), varName))
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)", escapeBacktick(string(rawNewIndexes)), varName))+"\n")
// ---
downParts = append(downParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)\n", escapeBacktick(string(rawOldIndexes)), varName))
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), &%s.Indexes)", escapeBacktick(string(rawOldIndexes)), varName))+"\n")
}
// Schema
@ -641,7 +641,7 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
downParts = append(downParts, "// add")
downParts = append(downParts, fmt.Sprintf("%s := &schema.SchemaField{}", fieldVar))
downParts = append(downParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawOldField)), fieldVar))
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawOldField)), fieldVar)))
downParts = append(downParts, fmt.Sprintf("%s.Schema.AddField(%s)\n", varName, fieldVar))
}
@ -660,7 +660,7 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
upParts = append(upParts, "// add")
upParts = append(upParts, fmt.Sprintf("%s := &schema.SchemaField{}", fieldVar))
upParts = append(upParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawNewField)), fieldVar))
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawNewField)), fieldVar)))
upParts = append(upParts, fmt.Sprintf("%s.Schema.AddField(%s)\n", varName, fieldVar))
downParts = append(downParts, "// remove")
@ -692,12 +692,12 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
upParts = append(upParts, "// update")
upParts = append(upParts, fmt.Sprintf("%s := &schema.SchemaField{}", fieldVar))
upParts = append(upParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawNewField)), fieldVar))
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawNewField)), fieldVar)))
upParts = append(upParts, fmt.Sprintf("%s.Schema.AddField(%s)\n", varName, fieldVar))
downParts = append(downParts, "// update")
downParts = append(downParts, fmt.Sprintf("%s := &schema.SchemaField{}", fieldVar))
downParts = append(downParts, fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawOldField)), fieldVar))
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`%s`), %s)", escapeBacktick(string(rawOldField)), fieldVar)))
downParts = append(downParts, fmt.Sprintf("%s.Schema.AddField(%s)\n", varName, fieldVar))
}
// ---------------------------------------------------------------
@ -791,3 +791,7 @@ func marhshalWithoutEscape(v any, prefix string, indent string) ([]byte, error)
func escapeBacktick(v string) string {
return strings.ReplaceAll(v, "`", "` + \"`\" + `")
}
func goErrIf(v string) string {
return "if err := " + v + "; err != nil {\n\t\t\treturn err\n\t\t}"
}