Skip to content

Commit 0fa9e43

Browse files
committed
error for enum truncation
1 parent be3f979 commit 0fa9e43

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

enginetest/queries/script_queries.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ var ScriptTests = []ScriptTest{
130130
{
131131
Query: "alter table xy modify y enum('a','b','c')",
132132
},
133-
//{
134-
// Query: "alter table xy modify y enum('a')",
135-
//},
133+
{
134+
Query: "alter table xy modify y enum('a')",
135+
ExpectedErr: sql.ErrEnumTypeTruncated,
136+
},
136137
},
137138
},
138139
{

sql/analyzer/validate_create_table.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ func validateModifyColumn(ctx *sql.Context, initialSch sql.Schema, schema sql.Sc
470470
return nil, err
471471
}
472472

473+
if e1, ok := newCol.Type.(sql.EnumType); ok {
474+
oldCol := initialSch[initialSch.IndexOfColName(oldColName)]
475+
if e2, ok := oldCol.Type.(sql.EnumType); ok {
476+
if len(e1.Values()) < len(e2.Values()) {
477+
return nil, sql.ErrEnumTypeTruncated.New()
478+
}
479+
}
480+
}
481+
473482
// TODO: When a column is being modified, we should ideally check that any existing table check constraints
474483
// are still valid (e.g. if the column type changed) and throw an error if they are invalidated.
475484
// That would be consistent with MySQL behavior.

sql/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@ var (
924924
ErrInvalidTypeForLimit = errors.NewKind("invalid limit. expected %T, found %T")
925925

926926
ErrColumnSpecifiedTwice = errors.NewKind("column '%v' specified twice")
927+
928+
ErrEnumTypeTruncated = errors.NewKind("new enum type change truncates value")
927929
)
928930

929931
// CastSQLError returns a *mysql.SQLError with the error code and in some cases, also a SQL state, populated for the

0 commit comments

Comments
 (0)