Repository: ClickHouse/ClickHouse
File: tests/queries/0_stateless/03822_named_collection_drop_dependency_check.sh (Shell)

Code before:
CREATE TABLE test_nc_dep_table2 (x UInt32) ENGINE = URL(${NC_NAME2});
"

# Both named collections should be protected
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME2}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Exchange the tables - dependencies should still work (UUID-based tracking)
$CLICKHOUSE_CLIENT -q "EXCHANGE TABLES test_nc_dep_table_renamed AND test_nc_dep_table2;"

# After exchange, both named collections should still be protected
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME2}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Clean up
$CLICKHOUSE_CLIENT -m -q "
DROP TABLE test_nc_dep_table_renamed;
DROP TABLE test_nc_dep_table2;
DROP NAMED COLLECTION ${NC_NAME};
DROP NAMED COLLECTION ${NC_NAME2};
"

# Test with mixed databases: one table in Atomic, one in Ordinary, both using the same named collection
# This tests both UUID-based and name-based tracking simultaneously
# Use --send_logs_level=fatal to suppress the deprecation warning for Ordinary database
$CLICKHOUSE_CLIENT --send_logs_level=fatal -m -q "
SET allow_deprecated_database_ordinary = 1;
DROP DATABASE IF EXISTS ${ORDINARY_DB};
CREATE DATABASE ${ORDINARY_DB} ENGINE = Ordinary;
"

$CLICKHOUSE_CLIENT -m -q "
CREATE NAMED COLLECTION ${NC_NAME} AS url = 'http://localhost:8123', format = 'CSV';
CREATE TABLE test_nc_dep_atomic (x UInt32) ENGINE = URL(${NC_NAME});
CREATE TABLE ${ORDINARY_DB}.test_nc_dep_ordinary (x UInt32) ENGINE = URL(${NC_NAME});
"

# Should fail because both tables use the named collection
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Test rename in Ordinary database (name-based tracking should update)
$CLICKHOUSE_CLIENT -q "RENAME TABLE ${ORDINARY_DB}.test_nc_dep_ordinary TO ${ORDINARY_DB}.test_nc_dep_ordinary_renamed;"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Drop the Atomic table, should still fail because Ordinary table uses the collection
$CLICKHOUSE_CLIENT -q "DROP TABLE test_nc_dep_atomic;"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Drop the Ordinary table, now drop should succeed
$CLICKHOUSE_CLIENT -m -q "
DROP TABLE ${ORDINARY_DB}.test_nc_dep_ordinary_renamed;

Review comment (suggestion):
The comment on line 78 explains why `--send_logs_level=fatal` is used, but this suppresses all logs including potential errors. Consider using a more targeted approach like `--send_logs_level=error` to suppress only warnings while keeping error visibility.
```suggestion
# Use --send_logs_level=error to suppress the deprecation warning for Ordinary database while keeping errors visible
$CLICKHOUSE_CLIENT --send_logs_level=error -m -q "
```

Code after:
CREATE TABLE test_nc_dep_table2 (x UInt32) ENGINE = URL(${NC_NAME2});
"

# Both named collections should be protected
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME2}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Exchange the tables - dependencies should still work (UUID-based tracking)
$CLICKHOUSE_CLIENT -q "EXCHANGE TABLES test_nc_dep_table_renamed AND test_nc_dep_table2;"

# After exchange, both named collections should still be protected
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME2}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Clean up
$CLICKHOUSE_CLIENT -m -q "
DROP TABLE test_nc_dep_table_renamed;
DROP TABLE test_nc_dep_table2;
DROP NAMED COLLECTION ${NC_NAME};
DROP NAMED COLLECTION ${NC_NAME2};
"

# Test with mixed databases: one table in Atomic, one in Ordinary, both using the same named collection
# This tests both UUID-based and name-based tracking simultaneously
# Use --send_logs_level=error to suppress the deprecation warning for Ordinary database
$CLICKHOUSE_CLIENT --send_logs_level=error -m -q "
SET allow_deprecated_database_ordinary = 1;
DROP DATABASE IF EXISTS ${ORDINARY_DB};
CREATE DATABASE ${ORDINARY_DB} ENGINE = Ordinary;
"

$CLICKHOUSE_CLIENT -m -q "
CREATE NAMED COLLECTION ${NC_NAME} AS url = 'http://localhost:8123', format = 'CSV';
CREATE TABLE test_nc_dep_atomic (x UInt32) ENGINE = URL(${NC_NAME});
CREATE TABLE ${ORDINARY_DB}.test_nc_dep_ordinary (x UInt32) ENGINE = URL(${NC_NAME});
"

# Should fail because both tables use the named collection
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Test rename in Ordinary database (name-based tracking should update)
$CLICKHOUSE_CLIENT -q "RENAME TABLE ${ORDINARY_DB}.test_nc_dep_ordinary TO ${ORDINARY_DB}.test_nc_dep_ordinary_renamed;"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Drop the Atomic table, should still fail because Ordinary table uses the collection
$CLICKHOUSE_CLIENT -q "DROP TABLE test_nc_dep_atomic;"
$CLICKHOUSE_CLIENT -m -q "DROP NAMED COLLECTION ${NC_NAME}; -- { serverError NAMED_COLLECTION_IS_USED }"

# Drop the Ordinary table, now drop should succeed
$CLICKHOUSE_CLIENT -m -q "
DROP TABLE ${ORDINARY_DB}.test_nc_dep_ordinary_renamed;