From ebe19cce34ce420a48678517523061b736535566 Mon Sep 17 00:00:00 2001 From: YANGGMM <18916107305@163.com> Date: Thu, 13 Apr 2023 15:00:12 +0800 Subject: [PATCH 1/5] add one column for mo_account --- pkg/frontend/authenticate.go | 46 +++++++++++++++---- .../cases/tenant/alter_account.result | 11 +++++ .../cases/tenant/alter_account.sql | 11 ++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/pkg/frontend/authenticate.go b/pkg/frontend/authenticate.go index 15cba2b5c833..d2b34d6ef780 100644 --- a/pkg/frontend/authenticate.go +++ b/pkg/frontend/authenticate.go @@ -775,6 +775,7 @@ var ( status varchar(300), created_time timestamp, comments varchar(256), + version bigint unsigned default 0, suspended_time timestamp default NULL );`, `create table mo_role( @@ -998,12 +999,14 @@ var ( const ( //privilege verification - checkTenantFormat = `select account_id,account_name,status,suspended_time from mo_catalog.mo_account where account_name = "%s";` + checkTenantFormat = `select account_id,account_name,status,version,suspended_time from mo_catalog.mo_account where account_name = "%s";` updateCommentsOfAccountFormat = `update mo_catalog.mo_account set comments = "%s" where account_name = "%s";` updateStatusOfAccountFormat = `update mo_catalog.mo_account set status = "%s",suspended_time = "%s" where account_name = "%s";` + updateStatusAndVersionOfAccountFormat = `update mo_catalog.mo_account set status = "%s",version = %d,suspended_time = "%s" where account_name = "%s";` + deleteAccountFromMoAccountFormat = `delete from mo_catalog.mo_account where account_name = "%s";` getPasswordOfUserFormat = `select user_id,authentication_string,default_role from mo_catalog.mo_user where user_name = "%s";` @@ -1375,6 +1378,14 @@ func getSqlForUpdateStatusOfAccount(ctx context.Context, status, timestamp, acco return fmt.Sprintf(updateStatusOfAccountFormat, status, timestamp, account), nil } +func getSqlForUpdateStatusAndVersionOfAccount(ctx context.Context, status, timestamp, account string, version uint64) (string, error) { + err := inputNameIsInvalid(ctx, status, account) + if err != nil { + return "", err + } + return fmt.Sprintf(updateStatusAndVersionOfAccountFormat, status, version, timestamp, account), nil +} + func getSqlForDeleteAccountFromMoAccount(ctx context.Context, account string) (string, error) { err := inputNameIsInvalid(ctx, account) if err != nil { @@ -2315,6 +2326,7 @@ func doAlterAccount(ctx context.Context, ses *Session, aa *tree.AlterAccount) er var sql string var erArray []ExecResult var targetAccountId uint64 + var version uint64 var accountExist bool account := ses.GetTenantInfo() if !(account.IsSysTenant() && account.IsMoAdminRole()) { @@ -2399,6 +2411,10 @@ func doAlterAccount(ctx context.Context, ses *Session, aa *tree.AlterAccount) er if err != nil { goto handleFailed } + version, err = erArray[0].GetUint64(ctx, i, 3) + if err != nil { + goto handleFailed + } } accountExist = true } else { @@ -2465,14 +2481,26 @@ func doAlterAccount(ctx context.Context, ses *Session, aa *tree.AlterAccount) er //Option 3: suspend or resume the account if aa.StatusOption.Exist { - sql, err = getSqlForUpdateStatusOfAccount(ctx, aa.StatusOption.Option.String(), types.CurrentTimestamp().String2(time.UTC, 0), aa.Name) - if err != nil { - goto handleFailed - } - bh.ClearExecResultSet() - err = bh.Exec(ctx, sql) - if err != nil { - goto handleFailed + if aa.StatusOption.Option == tree.AccountStatusSuspend { + sql, err = getSqlForUpdateStatusOfAccount(ctx, aa.StatusOption.Option.String(), types.CurrentTimestamp().String2(time.UTC, 0), aa.Name) + if err != nil { + goto handleFailed + } + bh.ClearExecResultSet() + err = bh.Exec(ctx, sql) + if err != nil { + goto handleFailed + } + } else if aa.StatusOption.Option == tree.AccountStatusOpen { + sql, err = getSqlForUpdateStatusAndVersionOfAccount(ctx, aa.StatusOption.Option.String(), types.CurrentTimestamp().String2(time.UTC, 0), aa.Name, (version+1)%math.MaxInt64) + if err != nil { + goto handleFailed + } + bh.ClearExecResultSet() + err = bh.Exec(ctx, sql) + if err != nil { + goto handleFailed + } } } } diff --git a/test/distributed/cases/tenant/alter_account.result b/test/distributed/cases/tenant/alter_account.result index 4449e4f01ed4..67f426c9b65f 100644 --- a/test/distributed/cases/tenant/alter_account.result +++ b/test/distributed/cases/tenant/alter_account.result @@ -30,3 +30,14 @@ alter account accx suspend; alter account accx open; drop account if exists acc1; drop account if exists accx; +drop account if exists acc1; +create account acc1 admin_name "root1" identified by "111"; +select account_name, version from mo_account where account_name = 'acc1'; +account_name version +acc_idx 0 +alter account acc1 suspend; +alter account acc1 open; +select account_name, version from mo_account where account_name = 'acc1'; +account_name version +acc_idx 1 +drop account if exists acc1; \ No newline at end of file diff --git a/test/distributed/cases/tenant/alter_account.sql b/test/distributed/cases/tenant/alter_account.sql index 6f5911e48802..ad565235bdd0 100644 --- a/test/distributed/cases/tenant/alter_account.sql +++ b/test/distributed/cases/tenant/alter_account.sql @@ -58,4 +58,13 @@ alter account accx suspend; alter account accx open; drop account if exists acc1; -drop account if exists accx; \ No newline at end of file +drop account if exists accx; + +--alter account check version +drop account if exists acc1; +create account acc1 admin_name "root1" identified by "111"; +select account_name, version from mo_account where account_name = 'acc1'; +alter account acc1 suspend; +alter account acc1 open; +select account_name, version from mo_account where account_name = 'acc1'; +drop account if exists acc1; \ No newline at end of file From 75ebaddd2432ee656d63f656bdbf17c6fac6509f Mon Sep 17 00:00:00 2001 From: YANGGMM <18916107305@163.com> Date: Thu, 13 Apr 2023 15:01:57 +0800 Subject: [PATCH 2/5] add bvt test --- test/distributed/cases/tenant/alter_account.result | 3 +++ test/distributed/cases/tenant/alter_account.sql | 1 + 2 files changed, 4 insertions(+) diff --git a/test/distributed/cases/tenant/alter_account.result b/test/distributed/cases/tenant/alter_account.result index 67f426c9b65f..c37c445cda42 100644 --- a/test/distributed/cases/tenant/alter_account.result +++ b/test/distributed/cases/tenant/alter_account.result @@ -36,6 +36,9 @@ select account_name, version from mo_account where account_name = 'acc1'; account_name version acc_idx 0 alter account acc1 suspend; +select account_name, version from mo_account where account_name = 'acc1'; +account_name version +acc_idx 0 alter account acc1 open; select account_name, version from mo_account where account_name = 'acc1'; account_name version diff --git a/test/distributed/cases/tenant/alter_account.sql b/test/distributed/cases/tenant/alter_account.sql index ad565235bdd0..fdfe4e65eaff 100644 --- a/test/distributed/cases/tenant/alter_account.sql +++ b/test/distributed/cases/tenant/alter_account.sql @@ -65,6 +65,7 @@ drop account if exists acc1; create account acc1 admin_name "root1" identified by "111"; select account_name, version from mo_account where account_name = 'acc1'; alter account acc1 suspend; +select account_name, version from mo_account where account_name = 'acc1'; alter account acc1 open; select account_name, version from mo_account where account_name = 'acc1'; drop account if exists acc1; \ No newline at end of file From 2aeb07939b8944a542b396c9777218c6f2788891 Mon Sep 17 00:00:00 2001 From: YANGGMM <18916107305@163.com> Date: Thu, 13 Apr 2023 16:27:40 +0800 Subject: [PATCH 3/5] fix UT test --- pkg/frontend/authenticate_test.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/frontend/authenticate_test.go b/pkg/frontend/authenticate_test.go index b31ccc489dc8..7a80b5e9a45e 100644 --- a/pkg/frontend/authenticate_test.go +++ b/pkg/frontend/authenticate_test.go @@ -5864,7 +5864,7 @@ func Test_doAlterAccount(t *testing.T) { sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name) mrs := newMrsForCheckTenant([][]interface{}{ - {0}, + {0, 0, 0, 0}, }) bh.sql2result[sql] = mrs @@ -5911,7 +5911,7 @@ func Test_doAlterAccount(t *testing.T) { sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name) mrs := newMrsForCheckTenant([][]interface{}{ - {0}, + {0, 0, 0, 0}, }) bh.sql2result[sql] = mrs @@ -6045,7 +6045,7 @@ func Test_doAlterAccount(t *testing.T) { sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name) mrs := newMrsForCheckTenant([][]interface{}{ - {0}, + {0, "0", "open", 0}, }) bh.sql2result[sql] = mrs @@ -6053,7 +6053,9 @@ func Test_doAlterAccount(t *testing.T) { bh.sql2result[sql] = newMrsForPasswordOfUser([][]interface{}{}) sql, _ = getSqlForUpdatePasswordOfUser(context.TODO(), stmt.AuthOption.IdentifiedType.Str, stmt.AuthOption.AdminName) - bh.sql2result[sql] = nil + bh.sql2result[sql] = newMrsForCheckTenant([][]interface{}{ + {0, 0, 0, 0}, + }) err := doAlterAccount(ses.GetRequestContext(), ses, stmt) convey.So(err, convey.ShouldNotBeNil) @@ -6167,7 +6169,7 @@ func Test_doAlterAccount(t *testing.T) { sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name) mrs := newMrsForCheckTenant([][]interface{}{ - {0}, + {0, 0, 0, 0}, }) bh.sql2result[sql] = mrs @@ -6208,7 +6210,7 @@ func Test_doAlterAccount(t *testing.T) { sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name) mrs := newMrsForCheckTenant([][]interface{}{ - {0}, + {0, 0, 0, 0}, }) bh.sql2result[sql] = mrs @@ -6908,8 +6910,19 @@ func newMrsForCheckTenant(rows [][]interface{}) *MysqlResultSet { col2 := &MysqlColumn{} col2.SetName("account_name") col2.SetColumnType(defines.MYSQL_TYPE_VARCHAR) + + col3 := &MysqlColumn{} + col3.SetName("status") + col3.SetColumnType(defines.MYSQL_TYPE_VARCHAR) + + col4 := &MysqlColumn{} + col4.SetName("version") + col4.SetColumnType(defines.MYSQL_TYPE_LONG) + mrs.AddColumn(col1) mrs.AddColumn(col2) + mrs.AddColumn(col3) + mrs.AddColumn(col4) for _, row := range rows { mrs.AddRow(row) From fccd7af02be47ec38a1dfcad3cbfc70603c3e763 Mon Sep 17 00:00:00 2001 From: YANGGMM <18916107305@163.com> Date: Thu, 13 Apr 2023 16:30:41 +0800 Subject: [PATCH 4/5] fix ut test --- test/distributed/cases/tenant/alter_account.result | 6 +++--- test/distributed/cases/tenant/alter_account.sql | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/distributed/cases/tenant/alter_account.result b/test/distributed/cases/tenant/alter_account.result index c37c445cda42..53697b97b8a3 100644 --- a/test/distributed/cases/tenant/alter_account.result +++ b/test/distributed/cases/tenant/alter_account.result @@ -32,15 +32,15 @@ drop account if exists acc1; drop account if exists accx; drop account if exists acc1; create account acc1 admin_name "root1" identified by "111"; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; account_name version acc_idx 0 alter account acc1 suspend; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; account_name version acc_idx 0 alter account acc1 open; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; account_name version acc_idx 1 drop account if exists acc1; \ No newline at end of file diff --git a/test/distributed/cases/tenant/alter_account.sql b/test/distributed/cases/tenant/alter_account.sql index fdfe4e65eaff..2afa3326a61a 100644 --- a/test/distributed/cases/tenant/alter_account.sql +++ b/test/distributed/cases/tenant/alter_account.sql @@ -63,9 +63,9 @@ drop account if exists accx; --alter account check version drop account if exists acc1; create account acc1 admin_name "root1" identified by "111"; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; alter account acc1 suspend; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; alter account acc1 open; -select account_name, version from mo_account where account_name = 'acc1'; +select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; drop account if exists acc1; \ No newline at end of file From bcddf2684246ed5db3ffae5abba1966d972f697a Mon Sep 17 00:00:00 2001 From: YANGGMM <18916107305@163.com> Date: Fri, 14 Apr 2023 10:21:22 +0800 Subject: [PATCH 5/5] fix bvt test --- test/distributed/cases/tenant/alter_account.result | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/distributed/cases/tenant/alter_account.result b/test/distributed/cases/tenant/alter_account.result index 53697b97b8a3..6257bf0fdbe5 100644 --- a/test/distributed/cases/tenant/alter_account.result +++ b/test/distributed/cases/tenant/alter_account.result @@ -33,14 +33,14 @@ drop account if exists accx; drop account if exists acc1; create account acc1 admin_name "root1" identified by "111"; select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; -account_name version -acc_idx 0 +account_name version +acc1 0 alter account acc1 suspend; select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; -account_name version -acc_idx 0 +account_name version +acc1 0 alter account acc1 open; select account_name, version from mo_catalog.mo_account where account_name = 'acc1'; -account_name version -acc_idx 1 +account_name version +acc1 1 drop account if exists acc1; \ No newline at end of file