|
| 1 | +import pytest |
| 2 | + |
| 3 | +from helpers.cluster import ClickHouseCluster, QueryRuntimeException |
| 4 | + |
| 5 | +cluster = ClickHouseCluster(__file__) |
| 6 | + |
| 7 | +node1 = cluster.add_instance( |
| 8 | + "node1", |
| 9 | + main_configs=["configs/remote_servers.xml"], |
| 10 | + with_zookeeper=True, |
| 11 | + macros={"shard": 1, "replica": 1}, |
| 12 | +) |
| 13 | +node2 = cluster.add_instance( |
| 14 | + "node2", |
| 15 | + main_configs=["configs/remote_servers.xml"], |
| 16 | + with_zookeeper=True, |
| 17 | + macros={"shard": 1, "replica": 2}, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="module") |
| 22 | +def started_cluster(): |
| 23 | + try: |
| 24 | + cluster.start() |
| 25 | + |
| 26 | + yield cluster |
| 27 | + |
| 28 | + finally: |
| 29 | + cluster.shutdown() |
| 30 | + |
| 31 | + |
| 32 | +def create_database(node, db_name: str, engine: str, comment: str): |
| 33 | + if engine == "Atomic": |
| 34 | + node.query( |
| 35 | + f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Atomic COMMENT '{comment}'" |
| 36 | + ) |
| 37 | + return |
| 38 | + |
| 39 | + if engine == "Lazy": |
| 40 | + node.query( |
| 41 | + f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Lazy(1) COMMENT '{comment}'" |
| 42 | + ) |
| 43 | + return |
| 44 | + |
| 45 | + if engine == "Memory": |
| 46 | + node.query( |
| 47 | + f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Memory COMMENT '{comment}'" |
| 48 | + ) |
| 49 | + return |
| 50 | + |
| 51 | + if engine == "Replicated": |
| 52 | + node.query( |
| 53 | + f"CREATE DATABASE {db_name} ON CLUSTER test_cluster " |
| 54 | + + r"ENGINE = Replicated('/db/test', '{shard}', '{replica}')" |
| 55 | + + f" COMMENT '{comment}'" |
| 56 | + ) |
| 57 | + return |
| 58 | + |
| 59 | + raise QueryRuntimeException(f"Not supported engine {engine}") |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("engine", ["Atomic", "Lazy", "Memory", "Replicated"]) |
| 63 | +def test_alter_database_comment(started_cluster, engine): |
| 64 | + node1.query("DROP DATABASE IF EXISTS test") |
| 65 | + node2.query("DROP DATABASE IF EXISTS test") |
| 66 | + |
| 67 | + create_database(node1, "test", engine, "initial comment") |
| 68 | + |
| 69 | + modified_comment = "modified comment" |
| 70 | + node1.query( |
| 71 | + f"ALTER DATABASE test ON CLUSTER test_cluster (MODIFY COMMENT '{modified_comment}')" |
| 72 | + ) |
| 73 | + |
| 74 | + assert ( |
| 75 | + node1.query("SELECT comment FROM system.databases WHERE name='test'").strip() |
| 76 | + == modified_comment |
| 77 | + ) |
| 78 | + assert ( |
| 79 | + node2.query("SELECT comment FROM system.databases WHERE name='test'").strip() |
| 80 | + == modified_comment |
| 81 | + ) |
| 82 | + |
| 83 | + node1.query(f"DETACH DATABASE test ON CLUSTER test_cluster") |
| 84 | + |
| 85 | + assert ( |
| 86 | + node1.query("SELECT count() FROM system.databases WHERE name='test'").strip() |
| 87 | + == "0" |
| 88 | + ) |
| 89 | + assert ( |
| 90 | + node2.query("SELECT count() FROM system.databases WHERE name='test'").strip() |
| 91 | + == "0" |
| 92 | + ) |
| 93 | + |
| 94 | + node1.query(f"ATTACH DATABASE test ON CLUSTER test_cluster") |
| 95 | + |
| 96 | + assert ( |
| 97 | + node1.query("SELECT comment FROM system.databases WHERE name='test'").strip() |
| 98 | + == modified_comment |
| 99 | + ) |
| 100 | + assert ( |
| 101 | + node2.query("SELECT comment FROM system.databases WHERE name='test'").strip() |
| 102 | + == modified_comment |
| 103 | + ) |
| 104 | + |
| 105 | + node1.query("DROP DATABASE IF EXISTS test") |
| 106 | + node2.query("DROP DATABASE IF EXISTS test") |
0 commit comments