Skip to content

Commit

Permalink
Add confugurable cachePath (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
movit committed Aug 11, 2023
1 parent 8b137fc commit 3320fcf
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This library aims to require as little configuration as possible, favouring over
| Password | postgres |
| Database | postgres |
| Version | 12.1.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 |
Expand Down
10 changes: 6 additions & 4 deletions cache_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
// The result of whether this cache is present will be returned to exists.
type CacheLocator func() (location string, exists bool)

func defaultCacheLocator(versionStrategy VersionStrategy) CacheLocator {
func defaultCacheLocator(cacheDirectory string, versionStrategy VersionStrategy) CacheLocator {
return func() (string, bool) {
cacheDirectory := ".embedded-postgres-go"
if userHome, err := os.UserHomeDir(); err == nil {
cacheDirectory = filepath.Join(userHome, ".embedded-postgres-go")
if cacheDirectory == "" {
cacheDirectory = ".embedded-postgres-go"
if userHome, err := os.UserHomeDir(); err == nil {
cacheDirectory = filepath.Join(userHome, ".embedded-postgres-go")
}
}

operatingSystem, architecture, version := versionStrategy()
Expand Down
13 changes: 12 additions & 1 deletion cache_locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test_defaultCacheLocator_NotExists(t *testing.T) {
locator := defaultCacheLocator(func() (string, string, PostgresVersion) {
locator := defaultCacheLocator("", func() (string, string, PostgresVersion) {
return "a", "b", "1.2.3"
})

Expand All @@ -16,3 +16,14 @@ func Test_defaultCacheLocator_NotExists(t *testing.T) {
assert.Contains(t, cacheLocation, ".embedded-postgres-go/embedded-postgres-binaries-a-b-1.2.3.txz")
assert.False(t, exists)
}

func Test_defaultCacheLocator_CustomPath(t *testing.T) {
locator := defaultCacheLocator("/custom/path", func() (string, string, PostgresVersion) {
return "a", "b", "1.2.3"
})

cacheLocation, exists := locator()

assert.Equal(t, cacheLocation, "/custom/path/embedded-postgres-binaries-a-b-1.2.3.txz")
assert.False(t, exists)
}
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
database string
username string
password string
cachePath string
runtimePath string
dataPath string
binariesPath string
Expand Down Expand Up @@ -82,6 +83,13 @@ func (c Config) RuntimePath(path string) Config {
return c
}

// CachePath sets the path that will be used for storing Postgres binaries archive.
// If this option is not set, ~/.go-embedded-postgres will be used.
func (c Config) CachePath(path string) Config {
c.cachePath = path
return c
}

// DataPath sets the path that will be used for the Postgres data directory.
// If this option is set, a previously initialized data directory will be reused if possible.
func (c Config) DataPath(path string) Config {
Expand Down
2 changes: 1 addition & 1 deletion embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newDatabaseWithConfig(config Config) *EmbeddedPostgres {
linuxMachineName,
shouldUseAlpineLinuxBuild,
)
cacheLocator := defaultCacheLocator(versionStrategy)
cacheLocator := defaultCacheLocator(config.cachePath, versionStrategy)
remoteFetchStrategy := defaultRemoteFetchStrategy(config.binaryRepositoryURL, versionStrategy, cacheLocator)

return &EmbeddedPostgres{
Expand Down
24 changes: 24 additions & 0 deletions embedded_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,30 @@ func Test_CustomBinariesRepo(t *testing.T) {
}
}

func Test_CachePath(t *testing.T) {
cacheTempDir, err := os.MkdirTemp("", "prepare_database_test_cache")
if err != nil {
panic(err)
}

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

database := NewDatabase(DefaultConfig().
CachePath(cacheTempDir))

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

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

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

0 comments on commit 3320fcf

Please sign in to comment.