MCPcopy
hub / github.com/pocketbase/pocketbase / onCollectionSaveExecute

Function onCollectionSaveExecute

core/collection_model.go:846–932  ·  view source on GitHub ↗
(e *CollectionEvent)

Source from the content-addressed store, hash-verified

844}
845
846func onCollectionSaveExecute(e *CollectionEvent) error {
847 defer func() {
848 if err := e.App.ReloadCachedCollections(); err != nil {
849 e.App.Logger().Warn("Failed to reload collections cache", "error", err)
850 }
851 }()
852
853 var oldCollection *Collection
854 if !e.Collection.IsNew() {
855 var err error
856 oldCollection, err = e.App.FindCachedCollectionByNameOrId(e.Collection.Id)
857 if err != nil {
858 return err
859 }
860
861 // invalidate previously issued auth tokens on auth rule change
862 if oldCollection.AuthRule != e.Collection.AuthRule &&
863 cast.ToString(oldCollection.AuthRule) != cast.ToString(e.Collection.AuthRule) {
864 e.Collection.AuthToken.Secret = security.RandomString(50)
865 }
866 }
867
868 originalApp := e.App
869 txErr := e.App.RunInTransaction(func(txApp App) error {
870 e.App = txApp
871
872 isView := e.Collection.IsView()
873
874 // ensures that the view collection shema is properly loaded
875 if isView {
876 query := e.Collection.ViewQuery
877
878 // generate collection fields list from the query
879 viewFields, err := e.App.CreateViewFields(query)
880 if err != nil {
881 return err
882 }
883
884 // delete old renamed view
885 if oldCollection != nil {
886 if err := e.App.DeleteView(oldCollection.Name); err != nil {
887 return err
888 }
889 }
890
891 // wrap view query if necessary
892 query, err = normalizeViewQueryId(e.App, query)
893 if err != nil {
894 return fmt.Errorf("failed to normalize view query id: %w", err)
895 }
896
897 // (re)create the view
898 if err := e.App.SaveView(e.Collection.Name, query); err != nil {
899 return err
900 }
901
902 // updates newCollection.Fields based on the generated view table info and query
903 e.Collection.Fields = viewFields

Callers

nothing calls this directly

Calls 14

RandomStringFunction · 0.92
normalizeViewQueryIdFunction · 0.85
IsViewMethod · 0.80
LoggerMethod · 0.65
IsNewMethod · 0.65
RunInTransactionMethod · 0.65
CreateViewFieldsMethod · 0.65
DeleteViewMethod · 0.65
SaveViewMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…