Enhance seed import functionality with detailed logging and column filtering
- Added detailed logging for import results in TestSeedImportYao to aid debugging. - Updated import functions to filter columns based on model definitions, ensuring only valid columns are processed. - Introduced a sorting mechanism for column names to maintain consistent ordering during imports.
This commit is contained in:
parent
2e2abf1d5f
commit
9c82f12eca
2 changed files with 37 additions and 4 deletions
31
seed/seed.go
31
seed/seed.go
|
|
@ -243,12 +243,17 @@ func importDataFromJSON(filename string, mod *model.Model, options ImportOption,
|
|||
return nil
|
||||
}
|
||||
|
||||
// Extract columns from first record
|
||||
// Extract columns from first record, but only include columns that exist in model
|
||||
columns := []string{}
|
||||
for key := range records[0] {
|
||||
columns = append(columns, key)
|
||||
if _, exists := mod.Columns[key]; exists {
|
||||
columns = append(columns, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort columns for consistent ordering
|
||||
sortColumns(columns)
|
||||
|
||||
// Convert to rows format
|
||||
handler := createJSONImportHandler(mod, columns, options, result)
|
||||
|
||||
|
|
@ -295,12 +300,17 @@ func importDataFromYao(filename string, mod *model.Model, options ImportOption,
|
|||
return nil
|
||||
}
|
||||
|
||||
// Extract columns from first record
|
||||
// Extract columns from first record, but only include columns that exist in model
|
||||
columns := []string{}
|
||||
for key := range records[0] {
|
||||
columns = append(columns, key)
|
||||
if _, exists := mod.Columns[key]; exists {
|
||||
columns = append(columns, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort columns for consistent ordering
|
||||
sortColumns(columns)
|
||||
|
||||
// Convert to rows format
|
||||
handler := createJSONImportHandler(mod, columns, options, result)
|
||||
|
||||
|
|
@ -551,3 +561,16 @@ func parseJSONField(value interface{}, columnType string) interface{} {
|
|||
|
||||
return jsonValue
|
||||
}
|
||||
|
||||
// sortColumns sorts column names alphabetically for consistent ordering
|
||||
func sortColumns(columns []string) {
|
||||
// Simple bubble sort for small arrays
|
||||
n := len(columns)
|
||||
for i := 0; i < n-1; i++ {
|
||||
for j := 0; j < n-i-1; j++ {
|
||||
if columns[j] > columns[j+1] {
|
||||
columns[j], columns[j+1] = columns[j+1], columns[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,6 +160,16 @@ func TestSeedImportYao(t *testing.T) {
|
|||
assert.NotNil(t, result)
|
||||
resultMap, ok := result.(*ImportResult)
|
||||
assert.True(t, ok, "Result should be ImportResult")
|
||||
|
||||
// Print detailed result for debugging
|
||||
if resultMap.Total == 0 || resultMap.Success == 0 {
|
||||
t.Logf("Import result: Total=%d, Success=%d, Failure=%d, Ignore=%d",
|
||||
resultMap.Total, resultMap.Success, resultMap.Failure, resultMap.Ignore)
|
||||
if len(resultMap.Errors) > 0 {
|
||||
t.Logf("Errors: %+v", resultMap.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record")
|
||||
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue