spool

package module
v0.0.0-...-80ea082 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 14 Imported by: 0

README

spool

CircleCI

A CLI tool to manage Cloud Spanner databases for testing.

spool

Please feel free to report issues and send pull requests, but note that this application is not officially supported as part of the Cloud Spanner product.

Motivation

When the development of spool started, the Cloud Spanner Emulator wasn't available yet. When using Cloud Spanner instances for continuous integration tests, it is inefficient to create a new test database on every run. This tool lets you reuse test databases in CI tests.

Installation

$ go get -u github.com/cloudspannerecosystem/spool/cmd/spool

Setup

Spool requires a database for metadata to manage databases. The following command sets up the database.

$ spool --project=${PROJECT} --instance=${INSTANCE} --database=${SPOOL_DATABASE} setup

Usage

usage: spool [<flags>] <command> [<args> ...]

A CLI tool to manage Cloud Spanner databases for testing.

Flags:
      --help               Show context-sensitive help (also try --help-long and --help-man).
  -p, --project=PROJECT    Set GCP project ID. (use $SPANNER_PROJECT_ID or $GOOGLE_CLOUD_PROJECT as default value)
  -i, --instance=INSTANCE  Set Cloud Spanner instance name. (use $SPANNER_INSTANCE_ID as default value)
  -d, --database=DATABASE  Set Cloud Spanner database name. (use $SPOOL_SPANNER_DATABASE_ID as default value)
  -s, --schema=SCHEMA      Set schema file path.

Commands:
  help [<command>...]
    Show help.

  setup
    Setup the database for spool metadata.

  create --db-name-prefix=DB-NAME-PREFIX [<flags>]
    Add new databases to the pool.

  get
    Get a idle database from the pool.

  get-or-create --db-name-prefix=DB-NAME-PREFIX
    Get or create a idle database from the pool.

  list [<flags>]
    Print databases.

  put <database>
    Return the database to the pool.

  clean [<flags>]
    Drop all idle databases.

Sample CircleCI configuration

version: 2

jobs:
  build:
    docker:
      - image: golang:1.13-stretch
        environment:
          PROJECT: project
          INSTANCE: instance
          SPOOL_DATABASE: spool
          PATH_TO_SCHEMA_FILE: path/to/schema.sql
          DATABASE_PREFIX: spool
    working_directory: /go/src/github.com/user/repo
    steps:
      - checkout
      - run:
          name: set GitHub token
          command: |
            rm -f ~/.gitconfig
            echo "machine github.com login ${GITHUB_TOKEN}" > ~/.netrc
      - run:
          name: install spool
          command: go get -u github.com/cloudspannerecosystem/spool/cmd/spool
      - run:
          name: get database for testing
          command: |
            DATABASE=$(spool --project=${PROJECT} --instance=${INSTANCE} --database=${SPOOL_DATABASE} --schema=${PATH_TO_SCHEMA_FILE} get-or-create --db-name-prefix=${DATABASE_PREFIX})
            echo "export DATABASE=${DATABASE}" >> ${BASH_ENV}
      - run:
          name: run tests
          command: echo "run your tests with /projects/${PROJECT}/instances/${INSTANCE}/databases/${DATABASE}"
      - run:
          name: release database
          when: always
          command: spool --project=${PROJECT} --instance=${INSTANCE} --database=${SPOOL_DATABASE} --schema=${PATH_TO_SCHEMA_FILE} put ${DATABASE}

  cleanup-old-test-db:
    docker:
      - image: golang:1.13-stretch
        environment:
          PROJECT: project
          INSTANCE: instance
          SPOOL_DATABASE: spool
          PATH_TO_SCHEMA_FILE: path/to/schema.sql
    working_directory: /go/src/github.com/user/repo
    steps:
      - checkout
      - run:
          name: set GitHub token
          command: |
            rm -f ~/.gitconfig
            echo "machine github.com login ${GITHUB_TOKEN}" > ~/.netrc
      - run:
          name: install spool
          command: go get -u github.com/cloudspannerecosystem/spool/cmd/spool
      - run:
          name: cleanup databases
          command: spool --project=${PROJECT} --instance=${INSTANCE} --database=${SPOOL_DATABASE} --schema=${PATH_TO_SCHEMA_FILE} clean --all --force --ignore-used-within-days=7

workflows:
  version: 2
  build-workflow:
    jobs:
      - build:
          context: org-global
  cleanup-workflow:
    triggers:
      - schedule:
          cron: '0 9 * * *'
          filters:
            branches:
              only: master
    jobs:
      - cleanup-old-test-db:
          context: org-global

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanAll

func CleanAll(ctx context.Context, conf *Config, filters ...func(sdb *model.SpoolDatabase) bool) error

CleanAll removes all idle databases.

func FilterNotUsedWithin

func FilterNotUsedWithin(d time.Duration) func(sdb *model.SpoolDatabase) bool

FilterNotUsedWithin returns a function which reports whether sdb is not used within d.

func FilterState

func FilterState(state State) func(sdb *model.SpoolDatabase) bool

FilterState returns a function which reports whether sdb.State is state.

func ListAll

func ListAll(ctx context.Context, conf *Config) ([]*model.SpoolDatabase, error)

ListAll gets all databases from the pool.

func Setup

func Setup(ctx context.Context, conf *Config) error

Setup creates a new spool metadata database.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(projectID, instanceID, databaseID string, opts ...option.ClientOption) *Config

func (*Config) ClientOptions

func (c *Config) ClientOptions() []option.ClientOption

func (*Config) Database

func (c *Config) Database() string

func (*Config) DatabaseID

func (c *Config) DatabaseID() string

func (*Config) Instance

func (c *Config) Instance() string

func (*Config) InstanceID

func (c *Config) InstanceID() string

func (*Config) ProjectID

func (c *Config) ProjectID() string

func (*Config) WithDatabaseID

func (c *Config) WithDatabaseID(databaseID string) *Config

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool represents a Spanner database pool.

func NewPool

func NewPool(ctx context.Context, conf *Config, ddl []byte) (*Pool, error)

NewPool creates a new Pool.

func (*Pool) Clean

func (p *Pool) Clean(ctx context.Context, filters ...func(sdb *model.SpoolDatabase) bool) error

Clean removes all idle databases.

func (*Pool) Create

func (p *Pool) Create(ctx context.Context, dbNamePrefix string) (*model.SpoolDatabase, error)

Create creates a new database and adds to the pool.

func (*Pool) Get

func (p *Pool) Get(ctx context.Context) (*model.SpoolDatabase, error)

Get gets a idle database from the pool.

func (*Pool) GetOrCreate

func (p *Pool) GetOrCreate(ctx context.Context, dbNamePrefix string) (*model.SpoolDatabase, error)

GetOrCreate gets a idle database or creates a new database.

func (*Pool) List

func (p *Pool) List(ctx context.Context) ([]*model.SpoolDatabase, error)

List gets all databases from the pool.

func (*Pool) Put

func (p *Pool) Put(ctx context.Context, dbName string) error

Put adds a database to the pool.

type State

type State int64

State represents a state of the database.

const (
	// StateIdle represents a idle state.
	StateIdle State = iota
	// StateBusy represents a busy state.
	StateBusy
)

func (State) Int64

func (s State) Int64() int64

Int64 returns s as int64.

func (State) String

func (s State) String() string

String returns a string representing the state.

Directories

Path Synopsis
cmd
Code generated by yo.
Code generated by yo.
Package statik contains static assets.
Package statik contains static assets.

Jump to

Keyboard shortcuts

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