-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jeff Ortel <[email protected]>
- Loading branch information
Showing
10 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package database | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/konveyor/tackle2-hub/model" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
// PK singleton pk sequence. | ||
var PK PkSequence | ||
|
||
// PkSequence provides a primary key sequence. | ||
type PkSequence struct { | ||
mutex sync.Mutex | ||
} | ||
|
||
// Load highest key for all models. | ||
func (r *PkSequence) Load(db *gorm.DB, models []any) (err error) { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
for _, m := range models { | ||
mt := reflect.TypeOf(m) | ||
if mt.Kind() == reflect.Ptr { | ||
mt = mt.Elem() | ||
} | ||
kind := strings.ToUpper(mt.Name()) | ||
db = r.session(db) | ||
q := db.Table(kind) | ||
q = q.Select("MAX(ID) id") | ||
cursor, err := q.Rows() | ||
if err != nil || !cursor.Next() { | ||
// not a table with id. | ||
// discarded. | ||
continue | ||
} | ||
id := int64(0) | ||
err = cursor.Scan(&id) | ||
_ = cursor.Close() | ||
if err != nil { | ||
r.add(db, kind, uint(0)) | ||
} else { | ||
r.add(db, kind, uint(id)) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Next returns the next primary key. | ||
func (r *PkSequence) Next(db *gorm.DB) (id uint) { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
kind := strings.ToUpper(db.Statement.Table) | ||
m := &model.PK{} | ||
db = r.session(db) | ||
err := db.First(m, "Kind", kind).Error | ||
if err != nil { | ||
return | ||
} | ||
m.LastID++ | ||
id = m.LastID | ||
err = db.Save(m).Error | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
|
||
// session returns a new DB with a new session. | ||
func (r *PkSequence) session(in *gorm.DB) (out *gorm.DB) { | ||
out = &gorm.DB{ | ||
Config: in.Config, | ||
} | ||
out.Config.Logger.LogMode(logger.Warn) | ||
out.Statement = &gorm.Statement{ | ||
DB: out, | ||
ConnPool: in.Statement.ConnPool, | ||
Context: in.Statement.Context, | ||
Clauses: map[string]clause.Clause{}, | ||
Vars: make([]interface{}, 0, 8), | ||
} | ||
return | ||
} | ||
|
||
// add the last (higher) id for the kind. | ||
func (r *PkSequence) add(db *gorm.DB, kind string, id uint) { | ||
m := &model.PK{Kind: kind} | ||
db = r.session(db) | ||
err := db.First(m).Error | ||
if err != nil { | ||
if !errors.Is(err, gorm.ErrRecordNotFound) { | ||
panic(err) | ||
} | ||
} | ||
if m.LastID > id { | ||
return | ||
} | ||
m.LastID = id | ||
db = r.session(db) | ||
err = db.Save(m).Error | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// assignPk assigns PK as needed. | ||
func assignPk(db *gorm.DB) { | ||
statement := db.Statement | ||
schema := statement.Schema | ||
if schema == nil { | ||
return | ||
} | ||
switch statement.ReflectValue.Kind() { | ||
case reflect.Slice, | ||
reflect.Array: | ||
for i := 0; i < statement.ReflectValue.Len(); i++ { | ||
for _, f := range schema.Fields { | ||
if f.Name != "ID" { | ||
continue | ||
} | ||
_, isZero := f.ValueOf( | ||
statement.Context, | ||
statement.ReflectValue.Index(i)) | ||
if isZero { | ||
id := PK.Next(db) | ||
_ = f.Set( | ||
statement.Context, | ||
statement.ReflectValue.Index(i), | ||
id) | ||
|
||
} | ||
break | ||
} | ||
} | ||
case reflect.Struct: | ||
for _, f := range schema.Fields { | ||
if f.Name != "ID" { | ||
continue | ||
} | ||
_, isZero := f.ValueOf( | ||
statement.Context, | ||
statement.ReflectValue) | ||
if isZero { | ||
id := PK.Next(db) | ||
_ = f.Set( | ||
statement.Context, | ||
statement.ReflectValue, | ||
id) | ||
} | ||
break | ||
} | ||
default: | ||
log.Info("[WARN] assignPk: unknown kind.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ func All() []any { | |
ImportTag{}, | ||
JobFunction{}, | ||
MigrationWave{}, | ||
PK{}, | ||
Proxy{}, | ||
Review{}, | ||
Setting{}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters