Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fergusstrange/embedded-postgres
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.26.0
Choose a base ref
...
head repository: fergusstrange/embedded-postgres
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.27.0
Choose a head ref
  • 4 commits
  • 9 files changed
  • 3 contributors

Commits on May 10, 2024

  1. feat: make encoding configurable in initdb (#133)

    zzzFelix authored May 10, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c00e987 View commit details
  2. Update build & test workflows (#136)

    der-eismann authored May 10, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    234c605 View commit details
  3. Update docs

    ferguss committed May 10, 2024
    Copy the full SHA
    a3b80e3 View commit details
  4. Purge unused files.

    ferguss committed May 10, 2024
    Copy the full SHA
    efb8f55 View commit details
Showing with 91 additions and 47 deletions.
  1. +2 −2 .circleci/config.yml
  2. +10 −10 .github/workflows/build.yml
  3. +6 −2 README.md
  4. +0 −20 cmd/main.go
  5. +8 −1 config.go
  6. +1 −1 embedded_postgres.go
  7. +34 −6 embedded_postgres_test.go
  8. +6 −2 prepare_database.go
  9. +24 −3 prepare_database_test.go
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -2,15 +2,15 @@ version: 2.1
executors:
linux-arm64:
machine:
image: ubuntu-2004:2022.04.1
image: ubuntu-2204:2024.01.2
resource_class: arm.medium
working_directory: /home/circleci/go/src/github.com/fergusstrange/embedded-postgres
apple-m1: &macos-executor
resource_class: macos.m1.medium.gen1
macos:
xcode: "14.2.0"
orbs:
go: circleci/go@1.7.3
go: circleci/go@1.11.0
jobs:
platform_test:
parameters:
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -11,11 +11,11 @@ jobs:
steps:
- name: Checkout
id: go
uses: actions/checkout@v1
uses: actions/checkout@v4
- name: Set Up Golang
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.18
go-version: 1.22
- name: Check Dependencies
run: |
go list -json -deps > go.list
@@ -37,7 +37,7 @@ jobs:
- name: Nancy Vulnerability
uses: sonatype-nexus-community/nancy-github-action@main
with:
nancyVersion: v1.0.36
nancyVersion: v1.0.46
nancyCommand: sleuth
- name: GolangCI Lint
run: |
@@ -53,14 +53,14 @@ jobs:
- name: Upload Coverage Report
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: GO111MODULE=off go get github.com/mattn/goveralls && $(go env GOPATH)/bin/goveralls -v -coverprofile=coverage.out -service=github
run: go install github.com/mattn/goveralls@latest && $(go env GOPATH)/bin/goveralls -v -coverprofile=coverage.out -service=github
alpine_tests:
name: Alpine Linux Platform Tests
runs-on: ubuntu-latest
container:
image: golang:1.18-alpine
image: golang:1.22-alpine
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set Up
run: |
apk add --upgrade gcc g++ && \
@@ -75,11 +75,11 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4
- name: Set Up Golang
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.18
go-version: 1.22
- name: Platform Tests
run: |
cd platform-test
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -42,13 +42,17 @@ This library aims to require as little configuration as possible, favouring over
| Password | postgres |
| Database | postgres |
| Version | 15.3.0 |
| Encoding | UTF8 |
| Locale | C |
| Version | 15.3.0 |
| CachePath | $USER_HOME/.embedded-postgres-go/ |
| RuntimePath | $USER_HOME/.embedded-postgres-go/extracted |
| DataPath | $USER_HOME/.embedded-postgres-go/extracted/data |
| BinariesPath | $USER_HOME/.embedded-postgres-go/extracted |
| BinaryRepositoryURL | https://repo1.maven.org/maven2 |
| Port | 5432 |
| StartTimeout | 15 Seconds |
| StartParameters | map[string]string{"max_connections": "101"} |

The *RuntimePath* directory is erased and recreated at each `Start()` and therefore not suitable for persistent data.

@@ -85,10 +89,10 @@ Password("wine").
Database("gin").
Version(V12).
RuntimePath("/tmp").
BinaryRepositoryURL("https://repo.local/central.proxy").
BinaryRepositoryURL("https://repo.local/central.proxy").
Port(9876).
StartTimeout(45 * time.Second).
StartParameters(map[string]string{"max_connections": "200"}).
StartParameters(map[string]string{"max_connections": "200"}).
Logger(logger))
err := postgres.Start()

20 changes: 0 additions & 20 deletions cmd/main.go

This file was deleted.

9 changes: 8 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ type Config struct {
dataPath string
binariesPath string
locale string
encoding string
startParameters map[string]string
binaryRepositoryURL string
startTimeout time.Duration
@@ -27,7 +28,7 @@ type Config struct {

// DefaultConfig provides a default set of configuration to be used "as is" or modified using the provided builders.
// The following can be assumed as defaults:
// Version: 15
// Version: 16
// Port: 5432
// Database: postgres
// Username: postgres
@@ -110,6 +111,12 @@ func (c Config) Locale(locale string) Config {
return c
}

// Encoding sets the default character set for initdb
func (c Config) Encoding(encoding string) Config {
c.encoding = encoding
return c
}

// StartParameters sets run-time parameters when starting Postgres (passed to Postgres via "-c").
//
// These parameters can be used to override the default configuration values in postgres.conf such
2 changes: 1 addition & 1 deletion embedded_postgres.go
Original file line number Diff line number Diff line change
@@ -167,7 +167,7 @@ func (ep *EmbeddedPostgres) cleanDataDirectoryAndInit() error {
return fmt.Errorf("unable to clean up data directory %s with error: %s", ep.config.dataPath, err)
}

if err := ep.initDatabase(ep.config.binariesPath, ep.config.runtimePath, ep.config.dataPath, ep.config.username, ep.config.password, ep.config.locale, ep.syncedLogger.file); err != nil {
if err := ep.initDatabase(ep.config.binariesPath, ep.config.runtimePath, ep.config.dataPath, ep.config.username, ep.config.password, ep.config.locale, ep.config.encoding, ep.syncedLogger.file); err != nil {
return err
}

40 changes: 34 additions & 6 deletions embedded_postgres_test.go
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ func Test_ErrorWhenUnableToInitDatabase(t *testing.T) {
return jarFile, true
}

database.initDatabase = func(binaryExtractLocation, runtimePath, dataLocation, username, password, locale string, logger *os.File) error {
database.initDatabase = func(binaryExtractLocation, runtimePath, dataLocation, username, password, locale string, encoding string, logger *os.File) error {
return errors.New("ah it did not work")
}

@@ -226,7 +226,7 @@ func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {
return jarFile, true
}

database.initDatabase = func(binaryExtractLocation, runtimePath, dataLocation, username, password, locale string, logger *os.File) error {
database.initDatabase = func(binaryExtractLocation, runtimePath, dataLocation, username, password, locale string, encoding string, logger *os.File) error {
_, _ = logger.Write([]byte("ah it did not work"))
return nil
}
@@ -257,6 +257,7 @@ func Test_CustomConfig(t *testing.T) {
Port(9876).
StartTimeout(10 * time.Second).
Locale("C").
Encoding("UTF8").
Logger(nil))

if err := database.Start(); err != nil {
@@ -356,6 +357,36 @@ func Test_CustomLocaleConfig(t *testing.T) {
}
}

func Test_CustomEncodingConfig(t *testing.T) {
database := NewDatabase(DefaultConfig().Encoding("UTF8"))
if err := database.Start(); err != nil {
shutdownDBAndFail(t, err, database)
}

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
shutdownDBAndFail(t, err, database)
}

rows := db.QueryRow("SHOW SERVER_ENCODING;")

var (
value string
)
if err := rows.Scan(&value); err != nil {
shutdownDBAndFail(t, err, database)
}
assert.Equal(t, "UTF8", value)

if err := db.Close(); err != nil {
shutdownDBAndFail(t, err, database)
}

if err := database.Stop(); err != nil {
shutdownDBAndFail(t, err, database)
}
}

func Test_ConcurrentStart(t *testing.T) {
var wg sync.WaitGroup

@@ -417,10 +448,7 @@ func Test_ConcurrentStart(t *testing.T) {
}

func Test_CustomStartParameters(t *testing.T) {
database := NewDatabase(DefaultConfig().StartParameters(map[string]string{
"max_connections": "101",
"shared_buffers": "16 MB", // Ensure a parameter with spaces encodes correctly.
}))
database := NewDatabase(DefaultConfig().StartParameters(map[string]string{"max_connections": "101"}))
if err := database.Start(); err != nil {
shutdownDBAndFail(t, err, database)
}
8 changes: 6 additions & 2 deletions prepare_database.go
Original file line number Diff line number Diff line change
@@ -18,10 +18,10 @@ const (
fmtAfterError = "%v happened after error: %w"
)

type initDatabase func(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, logger *os.File) error
type initDatabase func(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, encoding string, logger *os.File) error
type createDatabase func(port uint32, username, password, database string) error

func defaultInitDatabase(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, logger *os.File) error {
func defaultInitDatabase(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, encoding string, logger *os.File) error {
passwordFile, err := createPasswordFile(runtimePath, password)
if err != nil {
return err
@@ -38,6 +38,10 @@ func defaultInitDatabase(binaryExtractLocation, runtimePath, pgDataDir, username
args = append(args, fmt.Sprintf("--locale=%s", locale))
}

if encoding != "" {
args = append(args, fmt.Sprintf("--encoding=%s", encoding))
}

postgresInitDBBinary := filepath.Join(binaryExtractLocation, "bin/initdb")
postgresInitDBProcess := exec.Command(postgresInitDBBinary, args...)
postgresInitDBProcess.Stderr = logger
27 changes: 24 additions & 3 deletions prepare_database_test.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
)

func Test_defaultInitDatabase_ErrorWhenCannotCreatePasswordFile(t *testing.T) {
err := defaultInitDatabase("path_not_exists", "path_not_exists", "path_not_exists", "Tom", "Beer", "", os.Stderr)
err := defaultInitDatabase("path_not_exists", "path_not_exists", "path_not_exists", "Tom", "Beer", "", "", os.Stderr)

assert.EqualError(t, err, "unable to write password file to path_not_exists/pwfile")
}
@@ -49,7 +49,7 @@ func Test_defaultInitDatabase_ErrorWhenCannotStartInitDBProcess(t *testing.T) {

_, _ = logFile.Write([]byte("and here are the logs!"))

err = defaultInitDatabase(binTempDir, runtimeTempDir, filepath.Join(runtimeTempDir, "data"), "Tom", "Beer", "", logFile)
err = defaultInitDatabase(binTempDir, runtimeTempDir, filepath.Join(runtimeTempDir, "data"), "Tom", "Beer", "", "", logFile)

assert.NotNil(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("unable to init database using '%s/bin/initdb -A password -U Tom -D %s/data --pwfile=%s/pwfile'",
@@ -72,7 +72,7 @@ func Test_defaultInitDatabase_ErrorInvalidLocaleSetting(t *testing.T) {
}
}()

err = defaultInitDatabase(tempDir, tempDir, filepath.Join(tempDir, "data"), "postgres", "postgres", "en_XY", os.Stderr)
err = defaultInitDatabase(tempDir, tempDir, filepath.Join(tempDir, "data"), "postgres", "postgres", "en_XY", "", os.Stderr)

assert.NotNil(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("unable to init database using '%s/bin/initdb -A password -U postgres -D %s/data --pwfile=%s/pwfile --locale=en_XY'",
@@ -81,6 +81,27 @@ func Test_defaultInitDatabase_ErrorInvalidLocaleSetting(t *testing.T) {
tempDir))
}

func Test_defaultInitDatabase_ErrorInvalidEncodingSetting(t *testing.T) {
tempDir, err := os.MkdirTemp("", "prepare_database_test")
if err != nil {
panic(err)
}

defer func() {
if err := os.RemoveAll(tempDir); err != nil {
panic(err)
}
}()

err = defaultInitDatabase(tempDir, tempDir, filepath.Join(tempDir, "data"), "postgres", "postgres", "", "invalid", os.Stderr)

assert.NotNil(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("unable to init database using '%s/bin/initdb -A password -U postgres -D %s/data --pwfile=%s/pwfile --encoding=invalid'",
tempDir,
tempDir,
tempDir))
}

func Test_defaultInitDatabase_PwFileRemoved(t *testing.T) {
tempDir, err := os.MkdirTemp("", "prepare_database_test")
if err != nil {