spanneraccessor

package
v1.0.1-0...-b7ad348 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright 2024 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Set the maximum number of concurrent workers during foreign key creation.
	// This number should not be too high so as to not hit the AdminQuota limit.
	// AdminQuota limits are mentioned here: https://cloud.google.com/spanner/quotas#administrative_limits
	// If facing a quota limit error, consider reducing this value.
	MaxWorkers = 50
)

Functions

This section is empty.

Types

type SpannerAccessor

type SpannerAccessor interface {
	// Fetch the dialect of the spanner database.
	GetDatabaseDialect(ctx context.Context, dbURI string) (string, error)
	// CheckExistingDb checks whether the database with dbURI exists or not.
	// If API call doesn't respond then user is informed after every 5 minutes on command line.
	CheckExistingDb(ctx context.Context, dbURI string) (bool, error)
	// Create a database with no schema.
	CreateEmptyDatabase(ctx context.Context, dbURI string) error
	// Fetch the leader of the Spanner instance.
	GetSpannerLeaderLocation(ctx context.Context, instanceURI string) (string, error)
	// Check if a change stream already exists.
	CheckIfChangeStreamExists(ctx context.Context, changeStreamName, dbURI string) (bool, error)
	// Validate that change stream option 'VALUE_CAPTURE_TYPE' is 'NEW_ROW'.
	ValidateChangeStreamOptions(ctx context.Context, changeStreamName, dbURI string) error
	// Create a change stream with default options.
	CreateChangeStream(ctx context.Context, changeStreamName, dbURI string) error
	// Create new Database using conv
	CreateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string) error
	// Update Database using conv
	UpdateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string) error
	// Updates an existing Spanner database or create a new one if one does not exist using Conv
	CreateOrUpdateDatabase(ctx context.Context, dbURI, driver string, conv *internal.Conv, migrationType string) error
	// Check whether the db exists and if it does, verify if the schema is what we currently support.
	VerifyDb(ctx context.Context, dbURI string) (dbExists bool, err error)
	// Verify if an existing DB's ddl follows what is supported by Spanner migration tool. Currently, we only support empty schema when db already exists.
	ValidateDDL(ctx context.Context, dbURI string) error
	// UpdateDDLForeignKeys updates the Spanner database with foreign key constraints using ALTER TABLE statements.
	UpdateDDLForeignKeys(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string)
	// Deletes a database.
	DropDatabase(ctx context.Context, dbURI string) error
	//Runs a query against the provided spanner database and returns if the executed DML is validate or not
	ValidateDML(ctx context.Context, query string) (bool, error)
}

The SpannerAccessor provides methods that internally use a spanner client (can be adminClient/databaseclient/instanceclient etc). Methods should only contain generic logic here that can be used by multiple workflows.

type SpannerAccessorImpl

type SpannerAccessorImpl struct {
	InstanceClient spinstanceadmin.InstanceAdminClient
	AdminClient    spanneradmin.AdminClient
	SpannerClient  spannerclient.SpannerClient
}

This implements the SpannerAccessor interface. This is the primary implementation that should be used in all places other than tests.

func NewSpannerAccessorClientImpl

func NewSpannerAccessorClientImpl(ctx context.Context) (*SpannerAccessorImpl, error)

func NewSpannerAccessorClientImplWithSpannerClient

func NewSpannerAccessorClientImplWithSpannerClient(ctx context.Context, dbURI string) (*SpannerAccessorImpl, error)

func (*SpannerAccessorImpl) CheckExistingDb

func (sp *SpannerAccessorImpl) CheckExistingDb(ctx context.Context, dbURI string) (bool, error)

func (*SpannerAccessorImpl) CheckIfChangeStreamExists

func (sp *SpannerAccessorImpl) CheckIfChangeStreamExists(ctx context.Context, changeStreamName, dbURI string) (bool, error)

Consider using a CreateChangestream operation and check for alreadyExists error. That uses adminClient which can be unit tested.

func (*SpannerAccessorImpl) CreateChangeStream

func (sp *SpannerAccessorImpl) CreateChangeStream(ctx context.Context, changeStreamName, dbURI string) error

func (*SpannerAccessorImpl) CreateDatabase

func (sp *SpannerAccessorImpl) CreateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string) error

CreateDatabase returns a newly create Spanner DB. It automatically determines an appropriate project, selects a Spanner instance to use, generates a new Spanner DB name, and call into the Spanner admin interface to create the new DB.

func (*SpannerAccessorImpl) CreateEmptyDatabase

func (sp *SpannerAccessorImpl) CreateEmptyDatabase(ctx context.Context, dbURI string) error

func (*SpannerAccessorImpl) CreateOrUpdateDatabase

func (sp *SpannerAccessorImpl) CreateOrUpdateDatabase(ctx context.Context, dbURI, driver string, conv *internal.Conv, migrationType string) error

CreatesOrUpdatesDatabase updates an existing Spanner database or creates a new one if one does not exist.

func (*SpannerAccessorImpl) DropDatabase

func (sp *SpannerAccessorImpl) DropDatabase(ctx context.Context, dbURI string) error

func (*SpannerAccessorImpl) GetDatabaseDialect

func (sp *SpannerAccessorImpl) GetDatabaseDialect(ctx context.Context, dbURI string) (string, error)

func (*SpannerAccessorImpl) GetSpannerLeaderLocation

func (sp *SpannerAccessorImpl) GetSpannerLeaderLocation(ctx context.Context, instanceURI string) (string, error)

func (*SpannerAccessorImpl) Refresh

func (sp *SpannerAccessorImpl) Refresh(ctx context.Context, dbURI string)

func (*SpannerAccessorImpl) UpdateDDLForeignKeys

func (sp *SpannerAccessorImpl) UpdateDDLForeignKeys(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string)

UpdateDDLForeignKeys updates the Spanner database with foreign key constraints using ALTER TABLE statements.

func (*SpannerAccessorImpl) UpdateDatabase

func (sp *SpannerAccessorImpl) UpdateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string) error

UpdateDatabase updates an existing spanner database.

func (*SpannerAccessorImpl) ValidateChangeStreamOptions

func (sp *SpannerAccessorImpl) ValidateChangeStreamOptions(ctx context.Context, changeStreamName, dbURI string) error

func (*SpannerAccessorImpl) ValidateDDL

func (sp *SpannerAccessorImpl) ValidateDDL(ctx context.Context, dbURI string) error

ValidateDDL verifies if an existing DB's ddl follows what is supported by Spanner migration tool. Currently, we only support empty schema when db already exists.

func (*SpannerAccessorImpl) ValidateDML

func (sp *SpannerAccessorImpl) ValidateDML(ctx context.Context, query string) (bool, error)

func (*SpannerAccessorImpl) VerifyDb

func (sp *SpannerAccessorImpl) VerifyDb(ctx context.Context, dbURI string) (dbExists bool, err error)

VerifyDb checks whether the db exists and if it does, verifies if the schema is what we currently support.

type SpannerAccessorMock

type SpannerAccessorMock struct {
	GetDatabaseDialectMock          func(ctx context.Context, dbURI string) (string, error)
	CheckExistingDbMock             func(ctx context.Context, dbURI string) (bool, error)
	CreateEmptyDatabaseMock         func(ctx context.Context, dbURI string) error
	GetSpannerLeaderLocationMock    func(ctx context.Context, instanceURI string) (string, error)
	CheckIfChangeStreamExistsMock   func(ctx context.Context, changeStreamName, dbURI string) (bool, error)
	ValidateChangeStreamOptionsMock func(ctx context.Context, changeStreamName, dbURI string) error
	CreateChangeStreamMock          func(ctx context.Context, changeStreamName, dbURI string) error
	CreateDatabaseMock              func(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string) error
	UpdateDatabaseMock              func(ctx context.Context, dbURI string, conv *internal.Conv, driver string) error
	CreateOrUpdateDatabaseMock      func(ctx context.Context, dbURI, driver string, conv *internal.Conv, migrationType string) error
	VerifyDbMock                    func(ctx context.Context, dbURI string) (dbExists bool, err error)
	ValidateDDLMock                 func(ctx context.Context, dbURI string) error
	UpdateDDLForeignKeysMock        func(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string)
	DropDatabaseMock                func(ctx context.Context, dbURI string) error
	ValidateDMLMock                 func(ctx context.Context, query string) (bool, error)
}

Mock that implements the SpannerAccessor interface. Pass in unit tests where SpannerAccessor is an input parameter.

func (*SpannerAccessorMock) CheckExistingDb

func (sam *SpannerAccessorMock) CheckExistingDb(ctx context.Context, dbURI string) (bool, error)

func (*SpannerAccessorMock) CheckIfChangeStreamExists

func (sam *SpannerAccessorMock) CheckIfChangeStreamExists(ctx context.Context, changeStreamName, dbURI string) (bool, error)

func (*SpannerAccessorMock) CreateChangeStream

func (sam *SpannerAccessorMock) CreateChangeStream(ctx context.Context, changeStreamName, dbURI string) error

func (*SpannerAccessorMock) CreateDatabase

func (sam *SpannerAccessorMock) CreateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string) error

func (*SpannerAccessorMock) CreateEmptyDatabase

func (sam *SpannerAccessorMock) CreateEmptyDatabase(ctx context.Context, dbURI string) error

func (*SpannerAccessorMock) CreateOrUpdateDatabase

func (sam *SpannerAccessorMock) CreateOrUpdateDatabase(ctx context.Context, dbURI, driver string, conv *internal.Conv, migrationType string) error

func (*SpannerAccessorMock) DropDatabase

func (sam *SpannerAccessorMock) DropDatabase(ctx context.Context, dbURI string) error

DropDatabase implements SpannerAccessor.

func (*SpannerAccessorMock) GetDatabaseDialect

func (sam *SpannerAccessorMock) GetDatabaseDialect(ctx context.Context, dbURI string) (string, error)

func (*SpannerAccessorMock) GetSpannerLeaderLocation

func (sam *SpannerAccessorMock) GetSpannerLeaderLocation(ctx context.Context, instanceURI string) (string, error)

func (*SpannerAccessorMock) UpdateDDLForeignKeys

func (sam *SpannerAccessorMock) UpdateDDLForeignKeys(ctx context.Context, dbURI string, conv *internal.Conv, driver string, migrationType string)

func (*SpannerAccessorMock) UpdateDatabase

func (sam *SpannerAccessorMock) UpdateDatabase(ctx context.Context, dbURI string, conv *internal.Conv, driver string) error

func (*SpannerAccessorMock) ValidateChangeStreamOptions

func (sam *SpannerAccessorMock) ValidateChangeStreamOptions(ctx context.Context, changeStreamName, dbURI string) error

func (*SpannerAccessorMock) ValidateDDL

func (sam *SpannerAccessorMock) ValidateDDL(ctx context.Context, dbURI string) error

func (*SpannerAccessorMock) ValidateDML

func (sam *SpannerAccessorMock) ValidateDML(ctx context.Context, query string) (bool, error)

ValidateDML implements SpannerAccessor.

func (*SpannerAccessorMock) VerifyDb

func (sam *SpannerAccessorMock) VerifyDb(ctx context.Context, dbURI string) (dbExists bool, err error)

Jump to

Keyboard shortcuts

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