From 821f59b8c7457806e4cce6f5d06c780d1894fa49 Mon Sep 17 00:00:00 2001 From: lalalalatt Date: Fri, 19 Jul 2024 01:25:28 +0800 Subject: [PATCH 1/2] chore: add relationship index storing caveat to migrations --- ....0007_add_relation_index_storing_caveat.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go diff --git a/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go b/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go new file mode 100644 index 0000000000..16453b2f90 --- /dev/null +++ b/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go @@ -0,0 +1,27 @@ +package migrations + +import ( + "context" + + "github.com/jackc/pgx/v5" +) + +const ( + addRelationshipIndexStoringCaveatQuery = ` + CREATE INDEX idx_relationship_caveat_storing ON relation_tuple (namespace, relation, object_id, userset_relation) STORING (caveat_name, caveat_context); + ` +) + +func init() { + err := CRDBMigrations.Register("add-relationship-index-storing-caveat", "add-relationship-counters-table", addRelationshipIndexStoringCaveat, noAtomicMigration) + if err != nil { + panic("failed to register migration: " + err.Error()) + } +} + +func addRelationshipIndexStoringCaveat(ctx context.Context, conn *pgx.Conn) error { + if _, err := conn.Exec(ctx, addRelationshipIndexStoringCaveatQuery); err != nil { + return err + } + return nil +} From e5e03b0cc23b1d7619b32b35ceda4c4fdffe9f45 Mon Sep 17 00:00:00 2001 From: peterxcli Date: Tue, 23 Jul 2024 11:21:58 +0800 Subject: [PATCH 2/2] chore: update relationship index storing caveat in migrations --- ...ration.0007_add_relation_index_storing_caveat.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go b/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go index 16453b2f90..eb4e17fd79 100644 --- a/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go +++ b/internal/datastore/crdb/migrations/zz_migration.0007_add_relation_index_storing_caveat.go @@ -8,7 +8,18 @@ import ( const ( addRelationshipIndexStoringCaveatQuery = ` - CREATE INDEX idx_relationship_caveat_storing ON relation_tuple (namespace, relation, object_id, userset_relation) STORING (caveat_name, caveat_context); + -- Drop existing indexes + DROP INDEX IF EXISTS ix_relation_tuple_by_subject; + DROP INDEX IF EXISTS ix_relation_tuple_by_subject_relation; + + -- Create new indexes with the STORING clause + CREATE INDEX ix_relation_tuple_by_subject_storing_caveat + ON relation_tuple (userset_object_id, userset_namespace, userset_relation, namespace, relation) + STORING (caveat_name, caveat_context); + + CREATE INDEX ix_relation_tuple_by_subject_relation_storing_caveat + ON relation_tuple (userset_namespace, userset_relation, namespace, relation) + STORING (caveat_name, caveat_context); ` )