(statement string, backupItem *storepb.PriorBackupDetail_Item)
| 337 | } |
| 338 | |
| 339 | func extractSQL(statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error) { |
| 340 | if backupItem == nil { |
| 341 | return "", errors.New("backup item is nil") |
| 342 | } |
| 343 | |
| 344 | list, err := SplitSQL(statement) |
| 345 | if err != nil { |
| 346 | return "", errors.Wrapf(err, "failed to split SQL") |
| 347 | } |
| 348 | |
| 349 | start := 0 |
| 350 | end := len(list) - 1 |
| 351 | for i, item := range list { |
| 352 | if equalOrLess(item.Start, backupItem.StartPosition) { |
| 353 | start = i |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | for i := len(list) - 1; i >= 0; i-- { |
| 358 | if equalOrGreater(list[i].Start, backupItem.EndPosition) { |
| 359 | end = i |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | _, sourceDatabase, err := common.GetInstanceDatabaseID(backupItem.SourceTable.Database) |
| 364 | if err != nil { |
| 365 | return "", errors.Wrapf(err, "failed to get source database ID for %s", backupItem.SourceTable.Database) |
| 366 | } |
| 367 | |
| 368 | var result []string |
| 369 | for i := start; i <= end; i++ { |
| 370 | containsSourceTable := false |
| 371 | tables, err := prepareTransformation(sourceDatabase, list[i].Text) |
| 372 | if err != nil { |
| 373 | if err == errNoBackupableDML { |
| 374 | continue |
| 375 | } |
| 376 | return "", errors.Wrap(err, "failed to prepare transformation") |
| 377 | } |
| 378 | for _, table := range tables { |
| 379 | if table.table.Schema == sourceDatabase && table.table.Table == backupItem.SourceTable.Table { |
| 380 | containsSourceTable = true |
| 381 | break |
| 382 | } |
| 383 | } |
| 384 | if containsSourceTable { |
| 385 | result = append(result, normalizeExtractedRestoreSQL(list[i].Text)) |
| 386 | } |
| 387 | } |
| 388 | return strings.Join(result, "\n"), nil |
| 389 | } |
| 390 | |
| 391 | func normalizeExtractedRestoreSQL(statement string) string { |
| 392 | return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(statement), ";")) |
no test coverage detected