| 244 | } |
| 245 | |
| 246 | func TestSQLitePrePostCommands(t *testing.T) { |
| 247 | // This test adds some pre-run and post-run SQLite commands and tests that |
| 248 | // their effects are present after running the writer. |
| 249 | fn, rm := testutil.TempFile(t) |
| 250 | defer rm() |
| 251 | |
| 252 | config := SQLiteWriterConfig{ |
| 253 | PathString: fn, |
| 254 | TableName: "lines", |
| 255 | PreRun: []string{"CREATE TABLE footable ( v INT )", "INSERT INTO footable ( v ) VALUES ( 55 )"}, |
| 256 | PostRun: []string{"INSERT INTO footable ( v ) VALUES ( 56 )", "CREATE INDEX footable_v ON footable ( v )"}, |
| 257 | } |
| 258 | |
| 259 | fieldByName := func(name string) (baker.FieldIndex, bool) { |
| 260 | switch name { |
| 261 | case "field0": |
| 262 | return 0, true |
| 263 | case "field1": |
| 264 | return 1, true |
| 265 | case "field2": |
| 266 | return 2, true |
| 267 | } |
| 268 | return 0, false |
| 269 | } |
| 270 | |
| 271 | fieldNames := []string{"field0", "field1", "field2"} |
| 272 | |
| 273 | cfg := baker.OutputParams{ |
| 274 | Fields: []baker.FieldIndex{0, 1, 2}, |
| 275 | Index: 0, |
| 276 | ComponentParams: baker.ComponentParams{ |
| 277 | DecodedConfig: &config, |
| 278 | FieldByName: fieldByName, |
| 279 | FieldNames: fieldNames, |
| 280 | }, |
| 281 | } |
| 282 | writer, err := newSQLiteWriter(false)(cfg) |
| 283 | if err != nil { |
| 284 | t.Fatalf("SQLite writer creation failed: %s", err) |
| 285 | } |
| 286 | |
| 287 | outch := make(chan baker.OutputRecord) |
| 288 | upch := make(chan string) |
| 289 | |
| 290 | // We are not actually putting any lines in this test so close record |
| 291 | // channel right away. |
| 292 | close(outch) |
| 293 | |
| 294 | go func() { |
| 295 | for range upch { |
| 296 | } |
| 297 | }() |
| 298 | |
| 299 | writer.Run(outch, upch) |
| 300 | close(upch) |
| 301 | |
| 302 | conn, err := sql.Open("sqlite3", fn) |
| 303 | if err != nil { |