delete hook -------------------------------------------------------------------
(e *CollectionEvent)
| 684 | // ------------------------------------------------------------------- |
| 685 | |
| 686 | func onCollectionDeleteExecute(e *CollectionEvent) error { |
| 687 | if e.Collection.System { |
| 688 | return fmt.Errorf("[%s] system collections cannot be deleted", e.Collection.Name) |
| 689 | } |
| 690 | |
| 691 | defer func() { |
| 692 | if err := e.App.ReloadCachedCollections(); err != nil { |
| 693 | e.App.Logger().Warn("Failed to reload collections cache", "error", err) |
| 694 | } |
| 695 | }() |
| 696 | |
| 697 | if !e.Collection.disableIntegrityChecks { |
| 698 | // ensure that there aren't any existing references. |
| 699 | // note: the select is outside of the transaction to prevent SQLITE_LOCKED error when mixing read&write in a single transaction |
| 700 | references, err := e.App.FindCollectionReferences(e.Collection, e.Collection.Id) |
| 701 | if err != nil { |
| 702 | return fmt.Errorf("[%s] failed to check collection references: %w", e.Collection.Name, err) |
| 703 | } |
| 704 | if total := len(references); total > 0 { |
| 705 | names := make([]string, 0, len(references)) |
| 706 | for ref := range references { |
| 707 | names = append(names, ref.Name) |
| 708 | } |
| 709 | return fmt.Errorf("[%s] failed to delete due to existing relation references: %s", e.Collection.Name, strings.Join(names, ", ")) |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | originalApp := e.App |
| 714 | |
| 715 | txErr := e.App.RunInTransaction(func(txApp App) error { |
| 716 | e.App = txApp |
| 717 | |
| 718 | // delete the related view or records table |
| 719 | if e.Collection.IsView() { |
| 720 | if err := txApp.DeleteView(e.Collection.Name); err != nil { |
| 721 | return err |
| 722 | } |
| 723 | } else { |
| 724 | if err := txApp.DeleteTable(e.Collection.Name); err != nil { |
| 725 | return err |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if !e.Collection.disableIntegrityChecks { |
| 730 | // trigger views resave to check for dependencies |
| 731 | if err := resaveViewsWithChangedFields(txApp, e.Collection.Id); err != nil { |
| 732 | return fmt.Errorf("[%s] failed to delete due to existing view dependency: %w", e.Collection.Name, err) |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | // delete |
| 737 | return e.Next() |
| 738 | }) |
| 739 | |
| 740 | e.App = originalApp |
| 741 | |
| 742 | return txErr |
| 743 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…