forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
normalized values on maxSelect change
- Loading branch information
1 parent
65aa114
commit 5344ec8
Showing
6 changed files
with
430 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
migrations/1678082970_normalize_single_multiple_values.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pocketbase/dbx" | ||
"github.com/pocketbase/pocketbase/daos" | ||
"github.com/pocketbase/pocketbase/models" | ||
"github.com/pocketbase/pocketbase/models/schema" | ||
) | ||
|
||
// Normalizes old single and multiple values of MultiValuer fields (file, select, relation). | ||
func init() { | ||
AppMigrations.Register(func(db dbx.Builder) error { | ||
dao := daos.New(db) | ||
|
||
collections := []*models.Collection{} | ||
if err := dao.CollectionQuery().All(&collections); err != nil { | ||
return err | ||
} | ||
|
||
for _, c := range collections { | ||
if c.IsView() { | ||
// skip view collections | ||
continue | ||
} | ||
|
||
for _, f := range c.Schema.Fields() { | ||
opt, ok := f.Options.(schema.MultiValuer) | ||
if !ok { | ||
continue | ||
} | ||
|
||
var updateQuery *dbx.Query | ||
|
||
if opt.IsMultiple() { | ||
updateQuery = dao.DB().NewQuery(fmt.Sprintf( | ||
`UPDATE {{%s}} set [[%s]] = ( | ||
CASE | ||
WHEN COALESCE([[%s]], '') = '' | ||
THEN '[]' | ||
ELSE ( | ||
CASE | ||
WHEN json_valid([[%s]]) AND json_type([[%s]]) == 'array' | ||
THEN [[%s]] | ||
ELSE json_array([[%s]]) | ||
END | ||
) | ||
END | ||
)`, | ||
c.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
)) | ||
} else { | ||
updateQuery = dao.DB().NewQuery(fmt.Sprintf( | ||
`UPDATE {{%s}} set [[%s]] = ( | ||
CASE | ||
WHEN COALESCE([[%s]], '[]') = '[]' | ||
THEN '' | ||
ELSE ( | ||
CASE | ||
WHEN json_valid([[%s]]) AND json_type([[%s]]) == 'array' | ||
THEN COALESCE(json_extract([[%s]], '$[#-1]'), '') | ||
ELSE [[%s]] | ||
END | ||
) | ||
END | ||
)`, | ||
c.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
f.Name, | ||
)) | ||
} | ||
|
||
if _, err := updateQuery.Execute(); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// trigger view query update after the records normalization | ||
// (ignore save error in case of invalid query to allow users to change it from the UI) | ||
for _, c := range collections { | ||
if !c.IsView() { | ||
continue | ||
} | ||
|
||
dao.SaveCollection(c) | ||
} | ||
|
||
return nil | ||
}, func(db dbx.Builder) error { | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.