clickhouse-backup icon indicating copy to clipboard operation
clickhouse-backup copied to clipboard

restore fails for parametrised views with `DEFINER` / `SQL SECURITY` when `restore_schema_on_cluster` is set

Open marsewe opened this issue 1 week ago • 0 comments

Description

When restoring a parametrised view whose DDL contains DEFINER = ... SQL SECURITY ... with restore_schema_on_cluster enabled, clickhouse-backup injects ON CLUSTER '<cluster>' after the SQL SECURITY clause. ClickHouse's grammar requires ON CLUSTER to come before DEFINER / SQL SECURITY, so the CREATE VIEW fails with a syntax error. The preceding DROP TABLE IF EXISTS ... ON CLUSTER ... NO DELAY has already run, so the view is lost after the failed restore.

Versions

  • clickhouse-backup: 26.8.3
  • ClickHouse server: 26.8.44

Steps to reproduce

create database repro on cluster default;

-- Parses: ON CLUSTER before DEFINER / SQL SECURITY
CREATE VIEW repro.v_repro
ON CLUSTER default
DEFINER = default SQL SECURITY DEFINER
AS SELECT {a:UInt8} AS x;


clickhouse-backup create --schema --tables 'repro.*' repro_b

CLICKHOUSE_RESTORE_SCHEMA_ON_CLUSTER=default clickhouse-backup restore --schema --rm repro_b



INF pkg/clickhouse/clickhouse.go:1523 > CREATE VIEW repro.v_repro UUID 'a6d0321e-e765-4b70-8c2e-570c157283d4' DEFINER = default SQL SECURITY DEFINER ON CLUSTER 'default'  AS SELECT {a:UInt8} AS x
INF pkg/clickhouse/clickhouse.go:532 > clickhouse connection closed
FTL cmd/clickhouse-backup/main.go:1425 > , error=RestoreSchema: restoreSchema: can't create table `repro`.`v_repro` after 1 times, please check your schema dependencies: CreateTable: execute create query: code: 62, message: Syntax error: failed at position 110 (ON): ON CLUSTER 'default'  AS SELECT {a:UInt8} AS x. Expected one of: COMMENT, AS
stack:
github.com/Altinity/clickhouse-backup/v2/pkg/backup.(*Backuper).restorePrologue()
        github.com/Altinity/clickhouse-backup/v2/pkg/backup/restore.go:501
github.com/Altinity/clickhouse-backup/v2/pkg/backup.(*Backuper).Restore()
        github.com/Altinity/clickhouse-backup/v2/pkg/backup/restore.go:103

marsewe avatar Nov 24 '26 12:11 marsewe