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

Function hasDisjointUniqueKey

backend/plugin/parser/mysql/restore.go:225–263  ·  view source on GitHub ↗
(ctx context.Context, rCtx base.RestoreContext, originalDatabase, originalTable string, updateColumns []string)

Source from the content-addressed store, hash-verified

223}
224
225func hasDisjointUniqueKey(ctx context.Context, rCtx base.RestoreContext, originalDatabase, originalTable string, updateColumns []string) (bool, error) {
226 columnMap := make(map[string]bool)
227 for _, column := range updateColumns {
228 columnMap[strings.ToLower(column)] = true
229 }
230
231 if rCtx.GetDatabaseMetadataFunc == nil {
232 return false, errors.Errorf("GetDatabaseMetadataFunc is nil")
233 }
234
235 _, metadata, err := rCtx.GetDatabaseMetadataFunc(ctx, rCtx.InstanceID, originalDatabase)
236 if err != nil {
237 return false, errors.Wrapf(err, "failed to get database metadata for %s", originalDatabase)
238 }
239 if metadata == nil {
240 return false, errors.Errorf("database metadata is nil for %s", originalDatabase)
241 }
242
243 schema := metadata.GetSchemaMetadata("")
244 if schema == nil {
245 return false, errors.Errorf("schema is nil for %s", originalDatabase)
246 }
247
248 tableMetadata := schema.GetTable(originalTable)
249 if tableMetadata == nil {
250 return false, errors.Errorf("table metadata is nil for %s.%s", originalDatabase, originalTable)
251 }
252
253 for _, index := range tableMetadata.GetProto().Indexes {
254 if !index.Primary && !index.Unique {
255 continue
256 }
257 if disjoint(index.Expressions, columnMap) {
258 return true, nil
259 }
260 }
261
262 return false, nil
263}
264
265func disjoint(a []string, b map[string]bool) bool {
266 for _, item := range a {

Callers 1

generateUpdateRestoreFunction · 0.70

Calls 6

ErrorfMethod · 0.80
GetSchemaMetadataMethod · 0.80
disjointFunction · 0.70
GetTableMethod · 0.45
GetProtoMethod · 0.45

Tested by

no test coverage detected