Skip to content

Commit f6f6bf6

Browse files
committed
add db.NeedsMigration()
1 parent 2afeace commit f6f6bf6

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

internal/cmdopts/cmdoptions.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,7 @@ func (c *Options) InitConfigReaders(ctx context.Context) error {
161161
if err != nil {
162162
return err
163163
}
164-
if m, ok := c.MetricsReaderWriter.(db.Migrator); ok {
165-
if needsMigration, err := m.NeedsMigration(); err != nil {
166-
return err
167-
} else if needsMigration {
168-
return metrics.ErrNeedsMigration
169-
}
170-
}
171-
return nil
164+
return db.NeedsMigration(ctx, c.MetricsReaderWriter, metrics.ErrNeedsMigration)
172165
}
173166

174167
// InitSinkWriter creates a new MultiWriter instance if needed.
@@ -177,14 +170,7 @@ func (c *Options) InitSinkWriter(ctx context.Context) (err error) {
177170
if err != nil {
178171
return err
179172
}
180-
if m, ok := c.SinksWriter.(db.Migrator); ok {
181-
if needsMigration, err := m.NeedsMigration(); err != nil {
182-
return err
183-
} else if needsMigration {
184-
return sinks.ErrNeedsMigration
185-
}
186-
}
187-
return nil
173+
return db.NeedsMigration(ctx, c.SinksWriter, sinks.ErrNeedsMigration)
188174
}
189175

190176
// NeedsSchemaUpgrade checks if the configuration database schema needs an upgrade.

internal/db/conn.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ type Migrator interface {
5252
NeedsMigration() (bool, error)
5353
}
5454

55+
func NeedsMigration(ctx context.Context, storage any, needsMigrationErr error) error {
56+
if m, ok := storage.(Migrator); ok {
57+
if needsMigration, err := m.NeedsMigration(); err != nil {
58+
return err
59+
} else if needsMigration {
60+
return needsMigrationErr
61+
}
62+
}
63+
return nil
64+
}
65+
5566
func MarshallParamToJSONB(v any) any {
5667
if v == nil {
5768
return nil

internal/metrics/types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,6 @@ type Writer interface {
209209
CreatePreset(presetName string, preset Preset) error
210210
}
211211

212-
type Migrator interface {
213-
Migrate() error
214-
NeedsMigration() (bool, error)
215-
}
216-
217212
type ReaderWriter interface {
218213
Reader
219214
Writer

0 commit comments

Comments
 (0)