Skip to content

Commit

Permalink
[pocketbase#2570] use ON for consistency with the SQLite examples and…
Browse files Browse the repository at this point in the history
… updated collection delete test to check for _exterAuths references
  • Loading branch information
ganigeorgiev committed May 25, 2023
1 parent c72c951 commit 833a84b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func initPragmas(db *dbx.DB) error {
PRAGMA journal_mode = WAL;
PRAGMA journal_size_limit = 200000000;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = TRUE;
PRAGMA foreign_keys = ON;
`).Execute()

return err
Expand Down
29 changes: 25 additions & 4 deletions daos/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
Expand Down Expand Up @@ -166,9 +167,9 @@ func TestDeleteCollection(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()

colEmpty := &models.Collection{}
colUnsaved := &models.Collection{}

colAuth, err := app.Dao().FindCollectionByNameOrId("clients")
colAuth, err := app.Dao().FindCollectionByNameOrId("users")
if err != nil {
t.Fatal(err)
}
Expand All @@ -187,6 +188,11 @@ func TestDeleteCollection(t *testing.T) {
t.Fatal(err)
}

colBase, err := app.Dao().FindCollectionByNameOrId("demo1")
if err != nil {
t.Fatal(err)
}

colView1, err := app.Dao().FindCollectionByNameOrId("view1")
if err != nil {
t.Fatal(err)
Expand All @@ -201,12 +207,14 @@ func TestDeleteCollection(t *testing.T) {
model *models.Collection
expectError bool
}{
{colEmpty, true},
{colAuth, false},
{colUnsaved, true},
{colReferenced, true},
{colSystem, true},
{colView1, true}, // view2 depend on it
{colView2, false},
{colView1, false}, // no longer has dependent collections
{colBase, false},
{colAuth, false}, // should delete also its related external auths
}

for i, s := range scenarios {
Expand All @@ -225,6 +233,19 @@ func TestDeleteCollection(t *testing.T) {
if app.Dao().HasTable(s.model.Name) {
t.Errorf("[%d] Expected table/view %s to be deleted", i, s.model.Name)
}

// check if the external auths were deleted
if s.model.IsAuth() {
var total int
err := app.Dao().ExternalAuthQuery().
Select("count(*)").
AndWhere(dbx.HashExp{"collectionId": s.model.Id}).
Row(&total)

if err != nil || total > 0 {
t.Fatalf("[%d] Expected external auths to be deleted, got %v (%v)", i, total, err)
}
}
}
}

Expand Down

0 comments on commit 833a84b

Please sign in to comment.