1
0
镜像自地址 https://github.com/astaxie/beego.git 已同步 2025-07-17 18:52:16 +00:00

Merge pull request #4211 from flycash/adt/all

allow users to ignore some table when run orm commands
这个提交包含在:
Ming Deng
2020-09-07 21:52:39 +08:00
提交者 GitHub
当前提交 9ccd58bfff
共有 5 个文件被更改,包括 59 次插入1 次删除

查看文件

@@ -142,6 +142,12 @@ func (d *commandSyncDb) Run() error {
}
for i, mi := range modelCache.allOrdered() {
if !isApplicableTableForDB(mi.addrField, d.al.Name) {
fmt.Printf("table `%s` is not applicable to database '%s'\n", mi.table, d.al.Name)
continue
}
if tables[mi.table] {
if !d.noInfo {
fmt.Printf("table `%s` already exists, skip\n", mi.table)

查看文件

@@ -414,7 +414,7 @@ func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) {
for _, mi := range modelCache.allOrdered() {
queries = append(queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s%s%s`, Q, mi.table, Q))
}
return queries,nil
return queries, nil
}
//getDbCreateSQL get database scheme creation sql queries

查看文件

@@ -107,6 +107,18 @@ func getTableUnique(val reflect.Value) [][]string {
return nil
}
// get whether the table needs to be created for the database alias
func isApplicableTableForDB(val reflect.Value, db string) bool {
fun := val.MethodByName("IsApplicableTableForDB")
if fun.IsValid() {
vals := fun.Call([]reflect.Value{reflect.ValueOf(db)})
if len(vals) > 0 && vals[0].Kind() == reflect.Bool {
return vals[0].Bool()
}
}
return true
}
// get snaked column name
func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string {
column := col

查看文件

@@ -0,0 +1,35 @@
// Copyright 2020
//
// Licensed 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 orm
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type NotApplicableModel struct {
Id int
}
func (n *NotApplicableModel) IsApplicableTableForDB(db string) bool {
return db == "default"
}
func Test_IsApplicableTableForDB(t *testing.T) {
assert.False(t, isApplicableTableForDB(reflect.ValueOf(&NotApplicableModel{}), "defa"))
assert.True(t, isApplicableTableForDB(reflect.ValueOf(&NotApplicableModel{}), "default"))
}

查看文件

@@ -75,6 +75,11 @@ type TableUniqueI interface {
TableUnique() [][]string
}
// IsApplicableTableForDB if return false, we won't create table to this db
type IsApplicableTableForDB interface {
IsApplicableTableForDB(db string) bool
}
// Driver define database driver
type Driver interface {
Name() string