From 6d85406eab2964306c78d5c5611c595fd965c499 Mon Sep 17 00:00:00 2001 From: Andrey Vertiprahov Date: Sat, 3 Apr 2021 14:08:22 +0500 Subject: [PATCH] Use for Old PM data migration separate db. --- core/clickhouse/connect.py | 6 ++++-- pm/models/metricscope.py | 13 ++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/clickhouse/connect.py b/core/clickhouse/connect.py index 0bbd424c8c..2ec2bcf625 100644 --- a/core/clickhouse/connect.py +++ b/core/clickhouse/connect.py @@ -66,8 +66,10 @@ class ClickhouseClient(object): raise ClickhouseError("%s: %s" % (code, body)) return [smart_text(row).split("\t") for row in body.splitlines()] - def ensure_db(self): - self.execute(post=f"CREATE DATABASE IF NOT EXISTS {config.clickhouse.db};", nodb=True) + def ensure_db(self, db_name=None): + self.execute( + post=f"CREATE DATABASE IF NOT EXISTS {db_name or config.clickhouse.db};", nodb=True + ) def has_table(self, name): r = self.execute( diff --git a/pm/models/metricscope.py b/pm/models/metricscope.py index 0ca8c33931..2a3109ba2c 100644 --- a/pm/models/metricscope.py +++ b/pm/models/metricscope.py @@ -32,6 +32,8 @@ id_lock = Lock() to_path_code = {} code_lock = Lock() +OLD_PM_SCHEMA_TABLE = "noc_old" + class KeyField(EmbeddedDocument): # Table field name @@ -341,9 +343,9 @@ class MetricScope(Document): changed = True # Old schema if ensure_column(raw_table, "path"): - # Old schema, data table will be rename to old_ for save data. - ch.rename_table(raw_table, f"old_{self.table_name}") - pass + # Old schema, data table will be move to old_noc db for save data. + ch.ensure_db(OLD_PM_SCHEMA_TABLE) + ch.rename_table(raw_table, f"{OLD_PM_SCHEMA_TABLE}.{raw_table}") # Ensure raw_* table if ch.has_table(raw_table): # raw_* table exists, check columns @@ -360,10 +362,7 @@ class MetricScope(Document): ch.execute(post=self.get_create_distributed_sql()) changed = True # Synchronize view - # @todo drop view if changed - if changed or not ch.has_table(table): - ch.execute(post=self.get_create_view_sql()) - changed = True + ch.execute(post=self.get_create_view_sql()) return changed def _get_to_path_code(self) -> str: -- GitLab