File tree Expand file tree Collapse file tree 8 files changed +59
-19
lines changed
Expand file tree Collapse file tree 8 files changed +59
-19
lines changed Original file line number Diff line number Diff line change 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 ,
Original file line number Diff line number Diff line change 11package migrations
22
33import (
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
1312var 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}
Original file line number Diff line number Diff line change 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.
1820func 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 ))
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
3335func 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments