-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: Fix #254 #334
*: Fix #254 #334
Changes from 11 commits
f4ec26f
027f1fb
0e9e876
055b375
f7e41e1
1085170
c214b7d
23e465f
cf05dfe
0188010
56d1a21
a62a648
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -426,7 +426,7 @@ func (d *ddl) AlterTable(ctx context.Context, ident table.Ident, specs []*AlterS | |
for _, spec := range specs { | ||
switch spec.Action { | ||
case AlterAddColumn: | ||
if err := d.addColumn(ident.Schema, tbl, spec, is.SchemaMetaVersion()); err != nil { | ||
if err := d.addColumn(ctx, ident.Schema, tbl, spec, is.SchemaMetaVersion()); err != nil { | ||
return errors.Trace(err) | ||
} | ||
default: | ||
|
@@ -438,7 +438,7 @@ func (d *ddl) AlterTable(ctx context.Context, ident table.Ident, specs []*AlterS | |
} | ||
|
||
// Add a column into table | ||
func (d *ddl) addColumn(schema model.CIStr, tbl table.Table, spec *AlterSpecification, schemaMetaVersion int64) error { | ||
func (d *ddl) addColumn(ctx context.Context, schema model.CIStr, tbl table.Table, spec *AlterSpecification, schemaMetaVersion int64) error { | ||
// Find position | ||
cols := tbl.Cols() | ||
position := len(cols) | ||
|
@@ -489,9 +489,11 @@ func (d *ddl) addColumn(schema model.CIStr, tbl table.Table, spec *AlterSpecific | |
tb := tbl.(*tables.Table) | ||
tb.Columns = newCols | ||
// TODO: update index | ||
// TODO: update default value | ||
// update infomation schema | ||
if err = updateDefaultValue(ctx, tb, col); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
// update infomation schema | ||
err = kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error { | ||
err := d.verifySchemaMetaVersion(txn, schemaMetaVersion) | ||
if err != nil { | ||
|
@@ -506,6 +508,47 @@ func (d *ddl) addColumn(schema model.CIStr, tbl table.Table, spec *AlterSpecific | |
return errors.Trace(err) | ||
} | ||
|
||
func updateDefaultValue(ctx context.Context, t *tables.Table, col *column.Col) error { | ||
txn, err := ctx.GetTxn(false) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
it, err := txn.Seek([]byte(t.FirstKey()), nil) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
defer it.Close() | ||
|
||
prefix := t.KeyPrefix() | ||
for it.Valid() && strings.HasPrefix(it.Key(), prefix) { | ||
handle, err0 := util.DecodeHandleFromRowKey(it.Key()) | ||
if err0 != nil { | ||
return errors.Trace(err0) | ||
} | ||
k := t.RecordKey(handle, col) | ||
colID, err0 := tables.ColumnID(k) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems that in addColumn there will be no new column, so here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep. |
||
if err0 != nil { | ||
return errors.Trace(err0) | ||
} | ||
if colID != col.ID { | ||
continue | ||
} | ||
|
||
// TODO: check and get timestamp/datetime default value. | ||
// refer to getDefaultValue in stmt/stmts/stmt_helper.go. | ||
if err0 = t.SetColValue(txn, k, col.DefaultValue); err0 != nil { | ||
return errors.Trace(err0) | ||
} | ||
|
||
rk := t.RecordKey(handle, nil) | ||
if it, err0 = kv.NextUntil(it, util.RowKeyPrefixFilter(rk)); err0 != nil { | ||
return errors.Trace(err0) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// drop table will proceed even if some table in the list does not exists | ||
func (d *ddl) DropTable(ctx context.Context, ti table.Ident) (err error) { | ||
is := d.GetInformationSchema() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any comment for this function? Or pick up a better name. "updateDefaultValue" is a little confused. "updateOldRows" may be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.