diff --git a/core/clickhouse/connect.py b/core/clickhouse/connect.py index 0bbd424c8cef21e7891634a03cbfd4aa4a1eb536..2ec2bcf62544e9fecccf96628691d5ece1b85352 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 0ca8c33931c7e9150c2935890061f5fb62598afa..2a3109ba2c19543a803db0c820db0950e82a91d5 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: