remote

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package remote allows for migrations to be run from a remote source, such as S3.

To configure migrations to work with S3, use the migrations package like normal, but call `remote.InitS3`. If you're using spf13/cobra, you might put it in a PersistentPreRun function:

rootCmd = &cobra.Command{
    PersistentPreRun: func(cmd *cobra.Command, args []string) {
        remote.InitS3(viper.GetString("s3.region"))
    },
// ...

Make sure your S3 credentials are defined in ~/.aws/credentials, per the Amazon AWS instructions.

You may copy the SQL migrations to an S3 bucket manually, or use the S3 push function:

if err := remote.PushS3(viper.GetString("migrations),
        viper.GetString("region"), viper.GetString("bucket")); err != nil {
    fmt.Fprintf(os.Stderr, "Failed to push migrations to S3: %s\n", err)
    os.Exit(1)
}

The default "db create" command creates migrations in a local directory, in expectation you'll use a "db push" command based on the above to push the migrations to S3.

After the InitS3 call, you can run the remote migrations the same way you run standard, disk-based migrations, but pass in the bucket name instead of a migrations directory:

conn, err := sql.Open("postgres", "postgres://....")
if err != nil {
    return err
}

if err := migrations.Migrate(conn, viper.GetString("bucket"),
        viper.GetInt("revision")); err != nil {
    fmt.Fprintf(os.Stderr, "Failed to migrate: %s\n", err)
    os.Exit(1)
}

See the remote/cmd package for examples (or feel free to use them in your own spf13/cobra and spf13/viper applications).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPath returned if the migration path is in an unexpected,
	// unparseable format.'
	ErrInvalidPath = errors.New("invalid path; expects bucket/migration")

	// ErrNotFound returned if the object or bucket doesn't exist.
	ErrNotFound = errors.New("not found")
)

Functions

func InitS3

func InitS3(region string) error

InitS3 configures the migration tool to use S3 for migration files, rather than local disk. Expects credentials to be in ~/.aws/credentials.

func PushS3

func PushS3(local, region, bucket string) error

PushS3 pushes the migrations defined in the local directory into the S3 region's bucket.

Types

type S3Reader

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

S3Reader implements the migrations ReadWrite IO interface to support migrations in an S3 bucket.

func NewS3Reader

func NewS3Reader(region string) (*S3Reader, error)

NewS3Reader constructs a new S3 IO interface for SQL migrations.

func (*S3Reader) CreateDirectory

func (s3r *S3Reader) CreateDirectory(bucket string) error

CreateDirectory creates an S3 bucket for the migrations if not already present.

func (*S3Reader) Exists

func (s3r *S3Reader) Exists(bucket, migration string) (time.Time, error)

Exists checks if the migration file exists in the bucket. If it does, returns the size of the migration. If not, returns ErrNotFound.

func (*S3Reader) Files

func (s3r *S3Reader) Files(bucket string) ([]string, error)

Files retrieves the migration names (keys) in the bucket.

func (*S3Reader) PushS3

func (s3r *S3Reader) PushS3(local, bucket string) error

PushS3 will copy the local migration files to the S3 bucket. If the file doesn't exist on S3 or the local timestamp is newer than the S3 timestamp, will push the migration to the S3 bucket.

If the bucket doesn't exist, will create it.

func (*S3Reader) Read

func (s3r *S3Reader) Read(path string) (io.Reader, error)

Read the SQL migration from S3. Expects path to be the bucket/migration file.

func (*S3Reader) WriteMigration

func (s3r *S3Reader) WriteMigration(bucket, filename string, migration []byte) error

WriteMigration writes the migration file to S3. Expects a path for the bucket/migration file.

Directories

Path Synopsis
Package cmd for remote migrations provides some github.com/spf13/cobra commands that may be shared and used in components using the migrations remote package.
Package cmd for remote migrations provides some github.com/spf13/cobra commands that may be shared and used in components using the migrations remote package.

Jump to

Keyboard shortcuts

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