mysql

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2020 License: MIT Imports: 14 Imported by: 1

README

MySQL Driver

  • Runs migrations in transactions. That means that if a migration fails, it will be safely rolled back.
  • Tries to return helpful error messages.
  • Stores migration version details in table schema_migrations. This table will be auto-generated.
  • Safe to run concurrently (schema_migrations table is locked during migrations)

Migrations SQL formatting

Each SQL statement MUST end with semicolon (;) FOLLOWED BY NEWLINE ! Whole migration will be executed inside transaction by default. Place SQL between "-- TXBEGIN" and "-- TXEND" comments for custom transaction:

  • you CAN have multiple separate transactions in single migration
  • any SQL not wrapped into TXBEGIN - TXEND will be executed without transaction. Add "-- NOTX" comment above all SQL to disable default transaction. NOTE: it's redundant when TXBEGIN/TXEND is used.

Usage

migrate -url mysql://user@tcp(host:port)/database -path ./db/migrations create add_field_to_table
migrate -url mysql://user@tcp(host:port)/database -path ./db/migrations up
migrate help # for more info

See full DSN (Data Source Name) documentation.

SSL

The MySQL driver will set a TLS config if the following env variables are set:

  • MYSQL_SERVER_CA
  • MYSQL_CLIENT_KEY
  • MYSQL_CLIENT_CERT

TODO: deprecate - library code should not rely on environment variables

Authors

Documentation

Overview

Package mysql implements the Driver interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(url string) (driver.Driver, error)

Open driver

Types

type Driver

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

Driver for MySQL

func (*Driver) Close

func (drv *Driver) Close() error

Close db connection

func (*Driver) Execute

func (drv *Driver) Execute(sql string) error

Execute sql

func (*Driver) Lock

func (drv *Driver) Lock() error

Lock schema_migrations table

func (*Driver) Migrate

func (drv *Driver) Migrate(f file.File) error

Migrate runs migration. It locks schema_migrations table, so concurrent execution is safe.

func (*Driver) Unlock

func (drv *Driver) Unlock() error

Unlock schema_migrations table

func (*Driver) Version

func (drv *Driver) Version() (file.Version, error)

Version returns the current migration version.

func (*Driver) Versions

func (drv *Driver) Versions() (file.Versions, error)

Versions returns the list of applied migrations.

Jump to

Keyboard shortcuts

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