Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Run Tests on Multiple Versions of PostgreSQL

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17'

- name: Lint
run: |
go install golang.org/x/lint/golint@latest
golint ./...
go vet ./...

test:
strategy:
matrix:
postgres-version: [10, 11, 12, 15, 17]
runs-on: ubuntu-latest
container: golang:alpine

services:
postgres:
image: postgres:${{ matrix.postgres-version }}
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 54${{ matrix.postgres-version }}:5432

steps:
- uses: actions/checkout@v3

- name: Install PostgreSQL (client)
run: |
apk --update add postgresql-client

- name: Check PostgreSQL version
run: |
psql --version

- name: Install dependencies
run: |
go mod download

- name: Run tests
env:
PGMGR_TEST_HOST: postgres
PGHOST: postgres
PGPORT: 54${{ matrix.postgres-version}}
PGMGR_TEST_PORT: 54${{ matrix.postgres-version}}
PGUSER: postgres
PGPASSWORD: postgres
run: |
go test -timeout=30s ./...
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v1.1.6

* Migrated to GitHub actions for CI.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ $ pgmgr db migrate
== Completed in 8 ms ==
```

## Internals

Under the hood, pgmgr needs to manage an internal schema migrations table to
track which migrations have been applied to the database. This table is
automatically created when the `pgmgr db migrate` command is run for the first
time, or you can create it manually.

To apply each migration, pgmgr uses its configured driver (`pq`, deprecated, or
`psql`) to run each migration code. It wraps the code in a transaction (unless
the migration is named with `.no_txn.` in its filename at any point) and adds
code at the end to log the migration in the schema migrations table.

## Configuration

`pgmgr` supports file-based configuration (useful for checking into your
Expand Down Expand Up @@ -135,10 +147,29 @@ look at the standard Postgres env vars (`PGHOST`, `PGUSERNAME`, etc).

```
pgmgr migration MigrationName # generates files for a new migration
pgmgr migration --no-txn MName # generate a migration which will run without wrapping transaction
pgmgr db create # creates the database if it doesn't exist
pgmgr db drop # drop the database
pgmgr db migrate # apply un-applied migrations
pgmgr db rollback # reverts the latest migration, if possible.
pgmgr db load # loads the schema dump file from PGMGR_DUMP_FILE
pgmgr db dump # dumps the database structure & seeds to PGMGR_DUMP_FILE
```

## Development

### Running tests

pgmgr tests depend on a running Postgres instance. The easiest way to get all that spun up
quickly is to use the `act` tool to run the GitHub Actions worfklow manually:

```
$ act
```

You can also ensure you have a user with name `postgres` and password `postgres` created in
your local postgres instance and run the tests directly:

```
$ go test ./...
```
9 changes: 5 additions & 4 deletions pgmgr/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgmgr

import (
"fmt"
"os"
"testing"
)
Expand Down Expand Up @@ -31,12 +32,12 @@ func TestDefaults(t *testing.T) {

LoadConfig(c, &TestContext{})

if c.Port != 5432 {
t.Fatal("config's port should default to 5432")
if fmt.Sprint(c.Port) != getEnv("PGMGR_TEST_PORT", "5432") {
t.Fatal("config's port should default to 5432 (or value of $PGMGR_TEST_PORT), but was ", c.Port)
}

if c.Host != "localhost" {
t.Fatal("config's host should default to localhost, but was ", c.Host)
if c.Host != getEnv("PGMGR_TEST_HOST", "localhost") {
t.Fatal("config's host should default to localhost (or value of $PGMGR_TEST_HOST), but was ", c.Host)
}

if c.MigrationTable != "schema_migrations" {
Expand Down
4 changes: 2 additions & 2 deletions pgmgr/pgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Create(c *Config) error {
return err
}

return sh("createdb", []string{c.Database})
return sh("createdb", []string{"-w", c.Database})
}

// Drop drops the database specified by the configuration.
Expand All @@ -58,7 +58,7 @@ func Drop(c *Config) error {
return err
}

return sh("dropdb", []string{c.Database})
return sh("dropdb", []string{"-w", c.Database})
}

// Dump dumps the schema and contents of the database to the dump file.
Expand Down
21 changes: 18 additions & 3 deletions pgmgr/pgmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgmgr
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
Expand All @@ -17,11 +18,19 @@ const (
dumpFile = "/tmp/pgmgr_dump.sql"
)

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

func globalConfig() *Config {
return &Config{
Username: "pgmgr",
Username: "postgres",
Password: "postgres",
Database: testDBName,
Host: "localhost",
Host: getEnv("PGMGR_TEST_HOST", "localhost"),
Port: 5432,
MigrationFolder: migrationFolder,
MigrationTable: "schema_migrations",
Expand All @@ -31,13 +40,19 @@ func globalConfig() *Config {
}

func TestCreate(t *testing.T) {
dropDB(t)
if err := dropDB(t); err != nil {
t.Log("database already does not exist; skipping dropdb")
}

t.Log("creating database...")

if err := Create(globalConfig()); err != nil {
t.Log(err)
t.Fatal("Could not create database")
}

t.Log("creating database done")

// if we can't remove that db, it couldn't have been created by us above.
if err := dropDB(t); err != nil {
t.Fatal("database doesn't seem to have been created!")
Expand Down