Skip to content

Commit 749bc0d

Browse files
authored
Allow DB.MetaPath to be configured (#485)
1 parent 2045363 commit 749bc0d

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

cmd/litestream/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) {
247247
// DBConfig represents the configuration for a single database.
248248
type DBConfig struct {
249249
Path string `yaml:"path"`
250+
MetaPath *string `yaml:"meta-path"`
250251
MonitorInterval *time.Duration `yaml:"monitor-interval"`
251252
CheckpointInterval *time.Duration `yaml:"checkpoint-interval"`
252253
MinCheckpointPageN *int `yaml:"min-checkpoint-page-count"`
@@ -266,6 +267,9 @@ func NewDBFromConfig(dbc *DBConfig) (*litestream.DB, error) {
266267
db := litestream.NewDB(path)
267268

268269
// Override default database settings if specified in configuration.
270+
if dbc.MetaPath != nil {
271+
db.SetMetaPath(*dbc.MetaPath)
272+
}
269273
if dbc.MonitorInterval != nil {
270274
db.MonitorInterval = *dbc.MonitorInterval
271275
}

db.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const BusyTimeout = 1 * time.Second
4444
type DB struct {
4545
mu sync.RWMutex
4646
path string // part to database
47+
metaPath string // Path to the database metadata.
4748
db *sql.DB // target database
4849
f *os.File // long-running db file descriptor
4950
rtx *sql.Tx // long running read transaction
@@ -101,9 +102,12 @@ type DB struct {
101102

102103
// NewDB returns a new instance of DB for a given path.
103104
func NewDB(path string) *DB {
105+
dir, file := filepath.Split(path)
106+
104107
db := &DB{
105-
path: path,
106-
notify: make(chan struct{}),
108+
path: path,
109+
metaPath: filepath.Join(dir, "."+file+MetaDirSuffix),
110+
notify: make(chan struct{}),
107111

108112
MinCheckpointPageN: DefaultMinCheckpointPageN,
109113
MaxCheckpointPageN: DefaultMaxCheckpointPageN,
@@ -147,20 +151,24 @@ func (db *DB) WALPath() string {
147151

148152
// MetaPath returns the path to the database metadata.
149153
func (db *DB) MetaPath() string {
150-
dir, file := filepath.Split(db.path)
151-
return filepath.Join(dir, "."+file+MetaDirSuffix)
154+
return db.metaPath
155+
}
156+
157+
// SetMetaPath sets the path to database metadata.
158+
func (db *DB) SetMetaPath(mp string) {
159+
db.metaPath = mp
152160
}
153161

154162
// GenerationNamePath returns the path of the name of the current generation.
155163
func (db *DB) GenerationNamePath() string {
156-
return filepath.Join(db.MetaPath(), "generation")
164+
return filepath.Join(db.metaPath, "generation")
157165
}
158166

159167
// GenerationPath returns the path of a single generation.
160168
// Panics if generation is blank.
161169
func (db *DB) GenerationPath(generation string) string {
162170
assert(generation != "", "generation name required")
163-
return filepath.Join(db.MetaPath(), "generations", generation)
171+
return filepath.Join(db.metaPath, "generations", generation)
164172
}
165173

166174
// ShadowWALDir returns the path of the shadow wal directory.
@@ -283,7 +291,7 @@ func (db *DB) Open() (err error) {
283291
}
284292

285293
// Clear old temporary files that my have been left from a crash.
286-
if err := removeTmpFiles(db.MetaPath()); err != nil {
294+
if err := removeTmpFiles(db.metaPath); err != nil {
287295
return fmt.Errorf("cannot remove tmp files: %w", err)
288296
}
289297

@@ -446,7 +454,7 @@ func (db *DB) init() (err error) {
446454
}
447455

448456
// Ensure meta directory structure exists.
449-
if err := internal.MkdirAll(db.MetaPath(), db.dirInfo); err != nil {
457+
if err := internal.MkdirAll(db.metaPath, db.dirInfo); err != nil {
450458
return err
451459
}
452460

@@ -522,7 +530,7 @@ func (db *DB) cleanGenerations() error {
522530
return err
523531
}
524532

525-
dir := filepath.Join(db.MetaPath(), "generations")
533+
dir := filepath.Join(db.metaPath, "generations")
526534
fis, err := ioutil.ReadDir(dir)
527535
if os.IsNotExist(err) {
528536
return nil
@@ -652,7 +660,7 @@ func (db *DB) createGeneration() (string, error) {
652660
generation := hex.EncodeToString(buf)
653661

654662
// Generate new directory.
655-
dir := filepath.Join(db.MetaPath(), "generations", generation)
663+
dir := filepath.Join(db.metaPath, "generations", generation)
656664
if err := internal.MkdirAll(dir, db.dirInfo); err != nil {
657665
return "", err
658666
}

0 commit comments

Comments
 (0)