Documentation
¶
Index ¶
- func Computed(name string, action func(context.Context, libschema.MyLogger, *sql.Tx) error, ...) libschema.Migration
- func Generate(name string, ...) libschema.Migration
- func Script(name string, sqlText string, opts ...libschema.MigrationOption) libschema.Migration
- type CheckResult
- type MySQL
- func (m *MySQL) ColumnDefault(table, column string) (*string, error)
- func (p *MySQL) CreateSchemaTableIfNotExists(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) error
- func (m *MySQL) DatabaseName() (string, error)
- func (p *MySQL) DoOneMigration(ctx context.Context, log libschema.MyLogger, d *libschema.Database, ...) (err error)
- func (m *MySQL) DoesColumnExist(table, column string) (bool, error)
- func (m *MySQL) GetTableConstraint(table, constraintName string) (string, bool, error)
- func (m *MySQL) HasPrimaryKey(table string) (bool, error)
- func (p *MySQL) IsMigrationSupported(d *libschema.Database, _ libschema.MyLogger, migration libschema.Migration) error
- func (p *MySQL) LoadStatus(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) ([]libschema.MigrationName, error)
- func (p *MySQL) LockMigrationsTable(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) error
- func (m *MySQL) TableHasIndex(table, indexName string) (bool, error)
- func (p *MySQL) UnlockMigrationsTable(_ libschema.MyLogger) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Computed ¶
func Computed( name string, action func(context.Context, libschema.MyLogger, *sql.Tx) error, opts ...libschema.MigrationOption) libschema.Migration
Computed creates a libschema.Migration from a Go function to run the migration directly.
Types ¶
type CheckResult ¶
type CheckResult string
const ( Safe CheckResult = "safe" DataAndDDL CheckResult = "dataAndDDL" NonIdempotentDDL CheckResult = "nonIdempotentDDL" )
func CheckScript ¶
func CheckScript(s string) CheckResult
CheckScript attempts to validate that an SQL command does not do both schema changes (DDL) and data changes.
type MySQL ¶
type MySQL struct {
// contains filtered or unexported fields
}
MySQL is a libschema.Driver for connecting to MySQL-like databases that have the following characteristics: * CANNOT do DDL commands inside transactions * Support UPSERT using INSERT ... ON DUPLICATE KEY UPDATE * uses /* -- and # for comments
Because mysql DDL commands cause transactions to autocommit, tracking the schema changes in a secondary table (like libschema does) is inherently unsafe. The MySQL driver will record that it is about to attempt a migration and it will record if that attempts succeeds or fails, but if the program terminates mid-transaction, it is beyond the scope of libschema to determine if the transaction succeeded or failed. Such transactions will be retried. For this reason, it is reccomend that DDL commands be written such that they are idempotent.
func New ¶
func New(log libschema.MyLogger, name string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, *MySQL, error)
New creates a libschema.Database with a mysql driver built in.
func (*MySQL) ColumnDefault ¶
ColumnDefault returns the default value for a column. If there is no default value, then nil is returned.
func (*MySQL) CreateSchemaTableIfNotExists ¶
func (p *MySQL) CreateSchemaTableIfNotExists(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) error
CreateSchemaTableIfNotExists creates the migration tracking table for libschema. It is expected to be called by libschema.
func (*MySQL) DatabaseName ¶
DatabaseName returns the name of the current database (aka schema for MySQL)
func (*MySQL) DoOneMigration ¶
func (p *MySQL) DoOneMigration(ctx context.Context, log libschema.MyLogger, d *libschema.Database, m libschema.Migration) (err error)
DoOneMigration applies a single migration. It is expected to be called by libschema.
func (*MySQL) DoesColumnExist ¶
DoesColumnExist returns true if the column exists
func (*MySQL) GetTableConstraint ¶
GetTableConstraints returns the type of constraint and if it is enforced.
func (*MySQL) HasPrimaryKey ¶
HasPrimaryKey returns true if the table has a primary key
func (*MySQL) IsMigrationSupported ¶
func (p *MySQL) IsMigrationSupported(d *libschema.Database, _ libschema.MyLogger, migration libschema.Migration) error
IsMigrationSupported checks to see if a migration is well-formed. Absent a code change, this should always return nil. It is expected to be called by libschema.
func (*MySQL) LoadStatus ¶
func (p *MySQL) LoadStatus(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) ([]libschema.MigrationName, error)
LoadStatus loads the current status of all migrations from the migration tracking table. It is expected to be called by libschema.
func (*MySQL) LockMigrationsTable ¶
func (p *MySQL) LockMigrationsTable(ctx context.Context, _ libschema.MyLogger, d *libschema.Database) error
LockMigrationsTable locks the migration tracking table for exclusive use by the migrations running now. It is expected to be called by libschema. In MySQL, locks are _not_ tied to transactions so closing the transaction does not release the lock. We'll use a transaction just to make sure that we're using the same connection. If LockMigrationsTable succeeds, be sure to call UnlockMigrationsTable.
func (*MySQL) TableHasIndex ¶
TableHasIndex returns true if there is an index matching the name given.
