| 24 | } |
| 25 | |
| 26 | func GenerateRestoreSQL(ctx context.Context, rCtx base.RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item) (string, error) { |
| 27 | originalSQL, err := extractStatement(statement, backupItem) |
| 28 | if err != nil { |
| 29 | return "", errors.Errorf("failed to extract single SQL: %v", err) |
| 30 | } |
| 31 | |
| 32 | if len(originalSQL) == 0 { |
| 33 | return "", errors.Errorf("no original SQL") |
| 34 | } |
| 35 | |
| 36 | // Find the matching DML statement node at the backup position. |
| 37 | matchingNode, err := findStatementAtPosition(statement, backupItem) |
| 38 | if err != nil { |
| 39 | return "", err |
| 40 | } |
| 41 | if matchingNode == nil { |
| 42 | return "", errors.Errorf("could not find statement at position (line %d:%d - %d:%d)", |
| 43 | backupItem.StartPosition.Line, backupItem.StartPosition.Column, |
| 44 | backupItem.EndPosition.Line, backupItem.EndPosition.Column) |
| 45 | } |
| 46 | |
| 47 | sqlForComment, truncated := common.TruncateString(originalSQL, maxCommentLength) |
| 48 | if truncated { |
| 49 | sqlForComment += "..." |
| 50 | } |
| 51 | |
| 52 | prependStatements, err := getPrependStatements(statement) |
| 53 | if err != nil { |
| 54 | return "", errors.Wrap(err, "failed to get prepend statements") |
| 55 | } |
| 56 | |
| 57 | return doGenerate(ctx, rCtx, sqlForComment, matchingNode, backupItem, prependStatements) |
| 58 | } |
| 59 | |
| 60 | func findStatementAtPosition(statement string, backupItem *storepb.PriorBackupDetail_Item) (ast.Node, error) { |
| 61 | stmts, err := ParsePg(statement) |