Documentation
¶
Index ¶
- func Computed(name string, action func(context.Context, *sql.Tx) error, ...) libschema.Migration
- func Generate(name string, generator func(context.Context, *sql.Tx) string, ...) libschema.Migration
- func Script(name string, sqlText string, opts ...libschema.MigrationOption) libschema.Migration
- type CheckResult
- type MySQL
- func (p *MySQL) ColumnDefault(table, column string) (*string, error)
- func (p *MySQL) CreateSchemaTableIfNotExists(ctx context.Context, _ *internal.Log, d *libschema.Database) error
- func (m *MySQL) DatabaseName() (string, error)
- func (p *MySQL) DoOneMigration(ctx context.Context, log *internal.Log, d *libschema.Database, ...) (result sql.Result, err error)
- func (p *MySQL) DoesColumnExist(table, column string) (bool, error)
- func (p *MySQL) GetTableConstraint(table, constraintName string) (string, bool, error)
- func (p *MySQL) HasPrimaryKey(table string) (bool, error)
- func (p *MySQL) IsMigrationSupported(d *libschema.Database, _ *internal.Log, migration libschema.Migration) error
- func (p *MySQL) LoadStatus(ctx context.Context, _ *internal.Log, d *libschema.Database) ([]libschema.MigrationName, error)
- func (p *MySQL) LockMigrationsTable(ctx context.Context, _ *internal.Log, d *libschema.Database) error
- func (p *MySQL) TableHasIndex(table, indexName string) (bool, error)
- func (p *MySQL) UnlockMigrationsTable(_ *internal.Log) error
- func (m *MySQL) UseDatabase(name string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Computed ¶
func Computed( name string, action func(context.Context, *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.
There are methods the MySQL type that can be used to query the state of the database and thus transform DDL commands that are not idempotent (like CREATE INDEX) into idempotent commands by only running them if they need to be run.
Because Go's database/sql uses connection pooling and the mysql "USE database" command leaks out of transactions, it is strongly recommended that the libschema.Option value of SchemaOverride be set when creating the libschema.Schema object. That SchemaOverride will be propagated into the MySQL object and be used as a default table for all of the functions to interrogate data defintion status.
func New ¶
func New(log *internal.Log, 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. The table is assumed to be in the current database unless m.UseDatabase() has been called.
func (*MySQL) CreateSchemaTableIfNotExists ¶
func (p *MySQL) CreateSchemaTableIfNotExists(ctx context.Context, _ *internal.Log, 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). A call to UseDatabase() overrides all future calls to DatabaseName(). If the MySQL object was created from a libschema.Schema that had SchemaOverride set then this will return whatever that value was. It is reccomened that UseDatabase() be called to make sure that the right database is returned.
func (*MySQL) DoOneMigration ¶
func (p *MySQL) DoOneMigration(ctx context.Context, log *internal.Log, d *libschema.Database, m libschema.Migration) (result sql.Result, 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 The table is assumed to be in the current database unless m.UseDatabase() has been called.
func (*MySQL) GetTableConstraint ¶
GetTableConstraints returns the type of constraint and if it is enforced. The table is assumed to be in the current database unless m.UseDatabase() has been called.
func (*MySQL) HasPrimaryKey ¶
HasPrimaryKey returns true if the table has a primary key The table is assumed to be in the current database unless m.UseDatabase() has been called.
func (*MySQL) IsMigrationSupported ¶
func (p *MySQL) IsMigrationSupported(d *libschema.Database, _ *internal.Log, 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, _ *internal.Log, 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, _ *internal.Log, 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. The table is assumed to be in the current database unless m.UseDatabase() has been called.
func (*MySQL) UnlockMigrationsTable ¶
UnlockMigrationsTable unlocks the migration tracking table. It is expected to be called by libschema.
func (*MySQL) UseDatabase ¶ added in v0.0.2
UseDatabase() overrides the default database for DatabaseName(), ColumnDefault(), HasPrimaryKey(), HasTableIndex(), DoesColumnExist(), and GetTableConstraint(). If name is empty then the override is removed and the database will be queried from the mysql server. Due to connection pooling in Go, that's a bad idea.
