admin

package
v0.0.0-...-e24534a Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SpannerAdmin

type SpannerAdmin struct {
	Context     context.Context
	Project     string
	Instance    string
	Opts        []option.ClientOption
	AdminClient *database.DatabaseAdminClient
}

SpannerAdmin - Wrapper for Cloud Spanner admin.

func (*SpannerAdmin) AlterDatabase

func (a *SpannerAdmin) AlterDatabase(db string, ddl []string) error

AlterDatabase - Alter a Cloud Spanner database.

Can be used with any valid DDL to add/alter/drop tables or add/alter/drop indexes.

Params:

db string - Name of the Cloud Spanner database to create.
ddl []string - Data Definition Language statements to alter a database.

Return:

error - An error if it occurred.
Example

ExampleSpannerAdmin_AlterDatabase - Example usage for AlterDatabase.

ctx := context.Background()

// Create the admin struct.
spannerAdmin := &SpannerAdmin{
	Context:  ctx,
	Project:  "my-awesome-project-id",
	Instance: "my-awesome-spanner-instance",
}

// Create the admin client.
err := spannerAdmin.CreateAdminClient()
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to create Spanner admin client. Reason - %+v", err)
}

// Some indexes we want to add.
indexDDL := []string{
	`CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)`,
	`CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) STORING (MarketingBudget)`,
}

// Alter the database.
dbErr := spannerAdmin.AlterDatabase("my-awesome-spanner-database", indexDDL)
if dbErr != nil {
	fmt.Fprintf(os.Stderr, "Failed to alter Spanner database. Reason - %+v", dbErr)
}
Output:

func (*SpannerAdmin) CreateAdminClient

func (a *SpannerAdmin) CreateAdminClient() error

CreateAdminClient - Create a new Cloud Spanner admin client.

Return:

error - An error if it occurred.
Example

ExampleSpannerAdmin_CreateAdminClient - Example usage for CreateAdminClient.

ctx := context.Background()

// Create the admin struct.
spannerAdmin := &SpannerAdmin{
	Context:  ctx,
	Project:  "my-awesome-project-id",
	Instance: "my-awesome-spanner-instance",
}

// Create the admin client.
err := spannerAdmin.CreateAdminClient()
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to create Spanner admin client. Reason - %+v", err)
	os.Exit(1)
}
Output:

func (*SpannerAdmin) CreateDatabase

func (a *SpannerAdmin) CreateDatabase(db string, ddl []string) error

CreateDatabase - Create a new Cloud Spanner database.

Params:

db string - Name of the Cloud Spanner database to create.
ddl []string - Data Definition Language statements to apply to the newly
created database.

Return:

error - An error if it occurred.
Example

ExampleSpannerAdmin_CreateDatabase - Example usage for CreateDatabase.

ctx := context.Background()

// Create the admin struct.
spannerAdmin := &SpannerAdmin{
	Context:  ctx,
	Project:  "my-awesome-project-id",
	Instance: "my-awesome-spanner-instance",
}

// Create the admin client.
err := spannerAdmin.CreateAdminClient()
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to create Spanner admin client. Reason - %+v", err)
}

// Some tables to create with the database.
tablesDDL := []string{
	`CREATE TABLE Singers (
			SingerId   INT64 NOT NULL,
			FirstName  STRING(1024),
			LastName   STRING(1024),
			SingerInfo BYTES(MAX)
		) PRIMARY KEY (SingerId)`,
	`CREATE TABLE Albums (
			SingerId	INT64 NOT NULL,
			AlbumId		INT64 NOT NULL,
			AlbumTitle	STRING(MAX),
		) PRIMARY KEY (SingerId, AlbumId),
		INTERLEAVE IN PARENT Singers ON DELETE CASCADE`,
}

// Create the database.
dbErr := spannerAdmin.CreateDatabase("my-awesome-spanner-database", tablesDDL)
if dbErr != nil {
	fmt.Fprintf(os.Stderr, "Failed to create Spanner database. Reason - %+v", dbErr)
}
Output:

Jump to

Keyboard shortcuts

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