spanner

package
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultProjectID is the default project ID for the Spanner container.
	DefaultProjectID = "test-project"
)

Variables

View Source
var WithProjectID = shared.WithProjectID

WithProjectID re-exports the common GCloud WithProjectID option

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the Spanner container type used in the module

func Run

Run creates an instance of the Spanner GCloud container type. The URI uses the empty string as the protocol.

Example
// runSpannerContainer {
ctx := context.Background()

spannerContainer, err := tcspanner.Run(
	ctx,
	"gcr.io/cloud-spanner-emulator/emulator:1.4.0",
	tcspanner.WithProjectID("spanner-project"),
)
defer func() {
	if err := testcontainers.TerminateContainer(spannerContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to run container: %v", err)
	return
}
// }

// spannerAdminClient {
projectID := spannerContainer.ProjectID()

const (
	instanceID   = "test-instance"
	databaseName = "test-db"
)

options := []option.ClientOption{
	option.WithEndpoint(spannerContainer.URI()),
	option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
	option.WithoutAuthentication(),
	internaloption.SkipDialSettingsValidation(),
}

instanceAdmin, err := instance.NewInstanceAdminClient(ctx, options...)
if err != nil {
	log.Printf("failed to create instance admin client: %v", err)
	return
}
defer instanceAdmin.Close()
// }

instanceOp, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
	Parent:     "projects/" + projectID,
	InstanceId: instanceID,
	Instance: &instancepb.Instance{
		DisplayName: instanceID,
	},
})
if err != nil {
	log.Printf("failed to create instance: %v", err)
	return
}

_, err = instanceOp.Wait(ctx)
if err != nil {
	log.Printf("failed to wait for instance creation: %v", err)
	return
}

// spannerDBAdminClient {
c, err := database.NewDatabaseAdminClient(ctx, options...)
if err != nil {
	log.Printf("failed to create admin client: %v", err)
	return
}
defer c.Close()
// }

databaseOp, err := c.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
	Parent:          fmt.Sprintf("projects/%s/instances/%s", projectID, instanceID),
	CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", databaseName),
	ExtraStatements: []string{
		"CREATE TABLE Languages (Language STRING(MAX), Mascot STRING(MAX)) PRIMARY KEY (Language)",
	},
})
if err != nil {
	log.Printf("failed to create database: %v", err)
	return
}
_, err = databaseOp.Wait(ctx)
if err != nil {
	log.Printf("failed to wait for database creation: %v", err)
	return
}

db := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectID, instanceID, databaseName)
client, err := spanner.NewClient(ctx, db, options...)
if err != nil {
	log.Printf("failed to create client: %v", err)
	return
}
defer client.Close()

_, err = client.Apply(ctx, []*spanner.Mutation{
	spanner.Insert("Languages",
		[]string{"language", "mascot"},
		[]any{"Go", "Gopher"}),
})
if err != nil {
	log.Printf("failed to apply mutation: %v", err)
	return
}
row, err := client.Single().ReadRow(ctx, "Languages",
	spanner.Key{"Go"}, []string{"mascot"})
if err != nil {
	log.Printf("failed to read row: %v", err)
	return
}

var mascot string
err = row.ColumnByName("Mascot", &mascot)
if err != nil {
	log.Printf("failed to read column: %v", err)
	return
}

fmt.Println(mascot)
Output:

Gopher

func (*Container) ProjectID

func (c *Container) ProjectID() string

ProjectID returns the project ID of the Spanner container.

func (*Container) URI

func (c *Container) URI() string

URI returns the URI of the Spanner container.

type Option

type Option = shared.Option

Option aliases the common GCloud option type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL