Documentation
¶
Index ¶
- Constants
- func ApplyMigrations(ctx context.Context, migrator Migrator, set *MigrationSet, lastVersion string, ...) error
- func GenerateSQLiteMigration(tb testing.TB, target string, migrations string) []string
- func MigrationChecksum(content []byte) string
- func MigrationSlug(text string) string
- func MigrationVerifyDirectory(set *MigrationSet) (string, func(), error)
- func MigrationVersionOfTime(moment time.Time) string
- func RenderMigrationFile(toolVersion string, generatedAt time.Time, instructions []drivers.Instruction) string
- func RenderMigrationPreview(set *MigrationSet) (string, error)
- func RenderMigrationStatus(set *MigrationSet) string
- func RunMigrationPreview(ctx context.Context, migrator Migrator, set *MigrationSet, output io.Writer) error
- func RunMigrationRepair(ctx context.Context, migrator Migrator, set *MigrationSet, output io.Writer) error
- func StepMigration(ctx context.Context, migrator Migrator, set *MigrationSet, input io.Reader, ...) error
- func WriteMigrationFiles(directory string, slug string, generatedAt time.Time, toolVersion string, ...) ([]string, error)
- func WriteSQLFile(tb testing.TB, directory string, name string, content string) string
- type AppliedMigration
- type Migration
- type MigrationConfig
- type MigrationEntry
- type MigrationSet
- type MigrationState
- type MigrationTransaction
- type Migrator
- type PostgresMigrationTransaction
- type PostgresMigrator
- func (m *PostgresMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
- func (m *PostgresMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
- func (m *PostgresMigrator) ClearDirty(ctx context.Context, migration *Migration) error
- func (m *PostgresMigrator) Close() error
- func (m *PostgresMigrator) DeleteRecord(ctx context.Context, version string) error
- func (m *PostgresMigrator) EnsureHistoryTable(ctx context.Context) error
- func (m *PostgresMigrator) Lock(ctx context.Context) error
- func (m *PostgresMigrator) RecordDirty(ctx context.Context, migration *Migration) error
- func (m *PostgresMigrator) TryLock(ctx context.Context) (bool, error)
- func (m *PostgresMigrator) Unlock(ctx context.Context) error
- func (m *PostgresMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error
- type SQLiteMigrationTransaction
- type SQLiteMigrator
- func (m *SQLiteMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
- func (m *SQLiteMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
- func (m *SQLiteMigrator) ClearDirty(ctx context.Context, migration *Migration) error
- func (m *SQLiteMigrator) Close() error
- func (m *SQLiteMigrator) DeleteRecord(ctx context.Context, version string) error
- func (m *SQLiteMigrator) EnsureHistoryTable(ctx context.Context) error
- func (m *SQLiteMigrator) Lock(ctx context.Context) error
- func (m *SQLiteMigrator) RecordDirty(ctx context.Context, migration *Migration) error
- func (m *SQLiteMigrator) TryLock(ctx context.Context) (bool, error)
- func (m *SQLiteMigrator) Unlock(ctx context.Context) error
- func (m *SQLiteMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error
- type TestingPostgresMigrator
- type TestingSQLiteMigrator
Constants ¶
const DefaultMigrationConfigPath = "dbdiff.yaml"
const PostgresTestConnectionString = "postgres://user:password@localhost:5432/dbdiff?sslmode=disable"
Variables ¶
This section is empty.
Functions ¶
func ApplyMigrations ¶
func GenerateSQLiteMigration ¶
func MigrationChecksum ¶
func MigrationSlug ¶
func MigrationVerifyDirectory ¶
func MigrationVerifyDirectory(set *MigrationSet) (string, func(), error)
func MigrationVersionOfTime ¶
func RenderMigrationFile ¶
func RenderMigrationPreview ¶
func RenderMigrationPreview(set *MigrationSet) (string, error)
func RenderMigrationStatus ¶
func RenderMigrationStatus(set *MigrationSet) string
func RunMigrationPreview ¶
func RunMigrationPreview(ctx context.Context, migrator Migrator, set *MigrationSet, output io.Writer) error
One transaction holds every pending file, because a file can read an object of an earlier file.
func RunMigrationRepair ¶
func RunMigrationRepair(ctx context.Context, migrator Migrator, set *MigrationSet, output io.Writer) error
RunMigrationRepair makes the record agree with the files. A changed row takes the checksum of its file. A missing row and a dirty row go away. An out of order file needs a new generate, so repair does not touch it.
func StepMigration ¶
func WriteMigrationFiles ¶
Types ¶
type AppliedMigration ¶
type MigrationConfig ¶
type MigrationConfig struct {
Driver drivers.DriverName `yaml:"driver"`
Source string `yaml:"source"`
Target string `yaml:"target"`
Schema string `yaml:"schema"`
Version string `yaml:"version"`
}
func ReadMigrationConfig ¶
func ReadMigrationConfig(path string, optional bool) (*MigrationConfig, error)
type MigrationEntry ¶
type MigrationEntry struct {
Migration *Migration
State MigrationState
AppliedAt time.Time
}
func (*MigrationEntry) Content ¶
func (e *MigrationEntry) Content() (string, error)
LoadMigrationSet reads the checksum, and this method reads the file again at the moment of the apply. An edit between the two reads gives a history row that names text that no engine ran.
func (*MigrationEntry) FileName ¶
func (e *MigrationEntry) FileName() string
type MigrationSet ¶
type MigrationSet struct {
Entries []*MigrationEntry
}
func LoadMigrationSet ¶
func NewMigrationSet ¶
func NewMigrationSet(files []*Migration, applied []AppliedMigration) *MigrationSet
func (*MigrationSet) Pending ¶
func (s *MigrationSet) Pending() []*MigrationEntry
func (*MigrationSet) ProblemError ¶
func (s *MigrationSet) ProblemError() error
func (*MigrationSet) Problems ¶
func (s *MigrationSet) Problems() []*MigrationEntry
func (*MigrationSet) RecordError ¶
func (s *MigrationSet) RecordError() error
type MigrationState ¶
type MigrationState string
const ( MigrationApplied MigrationState = "applied" MigrationPending MigrationState = "pending" MigrationChanged MigrationState = "changed" MigrationMissing MigrationState = "missing" MigrationOutOfOrder MigrationState = "out of order" MigrationDirty MigrationState = "dirty" )
type MigrationTransaction ¶
type Migrator ¶
type Migrator interface {
Close() error
EnsureHistoryTable(ctx context.Context) error
AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
Lock(ctx context.Context) error
TryLock(ctx context.Context) (bool, error)
Unlock(ctx context.Context) error
Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
RecordDirty(ctx context.Context, migration *Migration) error
ClearDirty(ctx context.Context, migration *Migration) error
UpdateChecksum(ctx context.Context, migration *Migration) error
DeleteRecord(ctx context.Context, version string) error
}
func NewMigrator ¶
type PostgresMigrationTransaction ¶
type PostgresMigrationTransaction struct {
Executor migrationExecutor
// This field is nil when the file holds the no-transaction directive. Commit and
// Rollback then do nothing, so the apply loop holds one path.
Transaction *sql.Tx
}
func (*PostgresMigrationTransaction) Apply ¶
func (t *PostgresMigrationTransaction) Apply(ctx context.Context, statement string) error
func (*PostgresMigrationTransaction) Commit ¶
func (t *PostgresMigrationTransaction) Commit() error
func (*PostgresMigrationTransaction) Record ¶
func (t *PostgresMigrationTransaction) Record(ctx context.Context, migration *Migration) error
func (*PostgresMigrationTransaction) Rollback ¶
func (t *PostgresMigrationTransaction) Rollback() error
type PostgresMigrator ¶
func NewPostgresMigrator ¶
func NewPostgresMigrator(ctx context.Context, connectionString string, schema string) (*PostgresMigrator, error)
pg_advisory_lock holds a lock of the session, so every statement runs on one connection. A lock that one pooled connection takes, and that another releases, does nothing.
func (*PostgresMigrator) AppliedMigrations ¶
func (m *PostgresMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
A read must stay a read, so this method creates no table. The command status calls it against a database that never ran a migration.
func (*PostgresMigrator) Begin ¶
func (m *PostgresMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
func (*PostgresMigrator) ClearDirty ¶
func (m *PostgresMigrator) ClearDirty(ctx context.Context, migration *Migration) error
func (*PostgresMigrator) Close ¶
func (m *PostgresMigrator) Close() error
func (*PostgresMigrator) DeleteRecord ¶
func (m *PostgresMigrator) DeleteRecord(ctx context.Context, version string) error
func (*PostgresMigrator) EnsureHistoryTable ¶
func (m *PostgresMigrator) EnsureHistoryTable(ctx context.Context) error
func (*PostgresMigrator) RecordDirty ¶
func (m *PostgresMigrator) RecordDirty(ctx context.Context, migration *Migration) error
func (*PostgresMigrator) TryLock ¶
func (m *PostgresMigrator) TryLock(ctx context.Context) (bool, error)
func (*PostgresMigrator) UpdateChecksum ¶
func (m *PostgresMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error
type SQLiteMigrationTransaction ¶
func (*SQLiteMigrationTransaction) Apply ¶
func (t *SQLiteMigrationTransaction) Apply(ctx context.Context, statement string) error
func (*SQLiteMigrationTransaction) Commit ¶
func (t *SQLiteMigrationTransaction) Commit() error
func (*SQLiteMigrationTransaction) Record ¶
func (t *SQLiteMigrationTransaction) Record(ctx context.Context, migration *Migration) error
func (*SQLiteMigrationTransaction) Rollback ¶
func (t *SQLiteMigrationTransaction) Rollback() error
type SQLiteMigrator ¶
func NewSQLiteMigrator ¶
func NewSQLiteMigrator(path string) (*SQLiteMigrator, error)
func (*SQLiteMigrator) AppliedMigrations ¶
func (m *SQLiteMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
func (*SQLiteMigrator) Begin ¶
func (m *SQLiteMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
func (*SQLiteMigrator) ClearDirty ¶
func (m *SQLiteMigrator) ClearDirty(ctx context.Context, migration *Migration) error
func (*SQLiteMigrator) Close ¶
func (m *SQLiteMigrator) Close() error
func (*SQLiteMigrator) DeleteRecord ¶
func (m *SQLiteMigrator) DeleteRecord(ctx context.Context, version string) error
func (*SQLiteMigrator) EnsureHistoryTable ¶
func (m *SQLiteMigrator) EnsureHistoryTable(ctx context.Context) error
func (*SQLiteMigrator) Lock ¶
func (m *SQLiteMigrator) Lock(ctx context.Context) error
A write transaction of SQLite takes the lock of the database already, and a second process then reads the error SQLITE_BUSY. A lock table adds a failure mode and no safety.
func (*SQLiteMigrator) RecordDirty ¶
func (m *SQLiteMigrator) RecordDirty(ctx context.Context, migration *Migration) error
func (*SQLiteMigrator) TryLock ¶
func (m *SQLiteMigrator) TryLock(ctx context.Context) (bool, error)
func (*SQLiteMigrator) UpdateChecksum ¶
func (m *SQLiteMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error
type TestingPostgresMigrator ¶
type TestingPostgresMigrator struct {
*PostgresMigrator
// contains filtered or unexported fields
}
func NewTestPostgresMigrator ¶
func NewTestPostgresMigrator(tb testing.TB) *TestingPostgresMigrator
func (*TestingPostgresMigrator) Schema ¶
func (m *TestingPostgresMigrator) Schema() string
func (*TestingPostgresMigrator) TableNames ¶
func (m *TestingPostgresMigrator) TableNames() []string
type TestingSQLiteMigrator ¶
type TestingSQLiteMigrator struct {
*SQLiteMigrator
Path string
// contains filtered or unexported fields
}
func NewTestSQLiteMigrator ¶
func NewTestSQLiteMigrator(tb testing.TB) *TestingSQLiteMigrator
func (*TestingSQLiteMigrator) TableNames ¶
func (m *TestingSQLiteMigrator) TableNames() []string