github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/spanner/migrations/zz_migration.0002_add_metadata_and_counters.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  	"github.com/google/uuid"
     9  )
    10  
    11  const (
    12  	createMetadata = `CREATE TABLE metadata (
    13  		unique_id STRING(36),
    14  	) PRIMARY KEY (unique_id)`
    15  
    16  	createCounters = `CREATE TABLE relationship_estimate_counters (
    17  		id BYTES(2) NOT NULL,
    18  		count INT64 NOT NULL
    19  	) PRIMARY KEY (id)`
    20  )
    21  
    22  func init() {
    23  	if err := SpannerMigrations.Register("add-metadata-and-counters", "initial", func(ctx context.Context, w Wrapper) error {
    24  		updateOp, err := w.adminClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
    25  			Database: w.client.DatabaseName(),
    26  			Statements: []string{
    27  				createMetadata,
    28  				createCounters,
    29  			},
    30  		})
    31  		if err != nil {
    32  			return err
    33  		}
    34  
    35  		return updateOp.Wait(ctx)
    36  	}, func(ctx context.Context, rwt *spanner.ReadWriteTransaction) error {
    37  		return rwt.BufferWrite([]*spanner.Mutation{
    38  			spanner.Insert("metadata", []string{"unique_id"}, []any{uuid.NewString()}),
    39  		})
    40  	}); err != nil {
    41  		panic("failed to register migration: " + err.Error())
    42  	}
    43  }