added explicit errors when trying to truncate view collections or deleting view records
This commit is contained in:
@@ -176,6 +176,10 @@ func (app *BaseApp) FindCollectionReferences(collection *Collection, excludeIds
|
||||
// Note that this method will also trigger the records related
|
||||
// cascade and file delete actions.
|
||||
func (app *BaseApp) TruncateCollection(collection *Collection) error {
|
||||
if collection.IsView() {
|
||||
return errors.New("view collections cannot be truncated since they don't store their own records.")
|
||||
}
|
||||
|
||||
return app.RunInTransaction(func(txApp App) error {
|
||||
records := make([]*Record, 0, 500)
|
||||
|
||||
|
||||
@@ -299,6 +299,18 @@ func TestFindCollectionTruncate(t *testing.T) {
|
||||
return len(entries), err
|
||||
}
|
||||
|
||||
t.Run("truncate view", func(t *testing.T) {
|
||||
view2, err := app.FindCollectionByNameOrId("view2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = app.TruncateCollection(view2)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected truncate to fail because view collections can't be truncated")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("truncate failure", func(t *testing.T) {
|
||||
demo3, err := app.FindCollectionByNameOrId("demo3")
|
||||
if err != nil {
|
||||
|
||||
@@ -378,7 +378,13 @@ func (app *BaseApp) registerRecordHooks() {
|
||||
e.Context,
|
||||
e.App,
|
||||
InterceptorActionDelete,
|
||||
e.Next,
|
||||
func() error {
|
||||
if e.Record.Collection().IsView() {
|
||||
return errors.New("view records cannot be deleted")
|
||||
}
|
||||
|
||||
return e.Next()
|
||||
},
|
||||
)
|
||||
},
|
||||
Priority: -99,
|
||||
|
||||
@@ -1841,9 +1841,21 @@ func TestRecordDelete(t *testing.T) {
|
||||
|
||||
// delete unsaved record
|
||||
// ---
|
||||
rec0 := core.NewRecord(demoCollection)
|
||||
if err := app.Delete(rec0); err == nil {
|
||||
t.Fatal("(rec0) Didn't expect to succeed deleting unsaved record")
|
||||
newRec := core.NewRecord(demoCollection)
|
||||
if err := app.Delete(newRec); err == nil {
|
||||
t.Fatal("(newRec) Didn't expect to succeed deleting unsaved record")
|
||||
}
|
||||
|
||||
// delete view record
|
||||
// ---
|
||||
viewRec, _ := app.FindRecordById("view2", "84nmscqy84lsi1t")
|
||||
if err := app.Delete(viewRec); err == nil {
|
||||
t.Fatal("(viewRec) Didn't expect to succeed deleting view record")
|
||||
}
|
||||
// check if it still exists
|
||||
viewRec, _ = app.FindRecordById(viewRec.Collection().Id, viewRec.Id)
|
||||
if viewRec == nil {
|
||||
t.Fatal("(viewRec) Expected view record to still exists")
|
||||
}
|
||||
|
||||
// delete existing record + external auths
|
||||
|
||||
Reference in New Issue
Block a user