Skip to content

Commit ad5d3ea

Browse files
authored
Improve logic to include originalUniqueKey in sharedUniqueKeys (#1453)
Until now, when building the list of SharedUniqueKeys, the code was selecting only the originalUniqueKeys that existed in ghostUniqueKeys list. However, as part of the alter table, it is possible to add more unique keys or even composed primary keys with multiple columns. The new logic keeps the old behaviour (it matches originalUniqueKey with exactly the same ghostUniqueKey) but also supports the cases where the originalUniqueKey is now a subset of one of the new ghostUniqueKeys. If such a case happens, we can still use the originalUniqueKey as normal because a new ghostUniqueKey with the columns of originalUniqueKey plus some new columns is inherently unique just by the columns in the originalUniqueKey, no matter the value in the new columns of the new unique key.
1 parent d5ab048 commit ad5d3ea

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

Diff for: go/logic/inspect.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,9 @@ func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [
779779
// the ALTER is on the name itself...
780780
for _, originalUniqueKey := range originalUniqueKeys {
781781
for _, ghostUniqueKey := range ghostUniqueKeys {
782-
if originalUniqueKey.Columns.EqualsByNames(&ghostUniqueKey.Columns) {
782+
if originalUniqueKey.Columns.IsSubsetOf(&ghostUniqueKey.Columns) {
783783
uniqueKeys = append(uniqueKeys, originalUniqueKey)
784+
break
784785
}
785786
}
786787
}

Diff for: go/logic/inspect_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) {
1616
origUniqKeys := []*sql.UniqueKey{
1717
{Columns: *sql.NewColumnList([]string{"id", "item_id"})},
1818
{Columns: *sql.NewColumnList([]string{"id", "org_id"})},
19+
{Columns: *sql.NewColumnList([]string{"id"})},
1920
}
2021
ghostUniqKeys := []*sql.UniqueKey{
2122
{Columns: *sql.NewColumnList([]string{"id", "item_id"})},
@@ -24,7 +25,8 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) {
2425
}
2526
inspector := &Inspector{}
2627
sharedUniqKeys := inspector.getSharedUniqueKeys(origUniqKeys, ghostUniqKeys)
27-
require.Len(t, sharedUniqKeys, 2)
28+
require.Len(t, sharedUniqKeys, 3)
2829
require.Equal(t, "id,item_id", sharedUniqKeys[0].Columns.String())
2930
require.Equal(t, "id,org_id", sharedUniqKeys[1].Columns.String())
31+
require.Equal(t, "id", sharedUniqKeys[2].Columns.String())
3032
}

Diff for: localtests/fail-no-shared-uk/create.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
drop table if exists gh_ost_test;
22
create table gh_ost_test (
3-
id int auto_increment,
3+
id int not null,
44
i int not null,
55
ts timestamp,
66
primary key(id)
7-
) auto_increment=1;
7+
);
88

99
drop event if exists gh_ost_test;
1010
delimiter ;;

Diff for: localtests/fail-no-shared-uk/extra_args

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--alter="drop primary key, add primary key (id, i)"
1+
--alter="drop primary key, add primary key (i)"

Diff for: localtests/shared-uk/create.sql

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
drop table if exists gh_ost_test;
2+
create table gh_ost_test (
3+
id int auto_increment,
4+
i int not null,
5+
ts timestamp,
6+
primary key(id)
7+
) auto_increment=1;
8+
9+
drop event if exists gh_ost_test;
10+
delimiter ;;
11+
create event gh_ost_test
12+
on schedule every 1 second
13+
starts current_timestamp
14+
ends current_timestamp + interval 60 second
15+
on completion not preserve
16+
enable
17+
do
18+
begin
19+
insert into gh_ost_test values (null, 11, now());
20+
insert into gh_ost_test values (null, 13, now());
21+
insert into gh_ost_test values (null, 17, now());
22+
end ;;

Diff for: localtests/shared-uk/extra_args

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--alter="drop primary key, add primary key (id, i)"

0 commit comments

Comments
 (0)