MCPcopy Index your code
hub / github.com/bytebase/bytebase / GenerateRestoreSQL

Function GenerateRestoreSQL

backend/plugin/parser/pg/restore.go:26–58  ·  view source on GitHub ↗
(ctx context.Context, rCtx base.RestoreContext, statement string, backupItem *storepb.PriorBackupDetail_Item)

Source from the content-addressed store, hash-verified

24}
25
26func 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
60func findStatementAtPosition(statement string, backupItem *storepb.PriorBackupDetail_Item) (ast.Node, error) {
61 stmts, err := ParsePg(statement)

Callers 1

TestRestoreFunction · 0.70

Calls 6

TruncateStringFunction · 0.92
findStatementAtPositionFunction · 0.85
ErrorfMethod · 0.80
extractStatementFunction · 0.70
getPrependStatementsFunction · 0.70
doGenerateFunction · 0.70

Tested by 1

TestRestoreFunction · 0.56