Skip to content

Commit

Permalink
PR(ACP): Disable Creating Index
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Apr 2, 2024
1 parent 4bed6dd commit 644acc2
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ type Collection interface {
// `IndexDescription.Name` must start with a letter or an underscore and can
// only contain letters, numbers, and underscores.
// If the name of the index is not provided, it will be generated.
// WARNING: This method can not create index for a collection that has a policy.
CreateIndex(context.Context, IndexDescription) (IndexDescription, error)

// DropIndex drops an index from the collection.
Expand Down
6 changes: 6 additions & 0 deletions db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ func (c *collection) createIndex(
txn datastore.Txn,
desc client.IndexDescription,
) (CollectionIndex, error) {
// Don't allow creating index on a permissioned collection, until following is implemented.
// TODO-ACP: ACP <> INDEX https://github.com/sourcenetwork/defradb/issues/2365
if c.Description().Policy.HasValue() {
return nil, ErrCanNotCreateIndexOnCollectionWithPolicy
}

if desc.Name != "" && !schema.IsValidIndexName(desc.Name) {
return nil, schema.NewErrIndexWithInvalidName("!")
}
Expand Down
1 change: 1 addition & 0 deletions db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const (

var (
ErrFailedToGetCollection = errors.New(errFailedToGetCollection)
ErrCanNotCreateIndexOnCollectionWithPolicy = errors.New("can not create index on a collection with a policy")
ErrSubscriptionsNotAllowed = errors.New("server does not accept subscriptions")
ErrInvalidFilter = errors.New("invalid filter")
ErrCollectionAlreadyExists = errors.New(errCollectionAlreadyExists)
Expand Down
174 changes: 174 additions & 0 deletions tests/integration/acp/index/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package test_acp_index

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
acpUtils "github.com/sourcenetwork/defradb/tests/integration/acp"
)

// This test documents that we don't allow creating indexes on collections that have policy
// until the following is implemented:
// TODO-ACP: ACP <> P2P https://github.com/sourcenetwork/defradb/issues/2365
func TestACP_IndexCreateWithSeparateRequest_OnCollectionWithPolicy_ReturnError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test acp, with creating new index using separate request on permissioned collection, error",
Actions: []any{

testUtils.AddPolicy{

Creator: acpUtils.Actor1Signature,

Policy: `
description: a test policy which marks a collection in a database as a resource
actor:
name: actor
resources:
users:
permissions:
read:
expr: owner + reader
write:
expr: owner
relations:
owner:
types:
- actor
reader:
types:
- actor
admin:
manages:
- reader
types:
- actor
`,

ExpectedPolicyID: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001",
},

testUtils.SchemaUpdate{
Schema: `
type Users @policy(
id: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001",
resource: "users"
) {
name: String
age: Int
}
`,
},

testUtils.CreateIndex{
CollectionID: 0,

IndexName: "some_index",

FieldName: "name",

ExpectedError: "can not create index on a collection with a policy",
},

testUtils.Request{
Request: `
query {
Users {
name
age
}
}`,

Results: []map[string]any{},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

// This test documents that we don't allow creating indexes on collections that have policy
// until the following is implemented:
// TODO-ACP: ACP <> P2P https://github.com/sourcenetwork/defradb/issues/2365
func TestACP_IndexCreateWithDirective_OnCollectionWithPolicy_ReturnError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test acp, with creating new index using directive on permissioned collection, error",
Actions: []any{

testUtils.AddPolicy{

Creator: acpUtils.Actor1Signature,

Policy: `
description: a test policy which marks a collection in a database as a resource
actor:
name: actor
resources:
users:
permissions:
read:
expr: owner + reader
write:
expr: owner
relations:
owner:
types:
- actor
reader:
types:
- actor
admin:
manages:
- reader
types:
- actor
`,

ExpectedPolicyID: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001",
},

testUtils.SchemaUpdate{
Schema: `
type Users @policy(
id: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001",
resource: "users"
) {
name: String @index
age: Int
}
`,

ExpectedError: "can not create index on a collection with a policy",
},

testUtils.Request{
Request: `
query {
Users {
name
age
}
}`,

ExpectedError: `Cannot query field "Users" on type "Query"`,
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit 644acc2

Please sign in to comment.