MCPcopy Create free account
hub / github.com/bytebase/bytebase / generateUpdateRestore

Function generateUpdateRestore

backend/plugin/parser/mysql/restore.go:105–149  ·  view source on GitHub ↗
(ctx context.Context, rCtx base.RestoreContext, stmt *ast.UpdateStmt, originalDatabase, originalTable, backupDatabase, backupTable string, normalColumns []string)

Source from the content-addressed store, hash-verified

103}
104
105func generateUpdateRestore(ctx context.Context, rCtx base.RestoreContext, stmt *ast.UpdateStmt, originalDatabase, originalTable, backupDatabase, backupTable string, normalColumns []string) (string, error) {
106 // Extract single tables from the UPDATE table references.
107 singleTables := extractSingleTablesFromTableExprs(originalDatabase, stmt.Tables)
108
109 // Find the table matching the original table.
110 var matchedTable *TableReference
111 for _, table := range singleTables {
112 if strings.EqualFold(table.Table, originalTable) {
113 matchedTable = table
114 break
115 }
116 }
117
118 // Extract update column names that belong to the original table.
119 updateColumns := extractUpdateColumns(stmt.SetList, originalDatabase, originalTable, matchedTable, normalColumns)
120
121 has, err := hasDisjointUniqueKey(ctx, rCtx, originalDatabase, originalTable, updateColumns)
122 if err != nil {
123 return "", err
124 }
125 if !has {
126 return "", errors.Errorf("no disjoint unique key found for %s.%s", originalDatabase, originalTable)
127 }
128
129 var buf strings.Builder
130 quotedColumnList := quoteMySQLColumns(normalColumns)
131 if _, err := fmt.Fprintf(&buf, "INSERT INTO `%s`.`%s` (%s) SELECT %s FROM `%s`.`%s` ON DUPLICATE KEY UPDATE ", originalDatabase, originalTable, quotedColumnList, quotedColumnList, backupDatabase, backupTable); err != nil {
132 return "", err
133 }
134
135 for i, field := range updateColumns {
136 if i > 0 {
137 if _, err := buf.WriteString(", "); err != nil {
138 return "", err
139 }
140 }
141 if _, err := fmt.Fprintf(&buf, "`%s` = VALUES(`%s`)", field, field); err != nil {
142 return "", err
143 }
144 }
145 if _, err := buf.WriteString(";"); err != nil {
146 return "", err
147 }
148 return buf.String(), nil
149}
150
151func quoteMySQLColumns(columns []string) string {
152 var quotedColumns []string

Callers 1

doGenerateFunction · 0.70

Calls 6

quoteMySQLColumnsFunction · 0.85
ErrorfMethod · 0.80
extractUpdateColumnsFunction · 0.70
hasDisjointUniqueKeyFunction · 0.70
StringMethod · 0.65

Tested by

no test coverage detected