Skip to content

Commit

Permalink
add one column for mo_account to check connect version (#8987)
Browse files Browse the repository at this point in the history
add one column for mo_account to check connect version

Approved by: @daviszhen, @heni02
  • Loading branch information
YANGGMM authored Apr 14, 2023
1 parent 91fc625 commit d2fd4f1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
46 changes: 37 additions & 9 deletions pkg/frontend/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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";`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions pkg/frontend/authenticate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -6045,15 +6045,17 @@ func Test_doAlterAccount(t *testing.T) {

sql, _ := getSqlForCheckTenant(context.TODO(), stmt.Name)
mrs := newMrsForCheckTenant([][]interface{}{
{0},
{0, "0", "open", 0},
})
bh.sql2result[sql] = mrs

sql, _ = getSqlForPasswordOfUser(context.TODO(), stmt.AuthOption.AdminName)
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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/distributed/cases/tenant/alter_account.result
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ 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_catalog.mo_account where account_name = 'acc1';
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
acc1 0
alter account acc1 open;
select account_name, version from mo_catalog.mo_account where account_name = 'acc1';
account_name version
acc1 1
drop account if exists acc1;
12 changes: 11 additions & 1 deletion test/distributed/cases/tenant/alter_account.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ alter account accx suspend;
alter account accx open;

drop account if exists acc1;
drop account if exists accx;
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_catalog.mo_account where account_name = 'acc1';
alter account acc1 suspend;
select account_name, version from mo_catalog.mo_account where account_name = 'acc1';
alter account acc1 open;
select account_name, version from mo_catalog.mo_account where account_name = 'acc1';
drop account if exists acc1;

0 comments on commit d2fd4f1

Please sign in to comment.