Skip to content

Commit 12de8a2

Browse files
authored
[v2] DB migration with version (#7103)
Signed-off-by: machichima <nary12321@gmail.com>
1 parent 0e0bbd3 commit 12de8a2

File tree

8 files changed

+59
-19
lines changed

8 files changed

+59
-19
lines changed

cache_service/manager/manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/glebarez/sqlite"
9+
"github.com/go-gormigrate/gormigrate/v2"
910
"github.com/stretchr/testify/require"
1011
durationpb "google.golang.org/protobuf/types/known/durationpb"
1112
"gorm.io/gorm"
@@ -21,7 +22,8 @@ func newTestManager(t *testing.T) *Manager {
2122

2223
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
2324
require.NoError(t, err)
24-
require.NoError(t, migrations.RunMigrations(db))
25+
m := gormigrate.New(db, gormigrate.DefaultOptions, migrations.CacheServiceMigrations)
26+
require.NoError(t, m.Migrate())
2527

2628
cfg := &cacheconfig.Config{
2729
HeartbeatGracePeriodMultiplier: 3,
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
package migrations
22

33
import (
4-
"context"
54
"fmt"
65

6+
"github.com/go-gormigrate/gormigrate/v2"
77
"gorm.io/gorm"
88

99
"github.com/flyteorg/flyte/v2/cache_service/repository/models"
10-
"github.com/flyteorg/flyte/v2/flytestdlib/logger"
1110
)
1211

1312
var allModels = []interface{}{
1413
&models.CachedOutput{},
1514
&models.Reservation{},
1615
}
1716

18-
func RunMigrations(db *gorm.DB) error {
17+
const MigrationIDInitSchema = "20260327_cache_service_init_schema"
18+
19+
var CacheServiceMigrations = []*gormigrate.Migration{
20+
{
21+
ID: MigrationIDInitSchema,
22+
Migrate: func(tx *gorm.DB) error {
23+
return migrateInitSchema(tx)
24+
},
25+
Rollback: func(tx *gorm.DB) error {
26+
// Intentionally no-op for now; this migration can contain destructive steps.
27+
return nil
28+
},
29+
},
30+
}
31+
32+
// migrateInitSchema initializes the cache service database schema.
33+
func migrateInitSchema(db *gorm.DB) error {
1934
if err := db.AutoMigrate(allModels...); err != nil {
20-
return fmt.Errorf("failed to run cache service migrations: %w", err)
35+
return fmt.Errorf("failed to initialize cache service schema: %w", err)
2136
}
22-
23-
logger.Infof(context.Background(), "Cache service migrations completed successfully")
2437
return nil
2538
}

cache_service/setup.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"net/http"
77

8+
"github.com/go-gormigrate/gormigrate/v2"
9+
810
"github.com/flyteorg/flyte/v2/app"
911
"github.com/flyteorg/flyte/v2/cache_service/config"
1012
"github.com/flyteorg/flyte/v2/cache_service/migrations"
@@ -17,8 +19,9 @@ import (
1719
// Requires sc.DB and sc.DataStore to be set by the standalone binary.
1820
func Setup(ctx context.Context, sc *app.SetupContext) error {
1921
cfg := config.GetConfig()
20-
if err := migrations.RunMigrations(sc.DB); err != nil {
21-
return err
22+
m := gormigrate.New(sc.DB, gormigrate.DefaultOptions, migrations.CacheServiceMigrations)
23+
if err := m.Migrate(); err != nil {
24+
return fmt.Errorf("cache_service: failed to run migrations: %w", err)
2225
}
2326

2427
path, handler := v2connect.NewCacheServiceHandler(service.NewCacheService(cfg, sc.DB))

manager/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,10 @@ manager:
266266
267267
### Database Issues
268268
269-
**Error:** "failed to run migrations"
269+
**Error:** relation/table not found (runs schema missing)
270270
271271
```bash
272-
# Remove and recreate database
273-
rm flyte.db
272+
# Restart manager so startup migrations run
274273
./bin/flyte-manager --config config.yaml
275274
```
276275

runs/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,13 @@ No code changes needed, just update `config.yaml`!
409409

410410
### Database migration
411411

412-
We are using gorm for auto migration, please ensure addding `gorm` tags in `/runs/repository/models/` when you
413-
add any more columns/models.
412+
Runs schema changes are managed through versioned gormigrate migrations.
413+
414+
Migrations run automatically during service startup.
415+
416+
```bash
417+
make run
418+
```
414419

415420
### Adding new features
416421

runs/migrations/migrations.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/go-gormigrate/gormigrate/v2"
78
"gorm.io/gorm"
89

910
"github.com/flyteorg/flyte/v2/flytestdlib/logger"
@@ -19,8 +20,23 @@ var AllModels = []interface{}{
1920
&models.TaskSpec{},
2021
}
2122

22-
// RunMigrations runs all database migrations for the runs service
23-
func RunMigrations(db *gorm.DB) error {
23+
const MigrationIDInitSchema = "20260327_runs_init_schema"
24+
25+
var RunsMigrations = []*gormigrate.Migration{
26+
{
27+
ID: MigrationIDInitSchema,
28+
Migrate: func(tx *gorm.DB) error {
29+
return migrateInitSchema(tx)
30+
},
31+
Rollback: func(tx *gorm.DB) error {
32+
// Intentionally no-op for now; this migration can contain destructive steps.
33+
return nil
34+
},
35+
},
36+
}
37+
38+
// migrateInitSchema initializes the runs service database schema.
39+
func migrateInitSchema(db *gorm.DB) error {
2440
ctx := context.Background()
2541

2642
// Drop stale tables from previous schema versions that are no longer used.

runs/setup.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"net/http"
88
"time"
99

10+
"github.com/go-gormigrate/gormigrate/v2"
11+
1012
"github.com/flyteorg/flyte/v2/app"
1113
"github.com/flyteorg/flyte/v2/gen/go/flyteidl2/actions/actionsconnect"
1214
flyteappconnect "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/app/appconnect"
@@ -32,8 +34,8 @@ import (
3234
// RunLogsService is also mounted to enable pod log streaming.
3335
func Setup(ctx context.Context, sc *app.SetupContext) error {
3436
cfg := config.GetConfig()
35-
36-
if err := migrations.RunMigrations(sc.DB); err != nil {
37+
m := gormigrate.New(sc.DB, gormigrate.DefaultOptions, migrations.RunsMigrations)
38+
if err := m.Migrate(); err != nil {
3739
return fmt.Errorf("runs: failed to run migrations: %w", err)
3840
}
3941

runs/test/api/setup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestMain(m *testing.M) {
7878
log.Println("Database initialized")
7979

8080
// Run migrations
81-
if err := migrations.RunMigrations(testDB); err != nil {
81+
if err := database.Migrate(ctx, dbConfig, migrations.RunsMigrations); err != nil {
8282
log.Printf("Failed to run migrations: %v", err)
8383
exitCode = 1
8484
return

0 commit comments

Comments
 (0)