@@ -33,7 +33,7 @@ type entry struct {
33
33
// paths of all the schema definition directories.
34
34
// The schema file will then be stored in the object's state
35
35
// 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 ) {
37
37
b , err := fs .ReadFile (filesystem , sqlcPath )
38
38
if err != nil {
39
39
return nil , err
@@ -70,17 +70,16 @@ func ParseSQLC(filesystem fs.FS, sqlcPath string, version int) ([]Schema, error)
70
70
continue
71
71
}
72
72
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 ()),
76
75
}
77
76
schemas = append (schemas , schema )
78
77
}
79
78
}
80
79
return schemas , nil
81
80
}
82
81
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 ) {
84
83
p := path .Clean (sqlcPath )
85
84
des , err := fs .ReadDir (filesystem , p )
86
85
if errors .Is (err , ErrNotADirectory ) {
@@ -99,16 +98,22 @@ func NewSQLCDefinition(filesystem fs.FS, sqlcPath string, name string, version i
99
98
if foundPath == "" {
100
99
return nil , errors .Errorf ("SQLC file '%s' does not exists" , p )
101
100
}
102
- schemas , err := ParseSQLC (filesystem , foundPath , version )
101
+ schemas , err := ParseSQLC (filesystem , foundPath )
103
102
if err != nil {
104
103
return nil , err
105
104
}
106
- return & SQLC {
105
+
106
+ sqlcdef := & SQLC {
107
107
schemas : schemas ,
108
108
filesystem : filesystem ,
109
109
name : name ,
110
110
sqlcPath : sqlcPath ,
111
- }, nil
111
+ }
112
+ sqlcdef .version , err = sqlcdef .GetLatestMigrationVersion ()
113
+ if err != nil {
114
+ return nil , err
115
+ }
116
+ return sqlcdef , err
112
117
}
113
118
114
119
// SQLC implements the `Definition` interface and keeps
@@ -118,6 +123,7 @@ type SQLC struct {
118
123
filesystem fs.FS
119
124
name string
120
125
sqlcPath string
126
+ version int
121
127
}
122
128
123
129
func (d * SQLC ) Name () string {
@@ -143,8 +149,7 @@ func (d *SQLC) Create(ctx context.Context, tx pgx.Tx) error {
143
149
144
150
for _ , schema := range d .schemas {
145
151
// 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 )
148
153
if err != nil {
149
154
return err
150
155
}
@@ -156,7 +161,7 @@ func (d *SQLC) Create(ctx context.Context, tx pgx.Tx) error {
156
161
// with the schema versions of it's schema definitions.
157
162
func (d * SQLC ) Validate (ctx context.Context , tx pgx.Tx ) error {
158
163
for _ , schema := range d .schemas {
159
- err := ValidateSchemaVersion (ctx , tx , d .Name (), schema )
164
+ err := ValidateSchemaVersion (ctx , tx , d .Name (), schema , d . version )
160
165
if err != nil {
161
166
return err
162
167
}
@@ -258,7 +263,7 @@ func (d *SQLC) Migrate(ctx context.Context, tx pgx.Tx) error {
258
263
}
259
264
260
265
// Update version after each successful migration
261
- err = UpdateSchemaVersion (ctx , tx , d .Name (), schema )
266
+ err = UpdateSchemaVersion (ctx , tx , d .Name (), schema , migration . Version )
262
267
if err != nil {
263
268
return errors .Wrapf (err , "failed to update schema version to %d" , migration .Version )
264
269
}
@@ -267,3 +272,18 @@ func (d *SQLC) Migrate(ctx context.Context, tx pgx.Tx) error {
267
272
268
273
return nil
269
274
}
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