UpdateCSVs scans the supplied directory for .csv files and applies the `updater` function to each. It saves back the result. This is useful when you need to bulk-change a lot of entries across many CSV files in a systematic way.
(csvDir string, updater func(fileName string, record *pluginhelper.CsvRecord))
| 257 | // UpdateCSVs scans the supplied directory for .csv files and applies the `updater` function to each. It saves back the result. |
| 258 | // This is useful when you need to bulk-change a lot of entries across many CSV files in a systematic way. |
| 259 | func (t *DataFlowTester) UpdateCSVs(csvDir string, updater func(fileName string, record *pluginhelper.CsvRecord)) errors.Error { |
| 260 | var csvFiles []string |
| 261 | err := filepath.WalkDir(csvDir, func(file string, dir fs.DirEntry, e error) error { |
| 262 | if e != nil { |
| 263 | return e |
| 264 | } |
| 265 | if filepath.Ext(file) == ".csv" { |
| 266 | csvFiles = append(csvFiles, file) |
| 267 | } |
| 268 | return nil |
| 269 | }) |
| 270 | if err != nil { |
| 271 | return errors.Convert(err) |
| 272 | } |
| 273 | for _, csvFile := range csvFiles { |
| 274 | csvIter, err := pluginhelper.NewCsvFileIterator(csvFile) |
| 275 | if err != nil { |
| 276 | return errors.Convert(err) |
| 277 | } |
| 278 | columns := csvIter.GetColumns() |
| 279 | var entries [][]string |
| 280 | func() { |
| 281 | defer csvIter.Close() |
| 282 | for csvIter.HasNext() { |
| 283 | records := csvIter.FetchRecords() |
| 284 | var csvEntry []string |
| 285 | for _, record := range records { |
| 286 | updater(filepath.Base(csvFile), record) |
| 287 | csvEntry = append(csvEntry, cast.ToString(record.Value)) |
| 288 | } |
| 289 | entries = append(entries, csvEntry) |
| 290 | } |
| 291 | }() |
| 292 | if len(entries) == 0 { |
| 293 | continue |
| 294 | } |
| 295 | func() { |
| 296 | csvWriter, _ := pluginhelper.NewCsvFileWriter(csvFile, columns) |
| 297 | defer csvWriter.Close() |
| 298 | for _, entry := range entries { |
| 299 | csvWriter.Write(entry) |
| 300 | } |
| 301 | csvWriter.Flush() |
| 302 | }() |
| 303 | } |
| 304 | return nil |
| 305 | } |
| 306 | |
| 307 | // CreateSnapshot reads rows from database and write them into .csv file. |
| 308 | func (t *DataFlowTester) CreateSnapshot(dst schema.Tabler, opts TableOptions) { |
nothing calls this directly
no test coverage detected