Skip to content

Commit 7a966e6

Browse files
authored
fix (extras/kms): add support for tableNamePrefix to schema type (#184)
I missed updating the schema type to support a tableNamePrefix in PR #182
1 parent 61c41ef commit 7a966e6

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

extras/kms/repository.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ func newRepository(r dbw.Reader, w dbw.Writer, opt ...Option) (*repository, erro
6868
if w == nil {
6969
return nil, fmt.Errorf("%s: nil writer: %w", op, ErrInvalidParameter)
7070
}
71-
if _, err := validateSchema(context.Background(), r); err != nil {
72-
return nil, fmt.Errorf("%s: %w", op, err)
73-
}
7471
opts := getOpts(opt...)
7572
if opts.withLimit == 0 {
7673
// zero signals the defaults should be used.
7774
opts.withLimit = defaultLimit
7875
}
76+
if _, err := validateSchema(context.Background(), r, opts.withTableNamePrefix); err != nil {
77+
return nil, fmt.Errorf("%s: %w", op, err)
78+
}
7979
return &repository{
8080
reader: r,
8181
writer: w,
@@ -88,13 +88,15 @@ func newRepository(r dbw.Reader, w dbw.Writer, opt ...Option) (*repository, erro
8888
// required migrations.Version
8989
func (r *repository) ValidateSchema(ctx context.Context) (string, error) {
9090
const op = "kms.(repository).validateVersion"
91-
return validateSchema(ctx, r.reader)
91+
return validateSchema(ctx, r.reader, r.tableNamePrefix)
9292
}
9393

94-
func validateSchema(ctx context.Context, r dbw.Reader) (string, error) {
94+
func validateSchema(ctx context.Context, r dbw.Reader, tableNamePrefix string) (string, error) {
9595
const op = "kms.validateSchema"
96-
var s schema
97-
if err := r.LookupWhere(ctx, &s, "1=1", nil); err != nil {
96+
s := schema{
97+
tableNamePrefix: tableNamePrefix,
98+
}
99+
if err := r.LookupWhere(ctx, &s, "1=1", nil, dbw.WithTable(s.TableName())); err != nil {
98100
return "", fmt.Errorf("%s: unable to get version: %w", op, err)
99101
}
100102
if s.Version != migrations.Version {

extras/kms/schema.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package kms
55

6-
import "time"
6+
import (
7+
"fmt"
8+
"time"
9+
)
710

811
// schema represents the current schema in the database
912
type schema struct {
@@ -13,7 +16,15 @@ type schema struct {
1316
UpdateTime time.Time
1417
// CreateTime is the create time of the initial version
1518
CreateTime time.Time
19+
20+
// tableNamePrefix defines the prefix to use before the table name and
21+
// allows us to support custom prefixes as well as multi KMSs within a
22+
// single schema.
23+
tableNamePrefix string `gorm:"-"`
1624
}
1725

18-
// TableName defines the table name for the Version type
19-
func (v *schema) TableName() string { return "kms_schema_version" }
26+
// TableName returns the table name
27+
func (k *schema) TableName() string {
28+
const tableName = "schema_version"
29+
return fmt.Sprintf("%s_%s", k.tableNamePrefix, tableName)
30+
}

0 commit comments

Comments
 (0)