github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/spanner/migrations/zz_migration.0001_initial_schema.go (about) 1 package migrations 2 3 import ( 4 "context" 5 6 "cloud.google.com/go/spanner" 7 "cloud.google.com/go/spanner/admin/database/apiv1/databasepb" 8 ) 9 10 const ( 11 createNamespaceConfig = `CREATE TABLE namespace_config ( 12 namespace STRING(1024), 13 serialized_config BYTES(MAX) NOT NULL, 14 timestamp TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true) 15 ) PRIMARY KEY (namespace)` 16 17 createRelationTuple = `CREATE TABLE relation_tuple ( 18 namespace STRING(1024) NOT NULL, 19 object_id STRING(1024) NOT NULL, 20 relation STRING(1024) NOT NULL, 21 userset_namespace STRING(1024) NOT NULL, 22 userset_object_id STRING(1024) NOT NULL, 23 userset_relation STRING(1024) NOT NULL, 24 timestamp TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true) 25 ) PRIMARY KEY (namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation)` 26 27 createSchemaVersion = `CREATE TABLE schema_version ( 28 version_num STRING(1024) NOT NULL 29 ) PRIMARY KEY (version_num)` 30 31 createChangelog = `CREATE TABLE changelog ( 32 timestamp TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true), 33 uuid STRING(36) NOT NULL, 34 operation INT64, 35 namespace STRING(1024) NOT NULL, 36 object_id STRING(1024) NOT NULL, 37 relation STRING(1024) NOT NULL, 38 userset_namespace STRING(1024) NOT NULL, 39 userset_object_id STRING(1024) NOT NULL, 40 userset_relation STRING(1024) NOT NULL, 41 ) PRIMARY KEY (timestamp, uuid, operation, namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation)` 42 43 insertEmptyVersion = `INSERT INTO schema_version (version_num) VALUES ('')` 44 45 createReverseQueryIndex = `CREATE INDEX ix_relation_tuple_by_subject ON relation_tuple (userset_object_id, userset_namespace, userset_relation, namespace, relation)` 46 createReverseCheckIndex = `CREATE INDEX ix_relation_tuple_by_subject_relation ON relation_tuple (userset_namespace, userset_relation, namespace, relation)` 47 ) 48 49 func init() { 50 if err := SpannerMigrations.Register("initial", "", func(ctx context.Context, w Wrapper) error { 51 updateOp, err := w.adminClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{ 52 Database: w.client.DatabaseName(), 53 Statements: []string{ 54 createNamespaceConfig, 55 createRelationTuple, 56 createSchemaVersion, 57 createChangelog, 58 createReverseQueryIndex, 59 createReverseCheckIndex, 60 }, 61 }) 62 if err != nil { 63 return err 64 } 65 66 return updateOp.Wait(ctx) 67 }, func(ctx context.Context, rwt *spanner.ReadWriteTransaction) error { 68 _, err := rwt.Update(ctx, spanner.NewStatement(insertEmptyVersion)) 69 return err 70 }); err != nil { 71 panic("failed to register migration: " + err.Error()) 72 } 73 }