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

Function tidbCreateColumnHelper

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

Source from the content-addressed store, hash-verified

1160}
1161
1162func tidbCreateColumnHelper(t *model.TableMetadata, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *storepb.Advice {
1163 if t.GetColumn(column.Name.Name.L) != nil {
1164 content := fmt.Sprintf("Column `%s` already exists in table `%s`", column.Name.Name.O, t.GetProto().Name)
1165 return &storepb.Advice{
1166 Status: storepb.Advice_ERROR,
1167 Code: code.ColumnExists.Int32(),
1168 Title: content,
1169 Content: content,
1170 StartPosition: &storepb.Position{Line: 0},
1171 }
1172 }
1173
1174 pos := len(t.GetProto().GetColumns()) + 1
1175 if position != nil {
1176 var err *storepb.Advice
1177 pos, err = tidbReorderColumn(t, position)
1178 if err != nil {
1179 return err
1180 }
1181 }
1182
1183 col := &storepb.ColumnMetadata{
1184 Name: column.Name.Name.L,
1185 Position: int32(pos),
1186 Default: "",
1187 Nullable: true,
1188 Type: column.Tp.CompactStr(),
1189 CharacterSet: column.Tp.GetCharset(),
1190 Collation: column.Tp.GetCollate(),
1191 }
1192 setNullDefault := false
1193
1194 for _, option := range column.Options {
1195 switch option.Tp {
1196 case tidbast.ColumnOptionPrimaryKey:
1197 col.Nullable = false
1198 if err := tidbCreatePrimaryKeyHelper(t, []string{col.Name}, tidbast.IndexTypeBtree.String()); err != nil {
1199 return err
1200 }
1201 case tidbast.ColumnOptionNotNull:
1202 col.Nullable = false
1203 case tidbast.ColumnOptionAutoIncrement:
1204 // we do not deal with AUTO-INCREMENT
1205 case tidbast.ColumnOptionDefaultValue:
1206 if err := checkDefault(col.Name, column.Tp, option.Expr); err != nil {
1207 return err
1208 }
1209 if option.Expr.GetType().GetType() != mysql.TypeNull {
1210 defaultValue, err := restoreNode(option.Expr, format.RestoreStringWithoutCharset)
1211 if err != nil {
1212 content := fmt.Sprintf("Failed to deparse default value: %v", err)
1213 return &storepb.Advice{
1214 Status: storepb.Advice_ERROR,
1215 Code: code.Internal.Int32(),
1216 Title: content,
1217 Content: content,
1218 StartPosition: &storepb.Position{Line: 0},
1219 }

Callers 3

tidbAlterTableFunction · 0.85
tidbCreateTableFunction · 0.85

Calls 14

tidbReorderColumnFunction · 0.85
checkDefaultFunction · 0.85
restoreNodeFunction · 0.85
tidbCreateIndexHelperFunction · 0.85
CreateColumnMethod · 0.80
StringMethod · 0.65
GetColumnMethod · 0.45
GetProtoMethod · 0.45
Int32Method · 0.45
GetColumnsMethod · 0.45
GetCharsetMethod · 0.45

Tested by

no test coverage detected