Skip to content

Commit eaa3489

Browse files
author
Shlomi Noach
authored
Merge branch 'master' into server-report-coordinates
2 parents 79cee99 + d757539 commit eaa3489

9 files changed

Lines changed: 110 additions & 5 deletions

File tree

go/base/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ type MigrationContext struct {
181181
UniqueKey *sql.UniqueKey
182182
SharedColumns *sql.ColumnList
183183
ColumnRenameMap map[string]string
184+
DroppedColumnsMap map[string]bool
184185
MappedSharedColumns *sql.ColumnList
185186
MigrationRangeMinValues *sql.ColumnValues
186187
MigrationRangeMaxValues *sql.ColumnValues

go/logic/inspect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,14 @@ func (this *Inspector) getSharedColumns(originalColumns, ghostColumns *sql.Colum
662662
}
663663
sharedColumnNames := []string{}
664664
for _, originalColumn := range originalColumns.Names() {
665+
isSharedColumn := false
665666
if columnsInGhost[originalColumn] || columnsInGhost[columnRenameMap[originalColumn]] {
667+
isSharedColumn = true
668+
}
669+
if this.migrationContext.DroppedColumnsMap[originalColumn] {
670+
isSharedColumn = false
671+
}
672+
if isSharedColumn {
666673
sharedColumnNames = append(sharedColumnNames, originalColumn)
667674
}
668675
}

go/logic/migrator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func (this *Migrator) validateStatement() (err error) {
248248
}
249249
log.Infof("Alter statement has column(s) renamed. gh-ost finds the following renames: %v; --approve-renamed-columns is given and so migration proceeds.", this.parser.GetNonTrivialRenames())
250250
}
251+
this.migrationContext.DroppedColumnsMap = this.parser.DroppedColumnsMap()
251252
return nil
252253
}
253254

go/sql/parser.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ import (
1414
var (
1515
sanitizeQuotesRegexp = regexp.MustCompile("('[^']*')")
1616
renameColumnRegexp = regexp.MustCompile(`(?i)\bchange\s+(column\s+|)([\S]+)\s+([\S]+)\s+`)
17+
dropColumnRegexp = regexp.MustCompile(`(?i)\bdrop\s+(column\s+|)([\S]+)$`)
1718
)
1819

1920
type Parser struct {
2021
columnRenameMap map[string]string
22+
droppedColumns map[string]bool
2123
}
2224

2325
func NewParser() *Parser {
2426
return &Parser{
2527
columnRenameMap: make(map[string]string),
28+
droppedColumns: make(map[string]bool),
2629
}
2730
}
2831

@@ -59,10 +62,9 @@ func (this *Parser) sanitizeQuotesFromAlterStatement(alterStatement string) (str
5962
return strippedStatement
6063
}
6164

62-
func (this *Parser) ParseAlterStatement(alterStatement string) (err error) {
63-
alterTokens, _ := this.tokenizeAlterStatement(alterStatement)
64-
for _, alterToken := range alterTokens {
65-
alterToken = this.sanitizeQuotesFromAlterStatement(alterToken)
65+
func (this *Parser) parseAlterToken(alterToken string) (err error) {
66+
{
67+
// rename
6668
allStringSubmatch := renameColumnRegexp.FindAllStringSubmatch(alterToken, -1)
6769
for _, submatch := range allStringSubmatch {
6870
if unquoted, err := strconv.Unquote(submatch[2]); err == nil {
@@ -71,10 +73,28 @@ func (this *Parser) ParseAlterStatement(alterStatement string) (err error) {
7173
if unquoted, err := strconv.Unquote(submatch[3]); err == nil {
7274
submatch[3] = unquoted
7375
}
74-
7576
this.columnRenameMap[submatch[2]] = submatch[3]
7677
}
7778
}
79+
{
80+
// drop
81+
allStringSubmatch := dropColumnRegexp.FindAllStringSubmatch(alterToken, -1)
82+
for _, submatch := range allStringSubmatch {
83+
if unquoted, err := strconv.Unquote(submatch[2]); err == nil {
84+
submatch[2] = unquoted
85+
}
86+
this.droppedColumns[submatch[2]] = true
87+
}
88+
}
89+
return nil
90+
}
91+
92+
func (this *Parser) ParseAlterStatement(alterStatement string) (err error) {
93+
alterTokens, _ := this.tokenizeAlterStatement(alterStatement)
94+
for _, alterToken := range alterTokens {
95+
alterToken = this.sanitizeQuotesFromAlterStatement(alterToken)
96+
this.parseAlterToken(alterToken)
97+
}
7898
return nil
7999
}
80100

@@ -91,3 +111,7 @@ func (this *Parser) GetNonTrivialRenames() map[string]string {
91111
func (this *Parser) HasNonTrivialRenames() bool {
92112
return len(this.GetNonTrivialRenames()) > 0
93113
}
114+
115+
func (this *Parser) DroppedColumnsMap() map[string]bool {
116+
return this.droppedColumns
117+
}

go/sql/parser_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,42 @@ func TestSanitizeQuotesFromAlterStatement(t *testing.T) {
120120
test.S(t).ExpectEquals(strippedStatement, "change column i int ''")
121121
}
122122
}
123+
124+
func TestParseAlterStatementDroppedColumns(t *testing.T) {
125+
126+
{
127+
parser := NewParser()
128+
statement := "drop column b"
129+
err := parser.ParseAlterStatement(statement)
130+
test.S(t).ExpectNil(err)
131+
test.S(t).ExpectEquals(len(parser.droppedColumns), 1)
132+
test.S(t).ExpectTrue(parser.droppedColumns["b"])
133+
}
134+
{
135+
parser := NewParser()
136+
statement := "drop column b, drop key c_idx, drop column `d`"
137+
err := parser.ParseAlterStatement(statement)
138+
test.S(t).ExpectNil(err)
139+
test.S(t).ExpectEquals(len(parser.droppedColumns), 2)
140+
test.S(t).ExpectTrue(parser.droppedColumns["b"])
141+
test.S(t).ExpectTrue(parser.droppedColumns["d"])
142+
}
143+
{
144+
parser := NewParser()
145+
statement := "drop column b, drop key c_idx, drop column `d`, drop `e`, drop primary key, drop foreign key fk_1"
146+
err := parser.ParseAlterStatement(statement)
147+
test.S(t).ExpectNil(err)
148+
test.S(t).ExpectEquals(len(parser.droppedColumns), 3)
149+
test.S(t).ExpectTrue(parser.droppedColumns["b"])
150+
test.S(t).ExpectTrue(parser.droppedColumns["d"])
151+
test.S(t).ExpectTrue(parser.droppedColumns["e"])
152+
}
153+
{
154+
parser := NewParser()
155+
statement := "drop column b, drop bad statement, add column i int"
156+
err := parser.ParseAlterStatement(statement)
157+
test.S(t).ExpectNil(err)
158+
test.S(t).ExpectEquals(len(parser.droppedColumns), 1)
159+
test.S(t).ExpectTrue(parser.droppedColumns["b"])
160+
}
161+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
drop table if exists gh_ost_test;
2+
create table gh_ost_test (
3+
id int auto_increment,
4+
c1 int null,
5+
c2 int not null,
6+
primary key (id)
7+
) auto_increment=1;
8+
9+
insert into gh_ost_test values (null, null, 17);
10+
insert into gh_ost_test values (null, null, 19);
11+
12+
drop event if exists gh_ost_test;
13+
delimiter ;;
14+
create event gh_ost_test
15+
on schedule every 1 second
16+
starts current_timestamp
17+
ends current_timestamp + interval 60 second
18+
on completion not preserve
19+
enable
20+
do
21+
begin
22+
insert ignore into gh_ost_test values (101, 11, 23);
23+
insert ignore into gh_ost_test values (102, 13, 23);
24+
insert into gh_ost_test values (null, 17, 23);
25+
insert into gh_ost_test values (null, null, 29);
26+
set @last_insert_id := last_insert_id();
27+
-- update gh_ost_test set c2=c2+@last_insert_id where id=@last_insert_id order by id desc limit 1;
28+
delete from gh_ost_test where id=1;
29+
delete from gh_ost_test where c1=13; -- id=2
30+
end ;;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--alter="drop column c1, add column c1 int not null default 47"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c2

0 commit comments

Comments
 (0)