Skip to content

Commit c893a0d

Browse files
authored
Add support for CHANGE COLUMN in MySQL (#1605)
1 parent 0441e17 commit c893a0d

File tree

7 files changed

+102
-1
lines changed

7 files changed

+102
-1
lines changed

internal/endtoend/testdata/ddl_alter_table_change_column/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/ddl_alter_table_change_column/mysql/go/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/ddl_alter_table_change_column/mysql/go/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* name: Placeholder :exec */
2+
SELECT 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE foo (bar text);
2+
ALTER TABLE foo CHANGE COLUMN bar baz text;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,29 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
6868
})
6969

7070
case pcast.AlterTableChangeColumn:
71-
// spew.Dump("change column", spec)
71+
oldName := spec.OldColumnName.String()
72+
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
73+
Name: &oldName,
74+
Subtype: ast.AT_DropColumn,
75+
})
76+
77+
for _, def := range spec.NewColumns {
78+
name := def.Name.String()
79+
columnDef := ast.ColumnDef{
80+
Colname: def.Name.String(),
81+
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
82+
IsNotNull: isNotNull(def),
83+
}
84+
if def.Tp.Flen >= 0 {
85+
length := def.Tp.Flen
86+
columnDef.Length = &length
87+
}
88+
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
89+
Name: &name,
90+
Subtype: ast.AT_AddColumn,
91+
Def: &columnDef,
92+
})
93+
}
7294

7395
case pcast.AlterTableModifyColumn:
7496
for _, def := range spec.NewColumns {

0 commit comments

Comments
 (0)