You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by wa...@apache.org on 2022/10/24 06:57:42 UTC

[incubator-devlake] branch main updated: refactor: dora migrationscript (#3542)

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

warren 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 a8bb6eef refactor: dora migrationscript (#3542)
a8bb6eef is described below

commit a8bb6eef30fcaa0d3b6bc4cf6e410b2bea63bcac
Author: mappjzc <zh...@merico.dev>
AuthorDate: Mon Oct 24 14:57:38 2022 +0800

    refactor: dora migrationscript (#3542)
    
    Refactor Dora MigrationScript
    
    Nddtfjiang <zh...@merico.dev>
---
 plugins/dora/impl/impl.go                          |  4 +-
 .../migrationscripts/20220829_add_init_tables.go   | 41 --------------------
 .../20220928_add_dora_benchmark.go                 | 44 +++++++++++-----------
 plugins/dora/models/migrationscripts/register.go   |  7 ++--
 4 files changed, 27 insertions(+), 69 deletions(-)

diff --git a/plugins/dora/impl/impl.go b/plugins/dora/impl/impl.go
index 66f52f6f..aa13a6c7 100644
--- a/plugins/dora/impl/impl.go
+++ b/plugins/dora/impl/impl.go
@@ -21,7 +21,6 @@ import (
 	"fmt"
 
 	"github.com/apache/incubator-devlake/errors"
-	"github.com/apache/incubator-devlake/migration"
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/dora/models/migrationscripts"
 	"github.com/apache/incubator-devlake/plugins/dora/tasks"
@@ -34,6 +33,7 @@ var _ core.PluginMeta = (*Dora)(nil)
 var _ core.PluginInit = (*Dora)(nil)
 var _ core.PluginTask = (*Dora)(nil)
 var _ core.CloseablePluginTask = (*Dora)(nil)
+var _ core.PluginMigration = (*Dora)(nil)
 
 type Dora struct{}
 
@@ -80,7 +80,7 @@ func (plugin Dora) RootPkgPath() string {
 	return "github.com/apache/incubator-devlake/plugins/dora"
 }
 
-func (plugin Dora) MigrationScripts() []migration.Script {
+func (plugin Dora) MigrationScripts() []core.MigrationScript {
 	return migrationscripts.All()
 }
 
diff --git a/plugins/dora/models/migrationscripts/20220829_add_init_tables.go b/plugins/dora/models/migrationscripts/20220829_add_init_tables.go
deleted file mode 100644
index 84496384..00000000
--- a/plugins/dora/models/migrationscripts/20220829_add_init_tables.go
+++ /dev/null
@@ -1,41 +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 migrationscripts
-
-import (
-	"context"
-	"github.com/apache/incubator-devlake/errors"
-	"gorm.io/gorm"
-)
-
-type addInitTables struct{}
-
-func (u *addInitTables) Up(ctx context.Context, db *gorm.DB) errors.Error {
-	err := db.Migrator().AutoMigrate(
-	// TODO add you models
-	)
-	return errors.Convert(err)
-}
-
-func (*addInitTables) Version() uint64 {
-	return 20220829000001
-}
-
-func (*addInitTables) Name() string {
-	return "dora init schemas"
-}
diff --git a/plugins/dora/models/migrationscripts/20220928_add_dora_benchmark.go b/plugins/dora/models/migrationscripts/20220928_add_dora_benchmark.go
index 6ce33839..99907009 100644
--- a/plugins/dora/models/migrationscripts/20220928_add_dora_benchmark.go
+++ b/plugins/dora/models/migrationscripts/20220928_add_dora_benchmark.go
@@ -18,15 +18,15 @@ limitations under the License.
 package migrationscripts
 
 import (
-	"context"
 	"github.com/apache/incubator-devlake/errors"
+	"github.com/apache/incubator-devlake/helpers/migrationhelper"
 	"github.com/apache/incubator-devlake/models/migrationscripts/archived"
-	"gorm.io/gorm"
+	"github.com/apache/incubator-devlake/plugins/core"
 )
 
 type addDoraBenchmark struct{}
 
-type DoraBenchmark struct {
+type doraBenchmark struct {
 	archived.Model
 	Metric string `gorm:"type:varchar(255)"`
 	Low    string `gorm:"type:varchar(255)"`
@@ -35,29 +35,29 @@ type DoraBenchmark struct {
 	Elite  string `gorm:"type:varchar(255)"`
 }
 
-func (DoraBenchmark) TableName() string {
+func (doraBenchmark) TableName() string {
 	return "dora_benchmarks"
 }
 
-func (u *addDoraBenchmark) Up(ctx context.Context, db *gorm.DB) errors.Error {
-
-	err := db.Migrator().AutoMigrate(
-		&DoraBenchmark{},
+func (u *addDoraBenchmark) Up(baseRes core.BasicRes) errors.Error {
+	db := baseRes.GetDal()
+	err := migrationhelper.AutoMigrateTables(
+		baseRes,
+		&doraBenchmark{},
 	)
 	if err != nil {
-		return errors.Convert(err)
+		return err
 	}
+
 	defer func() {
 		if err != nil {
-			if db.Migrator().HasTable(&DoraBenchmark{}) {
-				err = db.Migrator().DropTable(&DoraBenchmark{})
-				if err != nil {
-					return
-				}
+			err = db.DropTables(&doraBenchmark{})
+			if err != nil {
+				return
 			}
 		}
 	}()
-	doraBenchmarkDF := &DoraBenchmark{
+	doraBenchmarkDF := &doraBenchmark{
 		Model: archived.Model{
 			ID: 1,
 		},
@@ -67,11 +67,11 @@ func (u *addDoraBenchmark) Up(ctx context.Context, db *gorm.DB) errors.Error {
 		High:   "Between once per week and once per month",
 		Elite:  "On-demand",
 	}
-	err = db.Create(doraBenchmarkDF).Error
+	err = db.Create(doraBenchmarkDF)
 	if err != nil {
 		return errors.Convert(err)
 	}
-	doraBenchmarkLTC := &DoraBenchmark{
+	doraBenchmarkLTC := &doraBenchmark{
 		Model: archived.Model{
 			ID: 2,
 		},
@@ -81,11 +81,11 @@ func (u *addDoraBenchmark) Up(ctx context.Context, db *gorm.DB) errors.Error {
 		High:   "Less than one week",
 		Elite:  "Less than one hour",
 	}
-	err = db.Create(doraBenchmarkLTC).Error
+	err = db.Create(doraBenchmarkLTC)
 	if err != nil {
 		return errors.Convert(err)
 	}
-	doraBenchmarkTTS := &DoraBenchmark{
+	doraBenchmarkTTS := &doraBenchmark{
 		Model: archived.Model{
 			ID: 3,
 		},
@@ -95,11 +95,11 @@ func (u *addDoraBenchmark) Up(ctx context.Context, db *gorm.DB) errors.Error {
 		High:   "Less than one day",
 		Elite:  "Less than one hour",
 	}
-	err = db.Create(doraBenchmarkTTS).Error
+	err = db.Create(doraBenchmarkTTS)
 	if err != nil {
 		return errors.Convert(err)
 	}
-	doraBenchmarkCFR := &DoraBenchmark{
+	doraBenchmarkCFR := &doraBenchmark{
 		Model: archived.Model{
 			ID: 4,
 		},
@@ -109,7 +109,7 @@ func (u *addDoraBenchmark) Up(ctx context.Context, db *gorm.DB) errors.Error {
 		High:   "16%-20%",
 		Elite:  "0-15%",
 	}
-	err = db.Create(doraBenchmarkCFR).Error
+	err = db.Create(doraBenchmarkCFR)
 	if err != nil {
 		return errors.Convert(err)
 	}
diff --git a/plugins/dora/models/migrationscripts/register.go b/plugins/dora/models/migrationscripts/register.go
index 693f9a81..82fa4c4c 100644
--- a/plugins/dora/models/migrationscripts/register.go
+++ b/plugins/dora/models/migrationscripts/register.go
@@ -17,12 +17,11 @@ limitations under the License.
 
 package migrationscripts
 
-import "github.com/apache/incubator-devlake/migration"
+import "github.com/apache/incubator-devlake/plugins/core"
 
 // All return all the migration scripts
-func All() []migration.Script {
-	return []migration.Script{
-		new(addInitTables),
+func All() []core.MigrationScript {
+	return []core.MigrationScript{
 		new(addDoraBenchmark),
 	}
 }