fork of https://github.com/jackc/Pggo/ Pggo is a standalone migration tool for PostgreSQL.
- Multi-platform
- Stand-alone binary
- SSH tunnel support built-in
- Data variable interpolation into migrations
go get -u github.com/jackc/pggo
To create a new pggo project in the current directory run:
pggo init
Or to create the project somewhere else:
pggo init path/to/project
Pggo projects are composed of a directory of migrations and optionally a config file. See the sample directory for an example.
Database connection settings can be specified via the standard PostgreSQL environment variables, via program arguments, or in a config file. By default pggo will look in the current directory for the config file pggo.conf and the migrations.
The pggo.conf
file is stored in the ini
format with two sections,
database
and data
. The database
section contains settings for connection
to the database server.
Values in the data
section will be available for interpolation into
migrations. This can help in scenarios where migrations are managing
permissions and the user to which permissions are granted should be
configurable.
If all database settings are supplied by PG* environment variables or program
arguments the config file is not required. In particular, using the PGSERVICE
can reduce or eliminate the need for a configuration file.
The entire pggo.conf
file is processed through the Go standard
text/template
package. The program environment is available at .env
.
Example pggo.conf
:
[database]
# host supports TCP addresses and Unix domain sockets
# host = /private/tmp
host = 127.0.0.1
# port = 5432
database = pggo_test
user = jack
password = {{.env.MIGRATOR_PASSWORD}}
# version_table = public.schema_version
#
# sslmode generally matches the behavior described in:
# http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION
#
# There are only two modes that most users should use:
# prefer - on trusted networks where security is not required
# verify-full - require SSL connection
# sslmode = prefer
# Proxy the above database connection via SSH
# [ssh-tunnel]
# host =
# port = 22
# user defaults to OS user
# user =
# password is not required if using SSH agent authentication
# password =
[data]
prefix = foo
app_user = joe
This flexibility configuration style allows handling multiple environments such as test, development, and production in several ways.
- Separate config file for each environment
- Environment variables for database settings and optionally one config file for shared settings
- Program arguments for database settings and optionally one config file for shared settings
To create a new migration:
pggo new name_of_migration
This will create a migration file with the given name prefixed by the next available sequence number (e.g. 001, 002, 003).
The migrations themselves have an extremely simple file format. They are simply the up and down SQL statements divided by a magic comment.
---- create above / drop below ----
Example:
create table t1(
id serial primary key
);
---- create above / drop below ----
drop table t1;
If a migration is irreversible such as a drop table, simply delete the magic comment.
drop table widgets;
To interpolate a custom data value from the config file prefix the name with a dot and surround the whole with double curly braces.
create table {{.prefix}}config(
id serial primary key
);
Migrations are read from files in the migration directory in the order of the numerical prefix. Each migration is run in a transaction.
Any SQL files in subdirectories of the migration directory, will be available for inclusion with the template command. This can be especially useful for definitions of views and functions that may have to be dropped and recreated when the underlying table(s) change.
// Include the file shared/v1_001.sql. Note the trailing dot.
// It is necessary if the shared file needs access to custom data values.
{{ template "shared/v1_001.sql" . }}
);
Pggo uses the standard Go text/template package so conditionals and other advanced templating features are available if needed. See the package docs for details.
To migrate up to the last version using migrations and config file located in the same directory simply run pggo:
pggo migrate
To migrate up or down to a specific version:
pggo migrate --destination 42
To migrate up N versions:
pggo migrate --destination +3
To migrate down N versions:
pggo migrate --destination -3
To migrate down and rerun the previous N versions:
pggo migrate --destination -+3
To use a different config file:
pggo migrate --config path/to/pggo.json
To use a different migrations directory:
pggo migrate --migrations path/to/migrations
Pggo includes SSH tunnel support. Simply supply the SSH host, and optionally
port, user, and password in the config file or as program arguments and Pggo
will tunnel the database connection through that server. When using a SSH tunnel
the database host should be from the context of the SSH server. For example, if
your PostgreSQL server is pg.example.com
, but you only have SSH access, then
your SSH host would be pg.example.com and your database host would be
localhost
.
Pggo will automatically use an SSH agent if available.
All the actual functionality of pggo is in the github.com/jackc/pggo/migrate library. If you need to embed migrations into your own application this library can help.
To run the tests pggo requires two test databases to run migrations against.
-
Create a new database for main pggo program tests.
-
Open testdata/pggo.conf.example
-
Enter the connection information.
-
Save as testdata/pggo.conf.
-
Create another database for the migrate library tests.
-
Run tests with the connection string for the migrate library tests in the MIGRATE_TEST_CONN_STRING environment variable
MIGRATE_TEST_CONN_STRING="host=/private/tmp database=pggo_migrate_test" go test ./...
The projects using the prior version of pggo that was distributed as a Ruby Gem are incompatible with the version 1 release. However, that version of pggo is still available through RubyGems and the source code is on the ruby branch.
- Fix default CLI version-table argument overriding config value
- Better locking to protect against multiple concurrent migrators on first run
- Update pgx version to support PostgreSQL service files
- Look for version table in all schemas in search_path instead of just the top schema
- Default version table is explicitly in public schema
- Update to pgx v4 (Alex Gaynor)
- Show PostgreSQL error details
- Rename inpggoal error type
- Issue
reset all
after every migration - Use go modules instead of Godep / vendoring
- Update to latest version of pgx (PostgreSQL driver)
- Support PGSSLROOTCERT
- Fix typos and inpggoal cleanup
- Refactor inpggoals for easier embedding (hsyed)
- Simplify SSH tunnel code so it does not listen on localhost
- Add SSH tunnel support
- Fix version output
- Evaluate config files through text/template with ENV
- Optionally read database connection settings from environment
- Accept database connection settings via program arguments
- Make config file optional
- Add status command
- Add relative migration destinations
- Add redo migration destinations
- Add TLS support
- Fix version output
- Better error messages
- Fix custom version table name
- Prefer host config whether connecting with unix domain socket or TCP
- Fix new migration short name
- Support socket directory as well as socket file
- Move to subcommand interface
- Require migrations to begin with ascending, gapless numbers
- Fix: migrations directory can contain other files
- Fix: gracefully handle invalid current version
- Fix: gracefully handle migrations with duplicate sequence number
- Do not require user -- default to OS user
- Add sub-template support
- Switch to ini for config files
- Add custom data merging
- Total rewrite in Go
- Print friendly error message when database error occurs instead of stack trace.
- Added ERB processing to SQL files
Copyright (c) 2011-2014 Jack Christensen, released under the MIT license