From 47ea049181e17a95526463e842ccd10f350c68eb Mon Sep 17 00:00:00 2001 From: Ronaldo Campos Date: Sun, 12 Oct 2025 15:21:39 +0100 Subject: [PATCH 1/4] Fixing replica query when cluster is default --- clickhouse_backend/patch/migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clickhouse_backend/patch/migrations.py b/clickhouse_backend/patch/migrations.py index e4e3f15..d3c5966 100644 --- a/clickhouse_backend/patch/migrations.py +++ b/clickhouse_backend/patch/migrations.py @@ -45,7 +45,7 @@ def _check_replicas(connection): with connection.cursor() as cursor: cursor.execute( - f"select replica_num from system.clusters where cluster={connection.migration_cluster}" + f"select replica_num from system.clusters where cluster='{connection.migration_cluster}'" ) (replica_count,) = cursor.fetchone() return replica_count >= 1 From 4d6c178935d351daa87db6fd7c0f43db386cbbe0 Mon Sep 17 00:00:00 2001 From: Ronaldo Campos Date: Sun, 12 Oct 2025 16:45:44 +0100 Subject: [PATCH 2/4] change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b5d361..5faf25c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### 1.5.0 +- feat: #139: Fix to replicas query when using default as cluster name - feat: #133: Fix simultaneous queries error when iteration is interrupted - feat: #130: Add `distributed_migrations` database setting to support distributed migration queries. - feat: #129: Add `toYearWeek` datetime functionality From 11fad78fc81c68eefaa49568ed547a83d2d9fa73 Mon Sep 17 00:00:00 2001 From: Ronaldo Campos Date: Mon, 20 Oct 2025 18:29:52 +0100 Subject: [PATCH 3/4] fixes --- CHANGELOG.md | 2 +- clickhouse_backend/patch/migrations.py | 15 +++++++++++---- tests/migrations/test_loader.py | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5faf25c..1cd25c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ### 1.5.0 -- feat: #139: Fix to replicas query when using default as cluster name +- fix: #139: Fix to replicas query when using default as cluster name - feat: #133: Fix simultaneous queries error when iteration is interrupted - feat: #130: Add `distributed_migrations` database setting to support distributed migration queries. - feat: #129: Add `toYearWeek` datetime functionality diff --git a/clickhouse_backend/patch/migrations.py b/clickhouse_backend/patch/migrations.py index d3c5966..3ef3708 100644 --- a/clickhouse_backend/patch/migrations.py +++ b/clickhouse_backend/patch/migrations.py @@ -44,13 +44,20 @@ def _check_replicas(connection): return connection.has_replicas with connection.cursor() as cursor: - cursor.execute( - f"select replica_num from system.clusters where cluster='{connection.migration_cluster}'" - ) - (replica_count,) = cursor.fetchone() + replica_count = _get_replicas(connection.migration_cluster, cursor) return replica_count >= 1 +def _get_replicas(cluster_name, cursor): + cursor.execute( + "select replica_num from system.clusters where cluster=%s", [cluster_name] + ) + res = cursor.fetchone() + if not res: + return 0 + return res[0] + + def patch_migrations(): patch_migration_recorder() patch_migration() diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py index 1110a0e..9c22cf0 100644 --- a/tests/migrations/test_loader.py +++ b/tests/migrations/test_loader.py @@ -2,6 +2,7 @@ import os from copy import deepcopy from importlib import import_module +from unittest.mock import patch from django.db import connection, connections from django.db.migrations.exceptions import ( @@ -15,6 +16,7 @@ from clickhouse_backend import compat from clickhouse_backend.backend.base import DatabaseWrapper +from clickhouse_backend.patch.migrations import _check_replicas, _get_replicas from .test_base import MigrationTestBase @@ -201,6 +203,28 @@ def test_apply_unapply_distributed(self): conn = connections[db] self.assertMigrationExists(conn, "0432_ponies", "myapp", deleted=True) + def test_checking_cluster_replicas(self): + """ + Tests checking cluster replicas for migrations + """ + db = DatabaseWrapper(deepcopy(self.lb), alias="load_balancer") + connections["load_balancer"] = db + + for db in self.databases: + wrapper = connections[db] + conn = wrapper.client.connection + has_clusters = _check_replicas(conn) + self.assertEqual(has_clusters, True) + + with connections['default'].cursor() as cursor: + # testing "default" cluster name on query + replicas = _get_replicas('default', cursor) + self.assertFalse(replicas) + + # testing migration_cluster query + replicas = _get_replicas(self.lb['OPTIONS']['migration_cluster'], cursor) + self.assertEqual(replicas, 1) + class LoaderTests(TestCase): """ From 07339a1f78af288d57c37080d445fd4b7cf47f3d Mon Sep 17 00:00:00 2001 From: Ronaldo Campos Date: Mon, 20 Oct 2025 18:33:38 +0100 Subject: [PATCH 4/4] lint --- tests/migrations/test_loader.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py index 9c22cf0..06d38e6 100644 --- a/tests/migrations/test_loader.py +++ b/tests/migrations/test_loader.py @@ -2,7 +2,6 @@ import os from copy import deepcopy from importlib import import_module -from unittest.mock import patch from django.db import connection, connections from django.db.migrations.exceptions import ( @@ -216,13 +215,13 @@ def test_checking_cluster_replicas(self): has_clusters = _check_replicas(conn) self.assertEqual(has_clusters, True) - with connections['default'].cursor() as cursor: + with connections["default"].cursor() as cursor: # testing "default" cluster name on query - replicas = _get_replicas('default', cursor) + replicas = _get_replicas("default", cursor) self.assertFalse(replicas) # testing migration_cluster query - replicas = _get_replicas(self.lb['OPTIONS']['migration_cluster'], cursor) + replicas = _get_replicas(self.lb["OPTIONS"]["migration_cluster"], cursor) self.assertEqual(replicas, 1)