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

Function tidbChangeColumnDefault

backend/plugin/schema/tidb/walk_through.go:589–656  ·  view source on GitHub ↗
(t *model.TableMetadata, column *tidbast.ColumnDef)

Source from the content-addressed store, hash-verified

587}
588
589func tidbChangeColumnDefault(t *model.TableMetadata, column *tidbast.ColumnDef) *storepb.Advice {
590 columnName := column.Name.Name.L
591 col := t.GetColumn(columnName)
592 if col == nil {
593 content := fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.GetProto().Name)
594 return &storepb.Advice{
595 Status: storepb.Advice_ERROR,
596 Code: code.ColumnNotExists.Int32(),
597 Title: content,
598 Content: content,
599 StartPosition: &storepb.Position{Line: 0},
600 }
601 }
602
603 colProto := col.GetProto()
604 if len(column.Options) == 1 {
605 // SET DEFAULT
606 if column.Options[0].Expr.GetType().GetType() != mysql.TypeNull {
607 if colProto.Type != "" {
608 switch strings.ToLower(colProto.Type) {
609 case "blob", "tinyblob", "mediumblob", "longblob",
610 "text", "tinytext", "mediumtext", "longtext",
611 "json",
612 "geometry":
613 content := fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName)
614 return &storepb.Advice{
615 Status: storepb.Advice_ERROR,
616 Code: code.InvalidColumnDefault.Int32(),
617 Title: content,
618 Content: content,
619 StartPosition: &storepb.Position{Line: 0},
620 }
621 default:
622 // Other column types allow default values
623 }
624 }
625
626 defaultValue, err := restoreNode(column.Options[0].Expr, format.RestoreStringWithoutCharset)
627 if err != nil {
628 content := fmt.Sprintf("Failed to deparse default value: %v", err)
629 return &storepb.Advice{
630 Status: storepb.Advice_ERROR,
631 Code: code.Internal.Int32(),
632 Title: content,
633 Content: content,
634 StartPosition: &storepb.Position{Line: 0},
635 }
636 }
637 colProto.Default = defaultValue
638 } else {
639 if !colProto.Nullable {
640 content := fmt.Sprintf("Invalid default value for column `%s`", columnName)
641 return &storepb.Advice{
642 Status: storepb.Advice_ERROR,
643 Code: code.SetNullDefaultForNotNullColumn.Int32(),
644 Title: content,
645 Content: content,
646 StartPosition: &storepb.Position{Line: 0},

Callers 1

tidbAlterTableFunction · 0.85

Calls 5

restoreNodeFunction · 0.85
GetColumnMethod · 0.45
GetProtoMethod · 0.45
Int32Method · 0.45
GetTypeMethod · 0.45

Tested by

no test coverage detected