(app App, collection *Collection)
| 323 | } |
| 324 | |
| 325 | func 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 | } |
no test coverage detected
searching dependent graphs…