github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/spanner/migrations/zz_migration.0003_add_caveats.go (about)

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  
     6  	"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
     7  )
     8  
     9  const (
    10  	createCaveatTable = `CREATE TABLE caveat (
    11      	name STRING(MAX),
    12      	definition BYTES(MAX) NOT NULL,
    13      	timestamp TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
    14  	) PRIMARY KEY (name)`
    15  
    16  	addRelationshipCaveatName = `ALTER TABLE relation_tuple
    17  		ADD COLUMN caveat_name STRING(MAX)`
    18  	addRelationshipCaveatContext = `ALTER TABLE relation_tuple
    19  		ADD COLUMN caveat_context JSON`
    20  
    21  	addChangelogCaveatName = `ALTER TABLE changelog 
    22  		ADD COLUMN caveat_name STRING(MAX)`
    23  	addChangelogCaveatContext = `ALTER TABLE changelog
    24  		ADD COLUMN caveat_context JSON`
    25  )
    26  
    27  func init() {
    28  	if err := SpannerMigrations.Register("add-caveats", "add-metadata-and-counters", func(ctx context.Context, w Wrapper) error {
    29  		updateOp, err := w.adminClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
    30  			Database: w.client.DatabaseName(),
    31  			Statements: []string{
    32  				createCaveatTable,
    33  				addRelationshipCaveatName,
    34  				addRelationshipCaveatContext,
    35  				addChangelogCaveatName,
    36  				addChangelogCaveatContext,
    37  			},
    38  		})
    39  		if err != nil {
    40  			return err
    41  		}
    42  		return updateOp.Wait(ctx)
    43  	}, nil); err != nil {
    44  		panic("failed to register migration: " + err.Error())
    45  	}
    46  }