diff --git a/cmd/loadjson/main.go b/cmd/loadjson/main.go index 5c370f03..2331176d 100644 --- a/cmd/loadjson/main.go +++ b/cmd/loadjson/main.go @@ -147,9 +147,11 @@ func processFile(ctx context.Context, path string) { // Note that this step can succeed with any valid JSON file. But we need to do some additional validation to skip // invalid query JSON files. if unmarshalErr := json.Unmarshal(bytes, queryInfo); unmarshalErr != nil { + log.Error().Err(unmarshalErr).Str("path", path).Msg("failed to unmarshal JSON") return } if queryInfo.QueryId == "" || queryInfo.QueryStats == nil || queryInfo.QueryStats.CreateTime == nil { + log.Error().Msg("QueryId, QueryStats or QueryStats.CreateTime is empty") return } log.Info().Str("path", path).Msg("start to process file") diff --git a/utils/orm.go b/utils/orm.go index 8e2a9787..85eb8978 100644 --- a/utils/orm.go +++ b/utils/orm.go @@ -49,13 +49,16 @@ func SqlInsertObject(ctx context.Context, db *sql.DB, obj any, tableNames ...Tab if len(rows) == 0 { continue } - placeholders := strings.Repeat("?,", rows[0].ColumnCount()) - // Get rid of the trailing comma. - placeholders = placeholders[:len(placeholders)-1] - sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", - table, strings.Join(rows[0].ColumnNames, ","), placeholders) + if len(rows) == 1 && rows[0].ColumnCount() <= 1 { + continue + } for _, row := range rows { + placeholders := strings.Repeat("?,", row.ColumnCount()) + // Get rid of the trailing comma. + placeholders = placeholders[:len(placeholders)-1] + sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", + table, strings.Join(row.ColumnNames, ","), placeholders) //log.Info().Str("sql", sqlStmt).Array("values", log.NewMarshaller(row.Values)).Msg("execute sql") _, err := tx.Exec(sqlStmt, row.Values...) if err != nil {