mongodb

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 15 Imported by: 0

README

MongoDB

  • Driver work with mongo through db.runCommands
  • Migrations support json format. It contains array of commands for db.runCommand. Every command is executed in separate request to database
  • All keys have to be in quotes "
  • Examples

Usage

mongodb://user:password@host:port/dbname?query (mongodb+srv:// also works, but behaves a bit differently. See docs for more information)

URL Query WithInstance Config Description
x-migrations-collection MigrationsCollection Name of the migrations collection
x-transaction-mode TransactionMode If set to true wrap commands in transaction. Available only for replica set. Driver is using strconv.ParseBool for parsing
x-advisory-locking true Feature flag for advisory locking, if set to false, disable advisory locking
x-advisory-lock-collection migrate_advisory_lock The name of the collection to use for advisory locking.
x-advisory-lock-timeout 15 The max time in seconds that migrate will wait to acquire a lock before failing.
x-advisory-lock-timeout-interval 10 The max time in seconds between attempts to acquire the advisory lock, the lock is attempted to be acquired using an exponential backoff algorithm.
dbname DatabaseName The name of the database to connect to
user The user to sign in as. Can be omitted
password The user's password. Can be omitted
host The host to connect to
port The port to bind to

Documentation

Index

Constants

View Source
const DefaultAdvisoryLockingFlag = true // the default value for the advisory locking feature flag. Default is true.
View Source
const DefaultLockTimeout = 15 // the default maximum time to wait for a lock to be released.
View Source
const DefaultLockTimeoutInterval = 10 // the default maximum intervals time for the locking timout.
View Source
const DefaultLockingCollection = "migrate_advisory_lock" // the collection to use for advisory locking by default.
View Source
const LockIndexName = "lock_unique_key" // the name of the index which adds unique constraint to the locking_key field.

Variables

View Source
var (
	ErrNoDatabaseName            = fmt.Errorf("no database name")
	ErrNilConfig                 = fmt.Errorf("no config")
	ErrLockTimeoutConfigConflict = fmt.Errorf("both x-advisory-lock-timeout-interval and x-advisory-lock-timout-interval were specified")
)
View Source
var DefaultMigrationsCollection = "schema_migrations"

Functions

func WithInstance

func WithInstance(instance *mongo.Client, config *Config) (database.Driver, error)

Types

type Config

type Config struct {
	DatabaseName         string
	MigrationsCollection string
	TransactionMode      bool
	Locking              Locking
}

type Locking

type Locking struct {
	CollectionName string
	Timeout        int
	Enabled        bool
	Interval       int
}

type Mongo

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

func (*Mongo) Close

func (m *Mongo) Close() error

func (*Mongo) Drop

func (m *Mongo) Drop() error

func (*Mongo) Lock

func (m *Mongo) Lock() error

Utilizes advisory locking on the config.LockingCollection collection This uses a unique index on the `locking_key` field.

func (*Mongo) Open

func (m *Mongo) Open(dsn string) (database.Driver, error)

func (*Mongo) Run

func (m *Mongo) Run(migration io.Reader) error

func (*Mongo) SetVersion

func (m *Mongo) SetVersion(version int, dirty bool) error

func (*Mongo) Unlock

func (m *Mongo) Unlock() error

func (*Mongo) Version

func (m *Mongo) Version() (version int, dirty bool, err error)

Jump to

Keyboard shortcuts

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