diff --git a/backend/core/migration/migrator.go b/backend/core/migration/migrator.go index f30a75b94ae..d60c9b8f970 100644 --- a/backend/core/migration/migrator.go +++ b/backend/core/migration/migrator.go @@ -81,12 +81,17 @@ func (m *migratorImpl) Register(scripts []plugin.MigrationScript, comment string } } +func (m *migratorImpl) Info(stage string) { + m.logger.Info("[%s] pending scripts: %d, executed scripts: %d, total: %d", stage, len(m.pending), len(m.executed), len(m.scripts)) +} + // Execute all registered migration script in order and mark them as executed in migration_history table func (m *migratorImpl) Execute() errors.Error { // sort the scripts by version sort.Slice(m.pending, func(i, j int) bool { return m.pending[i].script.Version() < m.pending[j].script.Version() }) + m.Info("Execute") // execute them one by one db := m.basicRes.GetDal() for _, swc := range m.pending { diff --git a/backend/server/api/api.go b/backend/server/api/api.go index fd659627be4..7c0266b6173 100644 --- a/backend/server/api/api.go +++ b/backend/server/api/api.go @@ -68,6 +68,7 @@ func Init() { func CreateAndRunApiServer() { // Setup and run the server Init() + services.InitExecuteMigration() router := CreateApiServer() SetupApiServer(router) RunApiServer(router) diff --git a/backend/server/services/init.go b/backend/server/services/init.go index 3a99c02e34f..e75af65bf9e 100644 --- a/backend/server/services/init.go +++ b/backend/server/services/init.go @@ -70,7 +70,7 @@ func InitResources() { if err != nil { panic(err) } - logger.Info("migration initialized") + logger.Info("migrator has been initialized") migrator.Register(migrationscripts.All(), "Framework") } @@ -84,25 +84,19 @@ func GetMigrator() plugin.Migrator { return migrator } -// Init the services module -// Should not be called concurrently -func Init() { - InitResources() - - // lock the database to avoid multiple devlake instances from sharing the same one - lockDatabase() - - // now, load the plugins - errors.Must(runner.LoadPlugins(basicRes)) - +func registerPluginsMigrationScripts() { // pull migration scripts from plugins to migrator for _, pluginInst := range plugin.AllPlugins() { if migratable, ok := pluginInst.(plugin.PluginMigration); ok { + logger.Info("register plugin:%s's migrations scripts", pluginInst.Name()) migrator.Register(migratable.MigrationScripts(), pluginInst.Name()) } } +} +func InitExecuteMigration() { // check if there are pending migration + logger.Info("has pending scripts? %v, FORCE_MIGRATION: %s", migrator.HasPendingScripts(), cfg.GetBool("FORCE_MIGRATION")) if migrator.HasPendingScripts() { if cfg.GetBool("FORCE_MIGRATION") { errors.Must(ExecuteMigration()) @@ -117,6 +111,20 @@ func Init() { } } +// Init the services module +// Should not be called concurrently +func Init() { + InitResources() + + // lock the database to avoid multiple devlake instances from sharing the same one + lockDatabase() + + // now, load the plugins + errors.Must(runner.LoadPlugins(basicRes)) + logger.Info("all plugins have been loaded") + registerPluginsMigrationScripts() +} + var statusLock sync.Mutex // ExecuteMigration executes all pending migration scripts and initialize services module diff --git a/backend/test/helper/client.go b/backend/test/helper/client.go index dcc30cba291..0af014b0513 100644 --- a/backend/test/helper/client.go +++ b/backend/test/helper/client.go @@ -172,7 +172,9 @@ func ConnectLocalServer(t *testing.T, clientConfig *LocalClientConfig) *DevlakeC cfg.Set("PLUGIN_DIR", throwawayDir) cfg.Set("LOGGING_DIR", throwawayDir) go func() { - initService.Do(func() { api.CreateAndRunApiServer() }) + initService.Do(func() { + api.CreateAndRunApiServer() + }) }() req, err2 := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/proceed-db-migration", addr), nil) require.NoError(t, err2)