Skip to content

Commit 3320fcf

Browse files
authored
Add confugurable cachePath (#120)
1 parent 8b137fc commit 3320fcf

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This library aims to require as little configuration as possible, favouring over
4242
| Password | postgres |
4343
| Database | postgres |
4444
| Version | 12.1.0 |
45+
| CachePath | $USER_HOME/.embedded-postgres-go/ |
4546
| RuntimePath | $USER_HOME/.embedded-postgres-go/extracted |
4647
| DataPath | $USER_HOME/.embedded-postgres-go/extracted/data |
4748
| BinariesPath | $USER_HOME/.embedded-postgres-go/extracted |

cache_locator.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
// The result of whether this cache is present will be returned to exists.
1111
type CacheLocator func() (location string, exists bool)
1212

13-
func defaultCacheLocator(versionStrategy VersionStrategy) CacheLocator {
13+
func defaultCacheLocator(cacheDirectory string, versionStrategy VersionStrategy) CacheLocator {
1414
return func() (string, bool) {
15-
cacheDirectory := ".embedded-postgres-go"
16-
if userHome, err := os.UserHomeDir(); err == nil {
17-
cacheDirectory = filepath.Join(userHome, ".embedded-postgres-go")
15+
if cacheDirectory == "" {
16+
cacheDirectory = ".embedded-postgres-go"
17+
if userHome, err := os.UserHomeDir(); err == nil {
18+
cacheDirectory = filepath.Join(userHome, ".embedded-postgres-go")
19+
}
1820
}
1921

2022
operatingSystem, architecture, version := versionStrategy()

cache_locator_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func Test_defaultCacheLocator_NotExists(t *testing.T) {
10-
locator := defaultCacheLocator(func() (string, string, PostgresVersion) {
10+
locator := defaultCacheLocator("", func() (string, string, PostgresVersion) {
1111
return "a", "b", "1.2.3"
1212
})
1313

@@ -16,3 +16,14 @@ func Test_defaultCacheLocator_NotExists(t *testing.T) {
1616
assert.Contains(t, cacheLocation, ".embedded-postgres-go/embedded-postgres-binaries-a-b-1.2.3.txz")
1717
assert.False(t, exists)
1818
}
19+
20+
func Test_defaultCacheLocator_CustomPath(t *testing.T) {
21+
locator := defaultCacheLocator("/custom/path", func() (string, string, PostgresVersion) {
22+
return "a", "b", "1.2.3"
23+
})
24+
25+
cacheLocation, exists := locator()
26+
27+
assert.Equal(t, cacheLocation, "/custom/path/embedded-postgres-binaries-a-b-1.2.3.txz")
28+
assert.False(t, exists)
29+
}

config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config struct {
1414
database string
1515
username string
1616
password string
17+
cachePath string
1718
runtimePath string
1819
dataPath string
1920
binariesPath string
@@ -82,6 +83,13 @@ func (c Config) RuntimePath(path string) Config {
8283
return c
8384
}
8485

86+
// CachePath sets the path that will be used for storing Postgres binaries archive.
87+
// If this option is not set, ~/.go-embedded-postgres will be used.
88+
func (c Config) CachePath(path string) Config {
89+
c.cachePath = path
90+
return c
91+
}
92+
8593
// DataPath sets the path that will be used for the Postgres data directory.
8694
// If this option is set, a previously initialized data directory will be reused if possible.
8795
func (c Config) DataPath(path string) Config {

embedded_postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newDatabaseWithConfig(config Config) *EmbeddedPostgres {
4444
linuxMachineName,
4545
shouldUseAlpineLinuxBuild,
4646
)
47-
cacheLocator := defaultCacheLocator(versionStrategy)
47+
cacheLocator := defaultCacheLocator(config.cachePath, versionStrategy)
4848
remoteFetchStrategy := defaultRemoteFetchStrategy(config.binaryRepositoryURL, versionStrategy, cacheLocator)
4949

5050
return &EmbeddedPostgres{

embedded_postgres_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,30 @@ func Test_CustomBinariesRepo(t *testing.T) {
620620
}
621621
}
622622

623+
func Test_CachePath(t *testing.T) {
624+
cacheTempDir, err := os.MkdirTemp("", "prepare_database_test_cache")
625+
if err != nil {
626+
panic(err)
627+
}
628+
629+
defer func() {
630+
if err := os.RemoveAll(cacheTempDir); err != nil {
631+
panic(err)
632+
}
633+
}()
634+
635+
database := NewDatabase(DefaultConfig().
636+
CachePath(cacheTempDir))
637+
638+
if err := database.Start(); err != nil {
639+
shutdownDBAndFail(t, err, database)
640+
}
641+
642+
if err := database.Stop(); err != nil {
643+
shutdownDBAndFail(t, err, database)
644+
}
645+
}
646+
623647
func Test_CustomBinariesLocation(t *testing.T) {
624648
tempDir, err := os.MkdirTemp("", "prepare_database_test")
625649
if err != nil {

0 commit comments

Comments
 (0)