Skip to content

Commit 34e270f

Browse files
add versioning to sqlc definition rather than in schema definition
1 parent 964601d commit 34e270f

File tree

11 files changed

+48
-29
lines changed

11 files changed

+48
-29
lines changed

rolling-shutter/chainobserver/db/collator/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var files embed.FS
1616
var Definition db.Definition
1717

1818
func init() {
19-
def, err := db.NewSQLCDefinition(files, "sql/", "chainobscollator", 1)
19+
def, err := db.NewSQLCDefinition(files, "sql/", "chainobscollator")
2020
if err != nil {
2121
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2222
}

rolling-shutter/chainobserver/db/keyper/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var files embed.FS
1616
var Definition db.Definition
1717

1818
func init() {
19-
def, err := db.NewSQLCDefinition(files, "sql/", "chainobskeyper", 1)
19+
def, err := db.NewSQLCDefinition(files, "sql/", "chainobskeyper")
2020
if err != nil {
2121
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2222
}

rolling-shutter/chainobserver/db/sync/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Definition db.Definition
1616

1717
func init() {
1818
var err error
19-
Definition, err = db.NewSQLCDefinition(files, "sql/", "chainobssync", 1)
19+
Definition, err = db.NewSQLCDefinition(files, "sql/", "chainobssync")
2020
if err != nil {
2121
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2222
}

rolling-shutter/keyper/database/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var files embed.FS
2121
var Definition db.Definition
2222

2323
func init() {
24-
sqlcDB, err := db.NewSQLCDefinition(files, "sql/", "keyper", 1)
24+
sqlcDB, err := db.NewSQLCDefinition(files, "sql/", "keyper")
2525
if err != nil {
2626
log.Fatal().Err(err).Msg("failed to initialize DB")
2727
}

rolling-shutter/keyperimpl/gnosis/database/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var files embed.FS
1717
var Definition db.Definition
1818

1919
func init() {
20-
def, err := db.NewSQLCDefinition(files, "sql/", "gnosiskeyper", 2)
20+
def, err := db.NewSQLCDefinition(files, "sql/", "gnosiskeyper")
2121
if err != nil {
2222
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2323
}

rolling-shutter/keyperimpl/shutterservice/database/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var files embed.FS
1717
var Definition db.Definition
1818

1919
func init() {
20-
def, err := db.NewSQLCDefinition(files, "sql/", "shutterservicekeyper", 1)
20+
def, err := db.NewSQLCDefinition(files, "sql/", "shutterservicekeyper")
2121
if err != nil {
2222
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2323
}

rolling-shutter/medley/db/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var metaDefinition Definition
1414

1515
func init() {
1616
var err error
17-
metaDefinition, err = NewSQLCDefinition(files, "sql/", "meta", 1)
17+
metaDefinition, err = NewSQLCDefinition(files, "sql/", "meta")
1818
if err != nil {
1919
log.Fatal().Err(err).Msg("failed to initialize DB metadata")
2020
}

rolling-shutter/medley/db/definitions.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ type Definition interface {
149149
}
150150

151151
type Schema struct {
152-
Version int
153-
Name string
154-
Path string
152+
Name string
153+
Path string
155154
}
156155

157156
type Migration struct {

rolling-shutter/medley/db/metadb.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func InsertDBVersion(ctx context.Context, tx pgx.Tx, version string) error {
3131
return insertMetaInf(ctx, tx, DatabaseVersionKey, version)
3232
}
3333

34-
func InsertSchemaVersion(ctx context.Context, tx pgx.Tx, definitionName string, schema Schema) error {
35-
return insertMetaInf(ctx, tx, MakeSchemaVersionKey(definitionName, schema.Name), fmt.Sprint(schema.Version))
34+
func InsertSchemaVersion(ctx context.Context, tx pgx.Tx, definitionName string, schema Schema, version int) error {
35+
return insertMetaInf(ctx, tx, MakeSchemaVersionKey(definitionName, schema.Name), fmt.Sprint(version))
3636
}
3737

3838
func insertMetaInf(ctx context.Context, tx pgx.Tx, key, val string) error {
@@ -44,16 +44,16 @@ func insertMetaInf(ctx context.Context, tx pgx.Tx, key, val string) error {
4444
})
4545
}
4646

47-
func UpdateSchemaVersion(ctx context.Context, tx pgx.Tx, defName string, schema Schema) error {
47+
func UpdateSchemaVersion(ctx context.Context, tx pgx.Tx, defName string, schema Schema, version int) error {
4848
return New(tx).UpdateMeta(ctx, UpdateMetaParams{
4949
Key: MakeSchemaVersionKey(defName, schema.Name),
50-
Value: fmt.Sprint(schema.Version),
50+
Value: fmt.Sprint(version),
5151
})
5252
}
5353

5454
// ValidateSchemaVersion checks that the database schema is compatible.
55-
func ValidateSchemaVersion(ctx context.Context, tx pgx.Tx, definitionName string, schema Schema) error {
56-
return expectMetaKeyVal(ctx, tx, MakeSchemaVersionKey(definitionName, schema.Name), fmt.Sprint(schema.Version))
55+
func ValidateSchemaVersion(ctx context.Context, tx pgx.Tx, definitionName string, schema Schema, version int) error {
56+
return expectMetaKeyVal(ctx, tx, MakeSchemaVersionKey(definitionName, schema.Name), fmt.Sprint(version))
5757
}
5858

5959
func expectMetaKeyVal(ctx context.Context, tx pgx.Tx, key, val string) error {

rolling-shutter/medley/db/sqlc.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type entry struct {
3333
// paths of all the schema definition directories.
3434
// The schema file will then be stored in the object's state
3535
// together with the passed in `version`.
36-
func ParseSQLC(filesystem fs.FS, sqlcPath string, version int) ([]Schema, error) {
36+
func ParseSQLC(filesystem fs.FS, sqlcPath string) ([]Schema, error) {
3737
b, err := fs.ReadFile(filesystem, sqlcPath)
3838
if err != nil {
3939
return nil, err
@@ -70,17 +70,16 @@ func ParseSQLC(filesystem fs.FS, sqlcPath string, version int) ([]Schema, error)
7070
continue
7171
}
7272
schema := Schema{
73-
Version: version,
74-
Name: base,
75-
Path: path.Join(schemaDirPath, i.Name()),
73+
Name: base,
74+
Path: path.Join(schemaDirPath, i.Name()),
7675
}
7776
schemas = append(schemas, schema)
7877
}
7978
}
8079
return schemas, nil
8180
}
8281

83-
func NewSQLCDefinition(filesystem fs.FS, sqlcPath string, name string, version int) (*SQLC, error) {
82+
func NewSQLCDefinition(filesystem fs.FS, sqlcPath string, name string) (*SQLC, error) {
8483
p := path.Clean(sqlcPath)
8584
des, err := fs.ReadDir(filesystem, p)
8685
if errors.Is(err, ErrNotADirectory) {
@@ -99,16 +98,22 @@ func NewSQLCDefinition(filesystem fs.FS, sqlcPath string, name string, version i
9998
if foundPath == "" {
10099
return nil, errors.Errorf("SQLC file '%s' does not exists", p)
101100
}
102-
schemas, err := ParseSQLC(filesystem, foundPath, version)
101+
schemas, err := ParseSQLC(filesystem, foundPath)
103102
if err != nil {
104103
return nil, err
105104
}
106-
return &SQLC{
105+
106+
sqlcdef := &SQLC{
107107
schemas: schemas,
108108
filesystem: filesystem,
109109
name: name,
110110
sqlcPath: sqlcPath,
111-
}, nil
111+
}
112+
sqlcdef.version, err = sqlcdef.GetLatestMigrationVersion()
113+
if err != nil {
114+
return nil, err
115+
}
116+
return sqlcdef, err
112117
}
113118

114119
// SQLC implements the `Definition` interface and keeps
@@ -118,6 +123,7 @@ type SQLC struct {
118123
filesystem fs.FS
119124
name string
120125
sqlcPath string
126+
version int
121127
}
122128

123129
func (d *SQLC) Name() string {
@@ -143,8 +149,7 @@ func (d *SQLC) Create(ctx context.Context, tx pgx.Tx) error {
143149

144150
for _, schema := range d.schemas {
145151
// this is initial creation of db, so create version as one here
146-
schema.Version = 1
147-
err := InsertSchemaVersion(ctx, tx, d.Name(), schema)
152+
err := InsertSchemaVersion(ctx, tx, d.Name(), schema, 1)
148153
if err != nil {
149154
return err
150155
}
@@ -156,7 +161,7 @@ func (d *SQLC) Create(ctx context.Context, tx pgx.Tx) error {
156161
// with the schema versions of it's schema definitions.
157162
func (d *SQLC) Validate(ctx context.Context, tx pgx.Tx) error {
158163
for _, schema := range d.schemas {
159-
err := ValidateSchemaVersion(ctx, tx, d.Name(), schema)
164+
err := ValidateSchemaVersion(ctx, tx, d.Name(), schema, d.version)
160165
if err != nil {
161166
return err
162167
}
@@ -258,7 +263,7 @@ func (d *SQLC) Migrate(ctx context.Context, tx pgx.Tx) error {
258263
}
259264

260265
// Update version after each successful migration
261-
err = UpdateSchemaVersion(ctx, tx, d.Name(), schema)
266+
err = UpdateSchemaVersion(ctx, tx, d.Name(), schema, migration.Version)
262267
if err != nil {
263268
return errors.Wrapf(err, "failed to update schema version to %d", migration.Version)
264269
}
@@ -267,3 +272,18 @@ func (d *SQLC) Migrate(ctx context.Context, tx pgx.Tx) error {
267272

268273
return nil
269274
}
275+
276+
func (d *SQLC) GetLatestMigrationVersion() (int, error) {
277+
migrations, err := d.LoadMigrations()
278+
if err != nil {
279+
return 0, err
280+
}
281+
282+
if len(migrations) == 0 {
283+
return 1, nil // Return 1 if no migrations exist
284+
}
285+
286+
// Since migrations are already sorted in LoadMigrations(),
287+
// we can just return the version of the last migration
288+
return migrations[len(migrations)-1].Version, nil
289+
}

0 commit comments

Comments
 (0)