forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate-ch.py
executable file
·57 lines (48 loc) · 1.84 KB
/
migrate-ch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ----------------------------------------------------------------------
# CH database schema migration tool
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# NOC modules
from noc.core.management.base import BaseCommand
from noc.core.clickhouse.connect import connection
from noc.core.clickhouse.ensure import ensure_bi_models, ensure_pm_scopes
from noc.core.mongo.connection import connect as mongo_connect
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("--host", dest="host", help="ClickHouse address")
parser.add_argument("--port", dest="port", type=int, help="ClickHouse port")
def handle(self, host=None, port=None, *args, **options):
self.host = host or None
self.port = port or None
self.connect()
mongo_connect()
self.create_dictionaries_db()
self.ensure_db()
changed = ensure_bi_models(connect=self.connect)
changed |= ensure_pm_scopes(connect=self.connect)
if changed:
self.print("CHANGED")
else:
self.print("OK")
def connect(self):
"""
Connect to database
:return:
"""
self.connect = connection(host=self.host, port=self.port, read_only=False)
def ensure_db(self):
"""
Ensure clickhouse database is exists
:return:
"""
self.print("Ensuring database")
self.connect.ensure_db()
def create_dictionaries_db(self):
self.print("Ensuring Dictionary database")
self.connect.execute(
post="CREATE DATABASE IF NOT EXISTS dictionaries ENGINE = Dictionary", nodb=True
)
if __name__ == "__main__":
Command().run()