MCPcopy
hub / github.com/pocketbase/pocketbase / createCollectionIndexes

Function createCollectionIndexes

core/collection_record_table_sync.go:325–366  ·  view source on GitHub ↗
(app App, collection *Collection)

Source from the content-addressed store, hash-verified

323}
324
325func createCollectionIndexes(app App, collection *Collection) error {
326 if collection.IsView() {
327 return nil // views don't have indexes
328 }
329
330 return app.RunInTransaction(func(txApp App) error {
331 // upsert new indexes
332 //
333 // note: we are returning validation errors because the indexes cannot be
334 // easily validated in a form, aka. before persisting the related
335 // collection record table changes
336 errs := validation.Errors{}
337 for i, idx := range collection.Indexes {
338 parsed := dbutils.ParseIndex(idx)
339
340 // ensure that the index is always for the current collection
341 parsed.TableName = collection.Name
342
343 if !parsed.IsValid() {
344 errs[strconv.Itoa(i)] = validation.NewError(
345 "validation_invalid_index_expression",
346 "Invalid CREATE INDEX expression.",
347 )
348 continue
349 }
350
351 if _, err := txApp.DB().NewQuery(parsed.Build()).Execute(); err != nil {
352 errs[strconv.Itoa(i)] = validation.NewError(
353 "validation_invalid_index_expression",
354 fmt.Sprintf("Failed to create index %s - %v.", parsed.IndexName, err.Error()),
355 )
356 continue
357 }
358 }
359
360 if len(errs) > 0 {
361 return validation.Errors{"indexes": errs}
362 }
363
364 return nil
365 })
366}

Callers 1

SyncRecordTableSchemaMethod · 0.85

Calls 9

ParseIndexFunction · 0.92
IsViewMethod · 0.80
IsValidMethod · 0.80
ExecuteMethod · 0.80
NewQueryMethod · 0.80
RunInTransactionMethod · 0.65
DBMethod · 0.65
ErrorMethod · 0.65
BuildMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…