Merge pull request #1217 from trheyi/main
Enhance seed import functionality with detailed logging and column fi…
This commit is contained in:
commit
a2fedc8ae2
2 changed files with 37 additions and 4 deletions
27
seed/seed.go
27
seed/seed.go
|
|
@ -243,11 +243,16 @@ func importDataFromJSON(filename string, mod *model.Model, options ImportOption,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract columns from first record
|
// Extract columns from first record, but only include columns that exist in model
|
||||||
columns := []string{}
|
columns := []string{}
|
||||||
for key := range records[0] {
|
for key := range records[0] {
|
||||||
|
if _, exists := mod.Columns[key]; exists {
|
||||||
columns = append(columns, key)
|
columns = append(columns, key)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort columns for consistent ordering
|
||||||
|
sortColumns(columns)
|
||||||
|
|
||||||
// Convert to rows format
|
// Convert to rows format
|
||||||
handler := createJSONImportHandler(mod, columns, options, result)
|
handler := createJSONImportHandler(mod, columns, options, result)
|
||||||
|
|
@ -295,11 +300,16 @@ func importDataFromYao(filename string, mod *model.Model, options ImportOption,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract columns from first record
|
// Extract columns from first record, but only include columns that exist in model
|
||||||
columns := []string{}
|
columns := []string{}
|
||||||
for key := range records[0] {
|
for key := range records[0] {
|
||||||
|
if _, exists := mod.Columns[key]; exists {
|
||||||
columns = append(columns, key)
|
columns = append(columns, key)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort columns for consistent ordering
|
||||||
|
sortColumns(columns)
|
||||||
|
|
||||||
// Convert to rows format
|
// Convert to rows format
|
||||||
handler := createJSONImportHandler(mod, columns, options, result)
|
handler := createJSONImportHandler(mod, columns, options, result)
|
||||||
|
|
@ -551,3 +561,16 @@ func parseJSONField(value interface{}, columnType string) interface{} {
|
||||||
|
|
||||||
return jsonValue
|
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)
|
assert.NotNil(t, result)
|
||||||
resultMap, ok := result.(*ImportResult)
|
resultMap, ok := result.(*ImportResult)
|
||||||
assert.True(t, ok, "Result should be 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.Total, 0, "Should import at least 1 record")
|
||||||
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
|
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue