Skip to content

Commit

Permalink
fix restore table or database with keyword(#2.0-dev) (#20151)
Browse files Browse the repository at this point in the history
fix restore table or database with keyword

Approved by: @daviszhen, @heni02, @sukki37
  • Loading branch information
YANGGMM authored Nov 19, 2024
1 parent e46d870 commit 3fced2d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/frontend/pitr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ func reCreateTableWithPitr(
}

getLogger(sid).Info(fmt.Sprintf("[%s] start to drop table: '%v',", pitrName, tblInfo.tblName))
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists %s", tblInfo.tblName)); err != nil {
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists `%s`", tblInfo.tblName)); err != nil {
return
}

Expand Down Expand Up @@ -1618,7 +1618,7 @@ func deleteCurFkTableInPitrRestore(ctx context.Context,
}

getLogger(sid).Info(fmt.Sprintf("start to drop table: %v", tblInfo.tblName))
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists %s.%s", tblInfo.dbName, tblInfo.tblName)); err != nil {
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists `%s`.`%s`", tblInfo.dbName, tblInfo.tblName)); err != nil {
return
}
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/frontend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func doCreateSnapshot(ctx context.Context, ses *Session, stmt *tree.CreateSnapSh
return err
}
}
getLogger(ses.GetService()).Info(fmt.Sprintf("create snapshot %s success", snapshotName))

// insert record to the system table

Expand Down Expand Up @@ -326,6 +327,8 @@ func doDropSnapshot(ctx context.Context, ses *Session, stmt *tree.DropSnapShot)
return err
}
}

getLogger(ses.GetService()).Info(fmt.Sprintf("drop snapshot %s success", string(stmt.Name)))
return err
}

Expand Down Expand Up @@ -569,7 +572,7 @@ func deleteCurFkTables(ctx context.Context, sid string, bh BackgroundExec, dbNam
}

getLogger(sid).Info(fmt.Sprintf("start to drop table: %v", tblInfo.tblName))
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists %s.%s", tblInfo.dbName, tblInfo.tblName)); err != nil {
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists `%s`.`%s`", tblInfo.dbName, tblInfo.tblName)); err != nil {
return
}
}
Expand Down Expand Up @@ -940,7 +943,7 @@ func dropClusterTable(
for _, tblInfo := range tableInfos {
if toAccountId == 0 && tblInfo.typ == clusterTable {
getLogger(sid).Info(fmt.Sprintf("[%s] start to drop system table: %v.%v", snapshotName, moCatalog, tblInfo.tblName))
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists %s.%s", moCatalog, tblInfo.tblName)); err != nil {
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists `%s`.`%s`", moCatalog, tblInfo.tblName)); err != nil {
return
}
}
Expand Down Expand Up @@ -1071,7 +1074,7 @@ func recreateTable(
}

getLogger(sid).Info(fmt.Sprintf("[%s] start to drop table: %v,", snapshotName, tblInfo.tblName))
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists %s", tblInfo.tblName)); err != nil {
if err = bh.Exec(ctx, fmt.Sprintf("drop table if exists `%s`", tblInfo.tblName)); err != nil {
return
}

Expand Down Expand Up @@ -1792,7 +1795,7 @@ func dropDb(ctx context.Context, bh BackgroundExec, dbName string) (err error) {
}

// drop db
return bh.Exec(ctx, fmt.Sprintf("drop database if exists %s", dbName))
return bh.Exec(ctx, fmt.Sprintf("drop database if exists `%s`", dbName))
}

// checkTableIsMaster check if the table is master table
Expand Down
20 changes: 20 additions & 0 deletions test/distributed/cases/snapshot/snapshot_restore_keyword.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
drop account if exists `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;
create account `01929da4-89af-7ed8-8f32-8df9ed5ddd71` admin_name = 'test_account' identified by '111';
drop account if exists `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;
create account `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14` admin_name = 'test_account' identified by '111';
create database if not exists `mocloud_meta`;
use `mocloud_meta`;
create table if not exists `lock` (id int, name varchar(100));
insert into `lock` values(1, 'test');
select * from `mocloud_meta`.`lock`;
id name
1 test
create snapshot metadb202411181350 for account `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;
restore account `01929da4-89af-7ed8-8f32-8df9ed5ddd71` from snapshot metadb202411181350 to account `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;
use `mocloud_meta`;
select * from `mocloud_meta`.`lock`;
id name
1 test
drop snapshot if exists metadb202411181350;
drop account if exists `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;
drop account if exists `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;
31 changes: 31 additions & 0 deletions test/distributed/cases/snapshot/snapshot_restore_keyword.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
drop account if exists `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;
create account `01929da4-89af-7ed8-8f32-8df9ed5ddd71` admin_name = 'test_account' identified by '111';

drop account if exists `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;
create account `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14` admin_name = 'test_account' identified by '111';

-- @session:id=1&user=01929da4-89af-7ed8-8f32-8df9ed5ddd71:test_account&password = 111
create database if not exists `mocloud_meta`;
use `mocloud_meta`;
create table if not exists `lock` (id int, name varchar(100));
insert into `lock` values(1, 'test');
select * from `mocloud_meta`.`lock`;
-- @session



create snapshot metadb202411181350 for account `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;

-- @session:id=1&user=01929da4-89af-7ed8-8f32-8df9ed5ddd71:test_account&password = 111drop database if exists `mocloud_meta`;
-- @session

restore account `01929da4-89af-7ed8-8f32-8df9ed5ddd71` from snapshot metadb202411181350 to account `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;

-- @session:id=2&user=0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14:test_account&password = 111
use `mocloud_meta`;
select * from `mocloud_meta`.`lock`;
-- @session

drop snapshot if exists metadb202411181350;
drop account if exists `0192fbbb-bfb0-7d54-a2fe-b7fd26dbdb14`;
drop account if exists `01929da4-89af-7ed8-8f32-8df9ed5ddd71`;

0 comments on commit 3fced2d

Please sign in to comment.