updated collection indexes on system fields validator and normalized v0.23 old collections migration

This commit is contained in:
Gani Georgiev
2024-11-15 09:11:45 +02:00
parent e53c30ca4d
commit 18a7549e50
6 changed files with 140 additions and 24 deletions
+15 -13
View File
@@ -365,8 +365,10 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
c.Options["manageRule"] = nil
if options["manageRule"] != nil {
manageRule := cast.ToString(options["manageRule"])
c.Options["manageRule"] = &manageRule
manageRule, err := cast.ToStringE(options["manageRule"])
if err == nil && manageRule != "" {
c.Options["manageRule"] = migrateRule(&manageRule)
}
}
// passwordAuth
@@ -494,15 +496,15 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
// ---
c.Indexes = append(types.JSONArray[string]{
fmt.Sprintf("CREATE UNIQUE INDEX `_%s_username_idx` ON `%s` (username COLLATE NOCASE)", c.Id, c.Name),
fmt.Sprintf("CREATE UNIQUE INDEX `_%s_email_idx` ON `%s` (email) WHERE email != ''", c.Id, c.Name),
fmt.Sprintf("CREATE UNIQUE INDEX `_%s_tokenKey_idx` ON `%s` (tokenKey)", c.Id, c.Name),
fmt.Sprintf("CREATE UNIQUE INDEX `_%s_email_idx` ON `%s` (`email`) WHERE `email` != ''", c.Id, c.Name),
fmt.Sprintf("CREATE UNIQUE INDEX `_%s_tokenKey_idx` ON `%s` (`tokenKey`)", c.Id, c.Name),
}, c.Indexes...)
// prepend the auth system fields
// ---
tokenKeyField := map[string]any{
"id": fieldIdChecksum("text", "tokenKey"),
"type": "text",
"id": "_pbf_auth_tokenKey_",
"name": "tokenKey",
"system": true,
"hidden": true,
@@ -515,8 +517,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
"autogeneratePattern": "[a-zA-Z0-9_]{50}",
}
passwordField := map[string]any{
"id": fieldIdChecksum("password", "password"),
"type": "password",
"id": "_pbf_auth_password_",
"name": "password",
"presentable": false,
"system": true,
@@ -527,8 +529,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
"cost": bcrypt.DefaultCost, // new default
}
emailField := map[string]any{
"id": fieldIdChecksum("email", "email"),
"type": "email",
"id": "_pbf_auth_email_",
"name": "email",
"system": true,
"hidden": false,
@@ -538,8 +540,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
"onlyDomains": cast.ToStringSlice(options["onlyEmailDomains"]),
}
emailVisibilityField := map[string]any{
"id": fieldIdChecksum("bool", "emailVisibility"),
"type": "bool",
"id": "_pbf_auth_emailVisibility_",
"name": "emailVisibility",
"system": true,
"hidden": false,
@@ -547,8 +549,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
"required": false,
}
verifiedField := map[string]any{
"id": fieldIdChecksum("bool", "verified"),
"type": "bool",
"id": "_pbf_auth_verified_",
"name": "verified",
"system": true,
"hidden": false,
@@ -556,8 +558,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
"required": false,
}
usernameField := map[string]any{
"id": fieldIdChecksum("text", "username"),
"type": "text",
"id": "_pbf_auth_username_",
"name": "username",
"system": false,
"hidden": false,
@@ -597,8 +599,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
// prepend the id field
idField := map[string]any{
"id": fieldIdChecksum("text", "id"),
"type": "text",
"id": "_pbf_text_id_",
"name": "id",
"system": true,
"required": true,
@@ -631,8 +633,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
if addCreated {
createdField := map[string]any{
"id": fieldIdChecksum("autodate", "created"),
"type": "autodate",
"id": "_pbf_autodate_created_",
"name": "created",
"system": false,
"presentable": false,
@@ -645,8 +647,8 @@ func migrateOldCollections(txApp core.App, oldSettings *oldSettingsModel) error
if addUpdated {
updatedField := map[string]any{
"id": fieldIdChecksum("autodate", "updated"),
"type": "autodate",
"id": "_pbf_autodate_updated_",
"name": "updated",
"system": false,
"presentable": false,
+6 -6
View File
@@ -10,12 +10,12 @@ import (
// note: this migration will be deleted in future version
func collectionIdChecksum(c *core.Collection) string {
return "pbc_" + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(c.Type+c.Name))))
func collectionIdChecksum(typ, name string) string {
return "pbc_" + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(typ+name))))
}
func fieldIdChecksum(f core.Field) string {
return f.Type() + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(f.GetName()))))
func fieldIdChecksum(typ, name string) string {
return typ + strconv.Itoa(int(crc32.ChecksumIEEE([]byte(name))))
}
// normalize system collection and field ids
@@ -62,7 +62,7 @@ func init() {
originalId := c.Id
// normalize collection id
if checksum := collectionIdChecksum(c); c.Id != checksum {
if checksum := collectionIdChecksum(c.Type, c.Name); c.Id != checksum {
c.Id = checksum
needUpdate = true
}
@@ -73,7 +73,7 @@ func init() {
continue
}
if checksum := fieldIdChecksum(f); f.GetId() != checksum {
if checksum := fieldIdChecksum(f.Type(), f.GetName()); f.GetId() != checksum {
f.SetId(checksum)
needUpdate = true
}