@@ -3569,38 +3569,56 @@ func TestRestoreMapping(t *testing.T) {
35693569
35703570 fullCleanup (t , r , env , []string {testBackupName5 }, []string {"local" }, databaseList5 , false , true , true , "config-database-mapping.yml" )
35713571
3572- // Corner case 5: Full qualified table mapping (db.table:db2 .table_v2) - verify DROP uses target name
3573- // https://github.com/Altinity/clickhouse-backup/issues/XXX
3572+ // Corner case 5: Full qualified table mapping (db.table:db .table_v2, src_db.table:dst_db.table_v2 ) - verify DROP uses target table name
3573+ // https://github.com/Altinity/clickhouse-backup/issues/1302
35743574 log .Debug ().Msg ("Corner case 5: Full qualified table mapping with --schema restore" )
35753575 testBackupName6 := "test_fq_table_mapping"
3576- databaseList6 := []string {"source_db " , "target_db " }
3576+ databaseList6 := []string {"db-5 " , "src-db" , "dst-db " }
35773577 fullCleanup (t , r , env , []string {testBackupName6 }, []string {"local" }, databaseList6 , false , false , false , "config-database-mapping.yml" )
35783578
35793579 env .queryWithNoError (r , "CREATE DATABASE IF NOT EXISTS `source_db`" )
35803580 env .queryWithNoError (r , "CREATE DATABASE IF NOT EXISTS `target_db`" )
35813581 env .queryWithNoError (r , "CREATE TABLE `source_db`.original_table (dt DateTime, v UInt64) ENGINE=MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY dt" )
35823582 env .queryWithNoError (r , "INSERT INTO `source_db`.original_table SELECT '2022-01-01 00:00:00', number FROM numbers(5)" )
3583+ env .queryWithNoError (r , "CREATE DATABASE IF NOT EXISTS `db-5`" )
3584+ env .queryWithNoError (r , "CREATE TABLE `db-5`.table (dt DateTime, v UInt64) ENGINE=MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY dt" )
3585+ env .queryWithNoError (r , "INSERT INTO `db-5`.table SELECT '2022-01-01 00:00:00', number FROM numbers(5)" )
35833586 // Create target table to verify DROP operates on correct table
35843587 env .queryWithNoError (r , "CREATE TABLE `target_db`.renamed_table_v2 (dt DateTime, v UInt64) ENGINE=MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY dt" )
35853588 env .queryWithNoError (r , "INSERT INTO `target_db`.renamed_table_v2 SELECT '2023-01-01 00:00:00', number FROM numbers(3)" )
3589+ env .queryWithNoError (r , "CREATE TABLE `db-5`.table_v2 (dt DateTime, v UInt64) ENGINE=MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY dt" )
3590+ env .queryWithNoError (r , "INSERT INTO `db-5`.table_v2 SELECT '2023-01-01 00:00:00', number FROM numbers(3)" )
35863591
35873592 env .DockerExecNoError (r , "clickhouse-backup" , "clickhouse-backup" , "-c" , "/etc/clickhouse-backup/config-database-mapping.yml" , "create" , testBackupName6 )
35883593
3589- log .Debug ().Msg ("Restore with full qualified table mapping (source_db.original_table -> target_db.renamed_table_v2)" )
3590- out , err := env .DockerExecOut ("clickhouse-backup" , "clickhouse-backup" , "-c" , "/etc/clickhouse-backup/config-database-mapping.yml" , "restore" , "--schema" , "--rm" , "--restore-table-mapping" , "source_db.original_table:target_db.renamed_table_v2" , "--tables" , "source_db.original_table" , testBackupName6 )
3591- log .Debug ().Msg (out )
3592- r .NoError (err )
3594+ restoreFqMappingCases := []struct {
3595+ srcDb string
3596+ srcTable string
3597+ dstDb string
3598+ dstTable string
3599+ }{
3600+ {srcDb : "db-5" , srcTable : "table" , dstDb : "db-5" , dstTable : "table_v2" },
3601+ {srcDb : "source_db" , srcTable : "original_table" , dstDb : "target_db" , dstTable : "renamed_table_v2" },
3602+ }
35933603
3594- // Verify DROP used target table name, not source table name
3595- r .Contains (out , "DROP TABLE IF EXISTS `target_db`.`renamed_table_v2`" , "DROP should use target table name from mapping" )
3596- r .NotContains (out , "DROP TABLE IF EXISTS `source_db`.`original_table`" , "DROP should NOT use source table name" )
3604+ for _ , tc := range restoreFqMappingCases {
3605+ tableMapping := fmt .Sprintf ("%s.%s:%s.%s" , tc .srcDb , tc .srcTable , tc .dstDb , tc .dstTable )
3606+ log .Debug ().Msgf ("Restore with full qualified table mapping %s" , tableMapping )
3607+ out , err := env .DockerExecOut ("clickhouse-backup" , "clickhouse-backup" , "-c" , "/etc/clickhouse-backup/config-database-mapping.yml" , "restore" , "--schema" , "--rm" , "--restore-table-mapping" , tableMapping , "--tables" , tc .srcDb + "." + tc .srcTable , testBackupName6 )
3608+ log .Debug ().Msg (out )
3609+ r .NoError (err )
35973610
3598- // Verify table was created in target location
3599- env .checkCount (r , 1 , 1 , "SELECT count() FROM system.tables WHERE database='target_db' AND name='renamed_table_v2'" )
3611+ // Verify DROP used target table name, not source table name
3612+ r .Contains (out , fmt .Sprintf ("DROP TABLE IF EXISTS `%s`.`%s`" , tc .dstDb , tc .dstTable ), "DROP should use target table name from mapping" )
3613+ r .NotContains (out , fmt .Sprintf ("DROP TABLE IF EXISTS `%s`.`%s`" , tc .srcDb , tc .srcTable ), "DROP should NOT use source table name" )
36003614
3601- // Restore data and verify
3602- env .DockerExecNoError (r , "clickhouse-backup" , "clickhouse-backup" , "-c" , "/etc/clickhouse-backup/config-database-mapping.yml" , "restore" , "--data" , "--restore-table-mapping" , "source_db.original_table:target_db.renamed_table_v2" , "--tables" , "source_db.original_table" , testBackupName6 )
3603- env .checkCount (r , 1 , 5 , "SELECT count() FROM `target_db`.renamed_table_v2" )
3615+ // Verify table was created in target location
3616+ env .checkCount (r , 1 , 1 , fmt .Sprintf ("SELECT count() FROM system.tables WHERE database='%s' AND name='%s'" , tc .dstDb , tc .dstTable ))
3617+
3618+ // Restore data and verify
3619+ env .DockerExecNoError (r , "clickhouse-backup" , "clickhouse-backup" , "-c" , "/etc/clickhouse-backup/config-database-mapping.yml" , "restore" , "--data" , "--restore-table-mapping" , tableMapping , "--tables" , tc .srcDb + "." + tc .srcTable , testBackupName6 )
3620+ env .checkCount (r , 1 , 5 , fmt .Sprintf ("SELECT count() FROM `%s`.`%s`" , tc .dstDb , tc .dstTable ))
3621+ }
36043622
36053623 fullCleanup (t , r , env , []string {testBackupName6 }, []string {"local" }, databaseList6 , false , true , true , "config-database-mapping.yml" )
36063624
0 commit comments