From ed6edd877dc00383c2458797e79f9a47723b6908 Mon Sep 17 00:00:00 2001 From: Helder Sousa Date: Fri, 20 Sep 2024 00:10:49 +0100 Subject: [PATCH 1/3] Improve logic to include originalUniqueKey in sharedUniqueKeys 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. --- go/logic/inspect.go | 3 ++- go/logic/inspect_test.go | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/go/logic/inspect.go b/go/logic/inspect.go index 9d414a43e..42aee7e73 100644 --- a/go/logic/inspect.go +++ b/go/logic/inspect.go @@ -736,8 +736,9 @@ func (this *Inspector) getSharedUniqueKeys(originalUniqueKeys, ghostUniqueKeys [ // the ALTER is on the name itself... for _, originalUniqueKey := range originalUniqueKeys { for _, ghostUniqueKey := range ghostUniqueKeys { - if originalUniqueKey.Columns.EqualsByNames(&ghostUniqueKey.Columns) { + if originalUniqueKey.Columns.IsSubsetOf(&ghostUniqueKey.Columns) { uniqueKeys = append(uniqueKeys, originalUniqueKey) + break } } } diff --git a/go/logic/inspect_test.go b/go/logic/inspect_test.go index 54bc48ff0..4fa1eec11 100644 --- a/go/logic/inspect_test.go +++ b/go/logic/inspect_test.go @@ -17,6 +17,7 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) { origUniqKeys := []*sql.UniqueKey{ {Columns: *sql.NewColumnList([]string{"id", "item_id"})}, {Columns: *sql.NewColumnList([]string{"id", "org_id"})}, + {Columns: *sql.NewColumnList([]string{"id"})}, } ghostUniqKeys := []*sql.UniqueKey{ {Columns: *sql.NewColumnList([]string{"id", "item_id"})}, @@ -25,7 +26,8 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) { } inspector := &Inspector{} sharedUniqKeys := inspector.getSharedUniqueKeys(origUniqKeys, ghostUniqKeys) - test.S(t).ExpectEquals(len(sharedUniqKeys), 2) + test.S(t).ExpectEquals(len(sharedUniqKeys), 3) test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id") test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id") + test.S(t).ExpectEquals(sharedUniqKeys[2].Columns.String(), "id") } From 1471d6c0dfd54d94939e4b32ad1b30a727599f48 Mon Sep 17 00:00:00 2001 From: Helder Sousa Date: Sat, 4 Jan 2025 15:27:01 +0000 Subject: [PATCH 2/3] Update and add localtests for new logic --- localtests/fail-no-shared-uk/create.sql | 4 ++-- localtests/fail-no-shared-uk/extra_args | 2 +- localtests/shared-uk/create.sql | 22 ++++++++++++++++++++++ localtests/shared-uk/extra_args | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 localtests/shared-uk/create.sql create mode 100644 localtests/shared-uk/extra_args diff --git a/localtests/fail-no-shared-uk/create.sql b/localtests/fail-no-shared-uk/create.sql index 5bb45f2a3..8f092e790 100644 --- a/localtests/fail-no-shared-uk/create.sql +++ b/localtests/fail-no-shared-uk/create.sql @@ -1,10 +1,10 @@ drop table if exists gh_ost_test; create table gh_ost_test ( - id int auto_increment, + id int not null, i int not null, ts timestamp, primary key(id) -) auto_increment=1; +); drop event if exists gh_ost_test; delimiter ;; diff --git a/localtests/fail-no-shared-uk/extra_args b/localtests/fail-no-shared-uk/extra_args index 379a77d85..6f66ccdb9 100644 --- a/localtests/fail-no-shared-uk/extra_args +++ b/localtests/fail-no-shared-uk/extra_args @@ -1 +1 @@ ---alter="drop primary key, add primary key (id, i)" +--alter="drop primary key, add primary key (i)" diff --git a/localtests/shared-uk/create.sql b/localtests/shared-uk/create.sql new file mode 100644 index 000000000..5bb45f2a3 --- /dev/null +++ b/localtests/shared-uk/create.sql @@ -0,0 +1,22 @@ +drop table if exists gh_ost_test; +create table gh_ost_test ( + id int auto_increment, + i int not null, + ts timestamp, + primary key(id) +) auto_increment=1; + +drop event if exists gh_ost_test; +delimiter ;; +create event gh_ost_test + on schedule every 1 second + starts current_timestamp + ends current_timestamp + interval 60 second + on completion not preserve + enable + do +begin + insert into gh_ost_test values (null, 11, now()); + insert into gh_ost_test values (null, 13, now()); + insert into gh_ost_test values (null, 17, now()); +end ;; diff --git a/localtests/shared-uk/extra_args b/localtests/shared-uk/extra_args new file mode 100644 index 000000000..379a77d85 --- /dev/null +++ b/localtests/shared-uk/extra_args @@ -0,0 +1 @@ +--alter="drop primary key, add primary key (id, i)" From b6ccdd31fde2b7a9dba0434701839da5232cece4 Mon Sep 17 00:00:00 2001 From: Helder Sousa Date: Sat, 4 Jan 2025 23:51:14 +0000 Subject: [PATCH 3/3] Update test after incorrect conflict resolution when merging master to branch --- go/logic/inspect_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/logic/inspect_test.go b/go/logic/inspect_test.go index cd4981ebe..ac24f35ef 100644 --- a/go/logic/inspect_test.go +++ b/go/logic/inspect_test.go @@ -25,8 +25,8 @@ func TestInspectGetSharedUniqueKeys(t *testing.T) { } inspector := &Inspector{} sharedUniqKeys := inspector.getSharedUniqueKeys(origUniqKeys, ghostUniqKeys) - test.S(t).ExpectEquals(len(sharedUniqKeys), 3) - test.S(t).ExpectEquals(sharedUniqKeys[0].Columns.String(), "id,item_id") - test.S(t).ExpectEquals(sharedUniqKeys[1].Columns.String(), "id,org_id") - test.S(t).ExpectEquals(sharedUniqKeys[2].Columns.String(), "id") + require.Len(t, sharedUniqKeys, 3) + require.Equal(t, "id,item_id", sharedUniqKeys[0].Columns.String()) + require.Equal(t, "id,org_id", sharedUniqKeys[1].Columns.String()) + require.Equal(t, "id", sharedUniqKeys[2].Columns.String()) }