Documentation
¶
Overview ¶
Package pomegranate implements helper functions and a CLI (pmg) for creating and safely running SQL migrations.
Go projects can use pomegranate to ingest .sql files into a Go binary, and run them from there. See the README.md on Github for examples and explanations.
Index ¶
- func Connect(dial string) (*sql.DB, error)
- func FakeMigrateForwardTo(name string, db *sql.DB, allMigrations []Migration, confirm bool) error
- func FromEmbed(f embed.FS, prefix string) fs.ReadDirFS
- func IngestMigrations(dir, goFile, packageName string, generateTag bool) error
- func InitMigration(dir string) error
- func InitMigrationTimestamp(dir string, timestamp time.Time) error
- func MigrateBackwardTo(name string, db *sql.DB, allMigrations []Migration, confirm bool) error
- func MigrateForwardTo(name string, db *sql.DB, allMigrations []Migration, confirm bool) error
- func NewMigration(dir, name string) error
- func NewMigrationTimestamp(dir, name string, timestamp time.Time) error
- type Migration
- type MigrationLogRecord
- type MigrationRecord
- type OsDir
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect calls sql.Open for you, specifying the Postgres driver and printing the DB name and host to stdout so you can check that you're connecting to the right place before continuing. dial MUST be in URL form.
func FakeMigrateForwardTo ¶
FakeMigrateForwardTo will record all forward migrations that have not yet been run in the migration_state table, up to and including the one specified by `name`, without actually running their ForwardSQL. To fake all un-run migrations, set `name` to an empty string.
func FromEmbed ¶
FromEmbed is able to convert a embed.FS into something more useful.
When you do
//go:embed some/pathlike/to/migrations var embedded embed.FS
You must prefix every call to embedded with the string "some/path/or/migrations", or use this like
//go:embed some/pathlike/to/migrations var embedded embed.FS var migrations = FromEmbed(embedded, "some/pathlike/to/migrations")
Which allows one to directly pass `migrations` into
func IngestMigrations ¶
IngestMigrations reads all the migrations in the given directory and writes them to a Go source file in the same directory. The generateTag argument determines whether the new Go file will contain a "//go:generate" comment to tag it for automatic regeneration by "go generate".
func InitMigration ¶
InitMigration creates a new 00001_init migration in the given directory. This migration will contain the SQL commands necessary to create the migration_state table.
func InitMigrationTimestamp ¶
InitMigrationTimestamp creates a new {timestamp}_init migration in the given directory. This migration will contain the SQL commands necessary to create the `migration_state` table.
func MigrateBackwardTo ¶
MigrateBackwardTo will run backward migrations starting with the most recent in state, and going through the one provided in `name`.
func MigrateForwardTo ¶
MigrateForwardTo will run all forward migrations that have not yet been run, up to and including the one specified by `name`. To run all un-run migrations, set `name` to an empty string.
func NewMigration ¶
NewMigration creates a new directory containing forward.sql and backward.sql stubs. The directory created will use the name provided to the function, prepended by an auto-incrementing zero-padded number.
func NewMigrationTimestamp ¶
NewMigrationTimestamp creates a new directory containing forward.sql and backward.sql stubs. The directory created will use the name provided to the function, prepended by a timestamp formatted with `YYYYMMDDhhmmss` (i.e. `20060102150405`).
Types ¶
type Migration ¶
Migration contains the name and SQL for a migration. Arrays of Migrations are passed between many functions in the Pomegranate source. SeperateForwardStatements runs SQL statements seperately, delinieated by ";"
func ReadMigrationFS ¶
ReadMigrationFs allows one to embed an entire migration folder using the embed package.
go:embed migrations-dir var migrationDir embed.FS ... migrations, err := ReadMigrationFs(migrationDir)
It is expected that migrations is a *directory*, and it will be treated as such"
func ReadMigrationFiles ¶
ReadMigrationFiles reads all the migration files in the given directory and returns an array of Migration objects.
func (Migration) QuotedTemplateBackward ¶
QuotedTemplateBackward returns the BackwardSQL field of the Migration, properly escaped for easy injection into a migrations.go template.
func (Migration) QuotedTemplateForward ¶
QuotedTemplateForward returns the ForwardSQL field of the Migration, properly escaped for easy injection into a migrations.go template.
type MigrationLogRecord ¶
type MigrationLogRecord struct { ID int `db:"id"` Time time.Time `db:"time"` Name string `db:"name"` Op string `db:"op"` Who string `db:"who"` }
MigrationLogRecord represents a specific migration run at a specific point in time. Unlike MigrationRecord, this is an append-only table, showing the complete history of all forward and backward migrations. It is populated automatically by a Postgres trigger created in the init migration.
func GetMigrationLog ¶
func GetMigrationLog(db *sql.DB) ([]MigrationLogRecord, error)
GetMigrationLog returns the complete history of all migrations, forward and backward. If the migration_log table does not exist, it returns an empty list of MigrationLogRecords
type MigrationRecord ¶
type MigrationRecord struct { Name string `db:"name"` Time time.Time `db:"time"` Who string `db:"who"` }
MigrationRecord provides information on which migrations are currently in effect. An array of MigrationRecords is referred to as a "state" throughout the Pomegranate source. These are treated as a stack; MigrationRecords are added (inserted into the DB) as migrations run forward, and popped off (deleted from the DB) as migrations are run backward.
func GetMigrationState ¶
func GetMigrationState(db *sql.DB) ([]MigrationRecord, error)
GetMigrationState returns the stack of migration records stored in the database's migration_state table. If that table does not exist, it returns an empty list.
type OsDir ¶
type OsDir string
A OsDir is a fs.ReadDirFS that expects to be sitting on the local file system
func (OsDir) Open ¶
Open implements fs.ReadDirFS and just proxies for os.Open. This is not safe with "." and "..", and is intended only for well known paths
func (OsDir) ReadDir ¶
ReadDir implemnts fs.ReadDirFS and just proxies for os.ReadDir This is not safe with "." and "..", and is intended only for well known paths