You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2022/10/26 11:44:04 UTC

[incubator-devlake] branch main updated: refactor: remove the interface core.Migratable (#3591)

This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new ce676525 refactor: remove the interface core.Migratable (#3591)
ce676525 is described below

commit ce6765258fe22dfb02f39a53c90bff62ebf74423
Author: mindlesscloud <li...@merico.dev>
AuthorDate: Wed Oct 26 19:44:00 2022 +0800

    refactor: remove the interface core.Migratable (#3591)
    
    * refactor: remove the interface core.Migratable
    
    * refactor: remove the package
---
 migration/interface.go           |  30 ----------
 migration/migrator.go            | 123 ---------------------------------------
 migration/models.go              |  37 ------------
 plugins/core/plugin_migration.go |   6 --
 plugins/github/impl/impl.go      |   2 -
 runner/directrun.go              |   3 +-
 6 files changed, 1 insertion(+), 200 deletions(-)

diff --git a/migration/interface.go b/migration/interface.go
deleted file mode 100644
index e66c5ba2..00000000
--- a/migration/interface.go
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package migration
-
-import (
-	"context"
-	"github.com/apache/incubator-devlake/errors"
-	"gorm.io/gorm"
-)
-
-type Script interface {
-	Up(ctx context.Context, db *gorm.DB) errors.Error
-	Version() uint64
-	Name() string
-}
diff --git a/migration/migrator.go b/migration/migrator.go
deleted file mode 100644
index ca899298..00000000
--- a/migration/migrator.go
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package migration
-
-import (
-	"context"
-	"fmt"
-	"sort"
-	"sync"
-
-	"github.com/apache/incubator-devlake/errors"
-
-	"gorm.io/gorm"
-)
-
-var m = migrator{}
-
-type scriptWithComment struct {
-	Script
-	comment string
-}
-type migrator struct {
-	sync.Mutex
-	db       *gorm.DB
-	executed map[string]bool
-	scripts  []*scriptWithComment
-	pending  []*scriptWithComment
-}
-
-func Init(db *gorm.DB) {
-	m.db = db
-	var err error
-	m.executed, err = m.getExecuted()
-	if err != nil {
-		panic(err)
-	}
-}
-
-func (m *migrator) register(scripts []Script, comment string) {
-	m.Lock()
-	defer m.Unlock()
-	for _, script := range scripts {
-		key := fmt.Sprintf("%s:%d", script.Name(), script.Version())
-		swc := &scriptWithComment{
-			Script:  script,
-			comment: comment,
-		}
-		m.scripts = append(m.scripts, swc)
-		if !m.executed[key] {
-			m.pending = append(m.pending, swc)
-		}
-	}
-}
-
-func (m *migrator) bookKeep(script *scriptWithComment) errors.Error {
-	record := &MigrationHistory{
-		ScriptVersion: script.Version(),
-		ScriptName:    script.Name(),
-		Comment:       script.comment,
-	}
-	return errors.Convert(m.db.Create(record).Error)
-}
-
-func (m *migrator) execute(ctx context.Context) errors.Error {
-	sort.Slice(m.pending, func(i, j int) bool {
-		return m.pending[i].Version() < m.pending[j].Version()
-	})
-	for _, script := range m.pending {
-		err := script.Up(ctx, m.db)
-		if err != nil {
-			return errors.Default.Wrap(err, fmt.Sprintf("error Up() on script [%s]", script.Name()))
-		}
-		err = m.bookKeep(script)
-		if err != nil {
-			return errors.Default.Wrap(err, fmt.Sprintf("error bookKeep() on script [%s]", script.Name()))
-		}
-	}
-	return nil
-}
-func (m *migrator) getExecuted() (map[string]bool, errors.Error) {
-	var err error
-	versions := make(map[string]bool)
-	err = m.db.Migrator().AutoMigrate(&MigrationHistory{})
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error performing migrations")
-	}
-	var records []MigrationHistory
-	err = m.db.Find(&records).Error
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error finding migration history records")
-	}
-	for _, record := range records {
-		versions[fmt.Sprintf("%s:%d", record.ScriptName, record.ScriptVersion)] = true
-	}
-	return versions, nil
-}
-
-func Register(scripts []Script, comment string) {
-	m.register(scripts, comment)
-}
-
-func Execute(ctx context.Context) errors.Error {
-	return m.execute(ctx)
-}
-
-func NeedConfirmation() bool {
-	return len(m.executed) > 0 && len(m.pending) > 0
-}
diff --git a/migration/models.go b/migration/models.go
deleted file mode 100644
index 174c4e98..00000000
--- a/migration/models.go
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package migration
-
-import (
-	"time"
-)
-
-const (
-	tableName = "_devlake_migration_history"
-)
-
-type MigrationHistory struct {
-	CreatedAt     time.Time
-	ScriptVersion uint64 `gorm:"primarykey"`
-	ScriptName    string `gorm:"primarykey;type:varchar(255)"`
-	Comment       string
-}
-
-func (MigrationHistory) TableName() string {
-	return tableName
-}
diff --git a/plugins/core/plugin_migration.go b/plugins/core/plugin_migration.go
index af8feb3b..1df8f930 100644
--- a/plugins/core/plugin_migration.go
+++ b/plugins/core/plugin_migration.go
@@ -19,7 +19,6 @@ package core
 
 import (
 	"github.com/apache/incubator-devlake/errors"
-	"github.com/apache/incubator-devlake/migration"
 )
 
 // MigrationScript upgrades database to a newer version
@@ -40,8 +39,3 @@ type Migrator interface {
 type PluginMigration interface {
 	MigrationScripts() []MigrationScript
 }
-
-// TODO: remove this interface
-type Migratable interface {
-	MigrationScripts() []migration.Script
-}
diff --git a/plugins/github/impl/impl.go b/plugins/github/impl/impl.go
index 2821cc0f..84f7a333 100644
--- a/plugins/github/impl/impl.go
+++ b/plugins/github/impl/impl.go
@@ -36,8 +36,6 @@ var _ core.PluginMeta = (*Github)(nil)
 var _ core.PluginInit = (*Github)(nil)
 var _ core.PluginTask = (*Github)(nil)
 var _ core.PluginApi = (*Github)(nil)
-
-// var _ core.Migratable = (*Github)(nil)
 var _ core.PluginBlueprintV100 = (*Github)(nil)
 var _ core.CloseablePluginTask = (*Github)(nil)
 
diff --git a/runner/directrun.go b/runner/directrun.go
index cbe72ee2..8b805743 100644
--- a/runner/directrun.go
+++ b/runner/directrun.go
@@ -31,7 +31,6 @@ import (
 	"github.com/apache/incubator-devlake/impl"
 	"github.com/apache/incubator-devlake/impl/dalgorm"
 	"github.com/apache/incubator-devlake/logger"
-	"github.com/apache/incubator-devlake/migration"
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/spf13/cobra"
 )
@@ -81,7 +80,7 @@ func DirectRun(cmd *cobra.Command, args []string, pluginTask core.PluginTask, op
 	if migratable, ok := pluginTask.(core.PluginMigration); ok {
 		migrator.Register(migratable.MigrationScripts(), cmd.Use)
 	}
-	err = migration.Execute(context.Background())
+	err = migrator.Execute()
 	if err != nil {
 		panic(err)
 	}