MCPcopy Index your code
hub / github.com/apache/devlake / CopyTableColumns

Function CopyTableColumns

backend/helpers/migrationhelper/migrationhelper.go:301–345  ·  view source on GitHub ↗

CopyTableColumns can copy data from src table to dst table

(
	basicRes context.BasicRes,
	srcTableName string,
	dstTableName string,
	transform func(*S) (*D, errors.Error),
)

Source from the content-addressed store, hash-verified

299
300// CopyTableColumns can copy data from src table to dst table
301func CopyTableColumns[S any, D any](
302 basicRes context.BasicRes,
303 srcTableName string,
304 dstTableName string,
305 transform func(*S) (*D, errors.Error),
306) (err errors.Error) {
307 db := basicRes.GetDal()
308
309 cursor, err := db.Cursor(dal.From(srcTableName))
310 if err != nil {
311 return errors.Default.Wrap(err, fmt.Sprintf("failed to load data from src table [%s]", srcTableName))
312 }
313 if !reflect.ValueOf(cursor).IsNil() {
314 defer cursor.Close()
315 }
316
317 batch, err := helper.NewBatchSave(basicRes, reflect.TypeOf(new(D)), 200, dstTableName)
318 if err != nil {
319 return errors.Default.Wrap(err, fmt.Sprintf("failed to instantiate BatchSave for table [%s]", srcTableName))
320 }
321 defer batch.Close()
322
323 if reflect.ValueOf(cursor).IsNil() {
324 return nil
325 }
326 for cursor.Next() {
327 srcTable := new(S)
328 err1 := db.Fetch(cursor, srcTable)
329 if err1 != nil {
330 return errors.Default.Wrap(err1, fmt.Sprintf("fail to load record from table [%s]", srcTableName))
331 }
332
333 dst, err1 := transform(srcTable)
334 if err1 != nil {
335 return errors.Default.Wrap(err1, fmt.Sprintf("failed to transform row %v", srcTable))
336 }
337
338 err1 = batch.Add(dst)
339 if err1 != nil {
340 return errors.Default.Wrap(err1, fmt.Sprintf("push to BatchSave failed %v", dstTableName))
341 }
342 }
343
344 return err
345}
346
347// PrimarykeyIsAutoIncrement check if the Primarykey is auto increment
348func PrimarykeyIsAutoIncrement(db dal.Dal, tableName string) errors.Error {

Callers 3

TransformTableFunction · 0.85
TestCopyTableColumnsFunction · 0.85

Calls 9

CloseMethod · 0.95
AddMethod · 0.95
WrapMethod · 0.80
GetDalMethod · 0.65
CursorMethod · 0.65
FromMethod · 0.65
CloseMethod · 0.65
NextMethod · 0.65
FetchMethod · 0.65

Tested by 2

TestCopyTableColumnsFunction · 0.68