You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/09/07 12:09:10 UTC

[GitHub] [incubator-devlake] klesh commented on a diff in pull request #2911: feat: implement API for plugin `customize`

klesh commented on code in PR #2911:
URL: https://github.com/apache/incubator-devlake/pull/2911#discussion_r964527893


##########
plugins/customize/api/api.go:
##########
@@ -0,0 +1,156 @@
+/*
+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 api
+
+import (
+	"errors"
+	"fmt"
+	"net/http"
+	"strings"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	"github.com/apache/incubator-devlake/plugins/customize/models"
+	"gorm.io/gorm/clause"
+)
+
+type field struct {
+	ColumnName string `json:"columnName"`
+	ColumnType string `json:"columnType"`
+}
+
+func getFields(d dal.Dal, tbl string) ([]field, error) {

Review Comment:
   all `error`s should be replaced by `errors.Error` our customized error by @keon94 



##########
plugins/customize/api/api.go:
##########
@@ -0,0 +1,156 @@
+/*
+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 api
+
+import (
+	"errors"
+	"fmt"
+	"net/http"
+	"strings"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	"github.com/apache/incubator-devlake/plugins/customize/models"
+	"gorm.io/gorm/clause"
+)
+
+type field struct {
+	ColumnName string `json:"columnName"`
+	ColumnType string `json:"columnType"`
+}
+
+func getFields(d dal.Dal, tbl string) ([]field, error) {
+	columns, err := d.GetColumns(&models.Table{tbl}, func(columnMeta dal.ColumnMeta) bool {
+		return strings.HasPrefix(columnMeta.Name(), "x_")
+	})
+	if err != nil {
+		return nil, err
+	}
+	var result []field
+	for _, col := range columns {
+		result = append(result, field{
+			ColumnName: col.Name(),
+			ColumnType: "VARCHAR(255)",
+		})
+	}
+	return result, nil
+}
+func checkField(d dal.Dal, table, field string) (bool, error) {
+	if !strings.HasPrefix(field, "x_") {
+		return false, errors.New("column name should start with `x_`")
+	}
+	fields, err := getFields(d, table)
+	if err != nil {
+		return false, err
+	}
+	for _, fld := range fields {
+		if fld.ColumnName == field {
+			return true, nil
+		}
+	}
+	return false, nil
+}
+
+func CreateField(d dal.Dal, table, field string) error {
+	exists, err := checkField(d, table, field)
+	if err != nil {
+		return err
+	}
+	if exists {
+		return nil
+	}
+	return d.Exec("ALTER TABLE ? ADD ? VARCHAR(255)", clause.Table{Name: table}, clause.Column{Name: field})

Review Comment:
   Can we delegate the implementation(sql) through the `Dal` interface?



##########
plugins/customize/api/api.go:
##########
@@ -0,0 +1,156 @@
+/*
+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 api
+
+import (
+	"errors"
+	"fmt"
+	"net/http"
+	"strings"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	"github.com/apache/incubator-devlake/plugins/customize/models"
+	"gorm.io/gorm/clause"
+)
+
+type field struct {
+	ColumnName string `json:"columnName"`
+	ColumnType string `json:"columnType"`
+}
+
+func getFields(d dal.Dal, tbl string) ([]field, error) {
+	columns, err := d.GetColumns(&models.Table{tbl}, func(columnMeta dal.ColumnMeta) bool {
+		return strings.HasPrefix(columnMeta.Name(), "x_")
+	})
+	if err != nil {
+		return nil, err
+	}
+	var result []field
+	for _, col := range columns {
+		result = append(result, field{
+			ColumnName: col.Name(),
+			ColumnType: "VARCHAR(255)",
+		})
+	}
+	return result, nil
+}
+func checkField(d dal.Dal, table, field string) (bool, error) {
+	if !strings.HasPrefix(field, "x_") {
+		return false, errors.New("column name should start with `x_`")
+	}
+	fields, err := getFields(d, table)
+	if err != nil {
+		return false, err
+	}
+	for _, fld := range fields {
+		if fld.ColumnName == field {
+			return true, nil
+		}
+	}
+	return false, nil
+}
+
+func CreateField(d dal.Dal, table, field string) error {
+	exists, err := checkField(d, table, field)
+	if err != nil {
+		return err
+	}
+	if exists {
+		return nil
+	}
+	return d.Exec("ALTER TABLE ? ADD ? VARCHAR(255)", clause.Table{Name: table}, clause.Column{Name: field})
+}
+
+func deleteField(d dal.Dal, table, field string) error {
+	exists, err := checkField(d, table, field)
+	if err != nil {
+		return err
+	}
+	if !exists {
+		return nil
+	}
+	return d.Exec("ALTER TABLE ? DROP COLUMN ?", clause.Table{Name: table}, clause.Column{Name: field})

Review Comment:
   Same as above



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org