Skip to content

Commit a08b871

Browse files
authored
Fixing replica query when cluster is default (#139)
1 parent 03b9adb commit a08b871

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### 1.5.0
2+
- fix: #139: Fix to replicas query when using default as cluster name
23
- feat: #133: Fix simultaneous queries error when iteration is interrupted
34
- feat: #130: Add `distributed_migrations` database setting to support distributed migration queries.
45
- feat: #129: Add `toYearWeek` datetime functionality

clickhouse_backend/patch/migrations.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ def _check_replicas(connection):
4444
return connection.has_replicas
4545

4646
with connection.cursor() as cursor:
47-
cursor.execute(
48-
f"select replica_num from system.clusters where cluster={connection.migration_cluster}"
49-
)
50-
(replica_count,) = cursor.fetchone()
47+
replica_count = _get_replicas(connection.migration_cluster, cursor)
5148
return replica_count >= 1
5249

5350

51+
def _get_replicas(cluster_name, cursor):
52+
cursor.execute(
53+
"select replica_num from system.clusters where cluster=%s", [cluster_name]
54+
)
55+
res = cursor.fetchone()
56+
if not res:
57+
return 0
58+
return res[0]
59+
60+
5461
def patch_migrations():
5562
patch_migration_recorder()
5663
patch_migration()

tests/migrations/test_loader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from clickhouse_backend import compat
1717
from clickhouse_backend.backend.base import DatabaseWrapper
18+
from clickhouse_backend.patch.migrations import _check_replicas, _get_replicas
1819

1920
from .test_base import MigrationTestBase
2021

@@ -201,6 +202,28 @@ def test_apply_unapply_distributed(self):
201202
conn = connections[db]
202203
self.assertMigrationExists(conn, "0432_ponies", "myapp", deleted=True)
203204

205+
def test_checking_cluster_replicas(self):
206+
"""
207+
Tests checking cluster replicas for migrations
208+
"""
209+
db = DatabaseWrapper(deepcopy(self.lb), alias="load_balancer")
210+
connections["load_balancer"] = db
211+
212+
for db in self.databases:
213+
wrapper = connections[db]
214+
conn = wrapper.client.connection
215+
has_clusters = _check_replicas(conn)
216+
self.assertEqual(has_clusters, True)
217+
218+
with connections["default"].cursor() as cursor:
219+
# testing "default" cluster name on query
220+
replicas = _get_replicas("default", cursor)
221+
self.assertFalse(replicas)
222+
223+
# testing migration_cluster query
224+
replicas = _get_replicas(self.lb["OPTIONS"]["migration_cluster"], cursor)
225+
self.assertEqual(replicas, 1)
226+
204227

205228
class LoaderTests(TestCase):
206229
"""

0 commit comments

Comments
 (0)