MCPcopy Create free account
hub / github.com/dolthub/doltgresql / validatePrimaryKey

Function validatePrimaryKey

server/analyzer/validate_create_table.go:355–403  ·  view source on GitHub ↗

validatePrimaryKey validates a primary key add or drop operation.

(ctx *sql.Context, initialSch, sch sql.Schema, ai *plan.AlterPK)

Source from the content-addressed store, hash-verified

353
354// validatePrimaryKey validates a primary key add or drop operation.
355func validatePrimaryKey(ctx *sql.Context, initialSch, sch sql.Schema, ai *plan.AlterPK) (sql.Schema, error) {
356 tableName := getTableName(ai.Table)
357 switch ai.Action {
358 case plan.PrimaryKeyAction_Create:
359 if analyzer.HasPrimaryKeys(sch) {
360 return nil, sql.ErrMultiplePrimaryKeysDefined.New()
361 }
362
363 colMap := schToColMap(sch)
364 idxDef := &sql.IndexDef{
365 Name: "PRIMARY",
366 Columns: ai.Columns,
367 Constraint: sql.IndexConstraint_Primary,
368 }
369
370 err := validateIndex(ctx, colMap, idxDef)
371 if err != nil {
372 return nil, err
373 }
374
375 for _, idxCol := range ai.Columns {
376 schCol := colMap[strings.ToLower(idxCol.Name)]
377 if schCol.Virtual {
378 return nil, sql.ErrVirtualColumnPrimaryKey.New()
379 }
380 }
381
382 // Set the primary keys
383 for _, col := range ai.Columns {
384 sch[sch.IndexOf(col.Name, tableName)].PrimaryKey = true
385 }
386
387 return sch, nil
388 case plan.PrimaryKeyAction_Drop:
389 if !analyzer.HasPrimaryKeys(sch) {
390 return nil, sql.ErrCantDropFieldOrKey.New("PRIMARY")
391 }
392
393 for _, col := range sch {
394 if col.PrimaryKey {
395 col.PrimaryKey = false
396 }
397 }
398
399 return sch, nil
400 default:
401 return sch, nil
402 }
403}
404
405// validateAlterIndex validates the specified column can have an index added, dropped, or renamed. Returns an updated
406// list of index name given the add, drop, or rename operations.

Callers 1

resolveAlterColumnFunction · 0.85

Calls 3

getTableNameFunction · 0.85
schToColMapFunction · 0.85
validateIndexFunction · 0.85

Tested by

no test coverage detected