Documentation
¶
Overview ¶
Implements tooling to setup a Spanner database configured for running integration tests against.
Index ¶
- func NewSpannerInstance(ctx context.Context, projectID, instanceID string, opts ...option.ClientOption) error
- func PostgresConnStr(username, password, host, port, database string) string
- type Migrator
- type PostgresContainer
- type PostgresDatabase
- type PostgresMigrator
- type SpannerContainer
- type SpannerDB
- type SpannerMigrator
- func (s *SpannerMigrator) Close() error
- func (s *SpannerMigrator) MigrateDropSchema(ctx context.Context) error
- func (s *SpannerMigrator) MigrateUpData(ctx context.Context, sourceURL string) error
- func (s *SpannerMigrator) MigrateUpSchema(ctx context.Context, sourceURL string) error
- func (s *SpannerMigrator) WithDataMigrationsTable(table string) *SpannerMigrator
- func (s *SpannerMigrator) WithSchemaMigrationsTable(table string) *SpannerMigrator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewSpannerInstance ¶
func NewSpannerInstance(ctx context.Context, projectID, instanceID string, opts ...option.ClientOption) error
NewSpannerInstance creates a spanner instance. This is intended for use with a spanner emulator.
func PostgresConnStr ¶ added in v0.1.2
Types ¶
type Migrator ¶ added in v0.3.0
type Migrator interface {
// MigrateUpSchema applies all up migrations for the database schema.
MigrateUpSchema(ctx context.Context, sourceURL string) error
// MigrateUpData applies all up migrations for the database data.
MigrateUpData(ctx context.Context, sourceURL string) error
// MigrateDropSchema drops the database schema.
MigrateDropSchema(ctx context.Context) error
}
Migrator is an interface for database migration.
type PostgresContainer ¶ added in v0.1.2
type PostgresContainer struct {
testcontainers.Container
// contains filtered or unexported fields
}
PostgresContainer represents a docker container running a postgres instance.
func NewPostgresContainer ¶ added in v0.1.2
func NewPostgresContainer(ctx context.Context, imageVersion string) (*PostgresContainer, error)
NewPostgresContainer returns a new PostgresContainer ready to use with postgres.
func (*PostgresContainer) Close ¶ added in v0.1.2
func (pc *PostgresContainer) Close()
Close closes all connections to the postgres instance
func (*PostgresContainer) CreateDatabase ¶ added in v0.1.2
func (pc *PostgresContainer) CreateDatabase(ctx context.Context, dbName string) (*PostgresDatabase, error)
CreateDatabase creates a new database with the given name and returns a connection to it.
type PostgresDatabase ¶ added in v0.1.2
PostgresDatabase represents a postgres database created and ready for migrations
func NewPostgresDatabase ¶ added in v0.1.2
func NewPostgresDatabase(ctx context.Context, username, password, host, port, databaseToCreate, schemaToCreate string) (*PostgresDatabase, error)
NewPostgresDatabase creates a new database and schema, then connects to it.
func (*PostgresDatabase) Close ¶ added in v0.1.2
func (db *PostgresDatabase) Close()
Close closes the database connection
func (*PostgresDatabase) MigrateDown ¶ added in v0.1.2
func (db *PostgresDatabase) MigrateDown(sourceURL string) error
MigrateDown will migrate all the way down
func (*PostgresDatabase) MigrateUp ¶ added in v0.1.2
func (db *PostgresDatabase) MigrateUp(sourceURL ...string) error
MigrateUp will migrate all the way up, applying all up migrations from all sourceURL's
func (*PostgresDatabase) Schema ¶ added in v0.1.2
func (db *PostgresDatabase) Schema() string
Schema returns the default schema
type PostgresMigrator ¶ added in v0.3.0
type PostgresMigrator struct {
// contains filtered or unexported fields
}
func NewPostgresMigrator ¶ added in v0.3.0
func NewPostgresMigrator(username, password, host, port, database string) *PostgresMigrator
NewPostgresMigrator returns a new PostgresMigrator. It does not attempt to create the database or schema.
func (*PostgresMigrator) MigrateDropSchema ¶ added in v0.3.0
func (p *PostgresMigrator) MigrateDropSchema(_ context.Context) error
FIXME(zredinger): implement this method
func (*PostgresMigrator) MigrateUpData ¶ added in v0.3.0
func (p *PostgresMigrator) MigrateUpData(_ context.Context, sourceURL string) error
FIXME(zredinger): implement this method
func (*PostgresMigrator) MigrateUpSchema ¶ added in v0.3.0
func (p *PostgresMigrator) MigrateUpSchema(_ context.Context, sourceURL string) error
MigrateUp will migrate all the way up, applying all up migrations from the sourceURL
Example ¶
ctx := context.Background()
container, err := NewPostgresContainer(ctx, "latest")
if err != nil {
panic(err)
}
defer container.Close()
db, err := container.CreateDatabase(ctx, "test_db")
if err != nil {
panic(err)
}
defer db.Close()
migrator := NewPostgresMigrator(container.superUsername, "password", container.host, container.port.Port(), db.dbName)
if err := migrator.MigrateUpSchema(ctx, "file://testdata/postgres/migrations"); err != nil {
panic(err)
}
type SpannerContainer ¶
type SpannerContainer struct {
testcontainers.Container
// contains filtered or unexported fields
}
SpannerContainer represents a docker container running a spanner instance. SpannerContainer.Close should be called to cleanup resources.
func NewSpannerContainer ¶
func NewSpannerContainer(ctx context.Context, imageVersion string) (*SpannerContainer, error)
NewSpannerContainer returns a initialized SpannerContainer ready to run to create databases for unit tests. SpannerContainer.Close should be called to cleanup resources.
func (*SpannerContainer) Close ¶
func (sc *SpannerContainer) Close() error
Close cleans up open resources
func (*SpannerContainer) CreateDatabase ¶ added in v0.2.0
CreateDatabase creates a database with dbName. Each test should create their own database for testing
Example ¶
ctx := context.Background()
container, err := NewSpannerContainer(ctx, "latest")
if err != nil {
panic(err)
}
defer func() {
closeErr := container.Close()
log.Println("Error closing container:", closeErr)
}()
db, err := container.CreateDatabase(ctx, "test_db")
if err != nil {
panic(err)
}
defer func() {
closeErr := db.Close()
log.Println("Error closing database:", closeErr)
}()
// Use the database..
db.Single().Query(ctx, spanner.NewStatement("SELECT 1"))
type SpannerDB ¶
SpannerDB represents a database created and ready for migrations
func NewSpannerDatabase ¶
func NewSpannerDatabase(ctx context.Context, projectID, instanceID, dbName string, opts ...option.ClientOption) (*SpannerDB, error)
NewSpannerDatabase will create a spanner database
func (*SpannerDB) MigrateDown ¶
MigrateDown will migrate all the way down
type SpannerMigrator ¶ added in v0.3.0
type SpannerMigrator struct {
// contains filtered or unexported fields
}
SpannerMigrator handles connecting to an existing spanner database and running migrations
func NewSpannerMigrator ¶ added in v0.3.0
func NewSpannerMigrator(ctx context.Context, projectID, instanceID, dbName string, opts ...option.ClientOption) (*SpannerMigrator, error)
NewSpannerMigrator connects to an existing spanner database and returns a SpannerMigrator
Uses the following tables by default to store migration versions:
- Data Migrations table: "DataMigrations"
- Schema Migrations table: "SchemaMigrations"
func (*SpannerMigrator) Close ¶ added in v0.3.0
func (s *SpannerMigrator) Close() error
Close cleans up resources
func (*SpannerMigrator) MigrateDropSchema ¶ added in v0.3.0
func (s *SpannerMigrator) MigrateDropSchema(ctx context.Context) error
MigrateDropSchema drops all objects in the schema
This happens in the following order:
- Drop views
- Drop FK constraints
- Drop Search Indexes
- Drop Indexes
- Drop tables
func (*SpannerMigrator) MigrateUpData ¶ added in v0.3.0
func (s *SpannerMigrator) MigrateUpData(ctx context.Context, sourceURL string) error
MigrateUpData will apply all data migrations from the sourceURL
Use for DML migrations
Example ¶
ctx := context.Background()
container, err := NewSpannerContainer(ctx, "latest")
if err != nil {
panic(err)
}
defer func() {
closeErr := container.Close()
log.Println("Error closing container:", closeErr)
}()
db, err := container.CreateDatabase(ctx, "test_db")
if err != nil {
panic(err)
}
defer func() {
closeErr := db.Close()
log.Println("Error closing database:", closeErr)
}()
migrator, err := NewSpannerMigrator(ctx, container.projectID, container.instanceID, "test_db", container.opts...)
if err != nil {
panic(err)
}
if err := migrator.MigrateUpData(ctx, "file://testdata/spanner/migrations"); err != nil {
panic(err)
}
func (*SpannerMigrator) MigrateUpSchema ¶ added in v0.3.0
func (s *SpannerMigrator) MigrateUpSchema(ctx context.Context, sourceURL string) error
MigrateUpSchema will migrate all the way up, applying all up migrations from the sourceURL
Use for DDL migrations
Example ¶
ctx := context.Background()
container, err := NewSpannerContainer(ctx, "latest")
if err != nil {
panic(err)
}
defer func() {
closeErr := container.Close()
log.Println("Error closing container:", closeErr)
}()
db, err := container.CreateDatabase(ctx, "test_db")
if err != nil {
panic(err)
}
defer func() {
closeErr := db.Close()
log.Println("Error closing database:", closeErr)
}()
migrator, err := NewSpannerMigrator(ctx, container.projectID, container.instanceID, "test_db", container.opts...)
if err != nil {
panic(err)
}
if err := migrator.MigrateUpSchema(ctx, "file://testdata/spanner/migrations"); err != nil {
panic(err)
}
func (*SpannerMigrator) WithDataMigrationsTable ¶ added in v0.3.0
func (s *SpannerMigrator) WithDataMigrationsTable(table string) *SpannerMigrator
WithDataMigrationsTable allows setting the data migration table to be used
func (*SpannerMigrator) WithSchemaMigrationsTable ¶ added in v0.3.0
func (s *SpannerMigrator) WithSchemaMigrationsTable(table string) *SpannerMigrator
WithSchemaMigrationsTable allows setting the schema migration table to be used