Skip to content

Commit eb6cfeb

Browse files
authored
Settings validation (#11)
1 parent 24cfa56 commit eb6cfeb

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Release][release-image]][releases]
44
[![License][license-image]][license]
55

6-
[release-image]: https://img.shields.io/badge/release-0.0.19-blue.svg?style=flat
6+
[release-image]: https://img.shields.io/badge/release-0.0.20-blue.svg?style=flat
77
[releases]: https://github.com/bakwc/mysql_ch_replicator/releases
88

99
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat

mysql_ch_replicator/config.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@
44
from dataclasses import dataclass
55

66

7+
def stype(obj):
8+
return type(obj).__name__
9+
10+
711
@dataclass
812
class MysqlSettings:
913
host: str = 'localhost'
1014
port: int = 3306
1115
user: str = 'root'
1216
password: str = ''
1317

18+
def validate(self):
19+
if not isinstance(self.host, str):
20+
raise ValueError(f'mysql host should be string and not {stype(self.host)}')
21+
22+
if not isinstance(self.port, int):
23+
raise ValueError(f'mysql port should be int and not {stype(self.port)}')
24+
25+
if not isinstance(self.user, str):
26+
raise ValueError(f'mysql user should be string and not {stype(self.user)}')
27+
28+
if not isinstance(self.password, str):
29+
raise ValueError(f'mysql password should be string and not {stype(self.password)}')
30+
1431

1532
@dataclass
1633
class ClickhouseSettings:
@@ -19,12 +36,35 @@ class ClickhouseSettings:
1936
user: str = 'root'
2037
password: str = ''
2138

39+
def validate(self):
40+
if not isinstance(self.host, str):
41+
raise ValueError(f'clickhouse host should be string and not {stype(self.host)}')
42+
43+
if not isinstance(self.port, int):
44+
raise ValueError(f'clickhouse port should be int and not {stype(self.port)}')
45+
46+
if not isinstance(self.user, str):
47+
raise ValueError(f'clickhouse user should be string and not {stype(self.user)}')
48+
49+
if not isinstance(self.password, str):
50+
raise ValueError(f'clickhouse password should be string and not {stype(self.password)}')
51+
2252

2353
@dataclass
2454
class BinlogReplicatorSettings:
2555
data_dir: str = 'binlog'
2656
records_per_file: int = 100000
2757

58+
def validate(self):
59+
if not isinstance(self.data_dir, str):
60+
raise ValueError(f'binlog_replicator data_dir should be string and not {stype(self.data_dir)}')
61+
62+
if not isinstance(self.records_per_file, int):
63+
raise ValueError(f'binlog_replicator records_per_file should be int and not {stype(self.data_dir)}')
64+
65+
if self.records_per_file <= 0:
66+
raise ValueError('binlog_replicator records_per_file should be positive')
67+
2868

2969
class Settings:
3070

@@ -48,6 +88,7 @@ def load(self, settings_file):
4888
assert isinstance(self.databases, str) or isinstance(self.databases, list)
4989
assert isinstance(self.tables, str) or isinstance(self.tables, list)
5090
self.binlog_replicator = BinlogReplicatorSettings(**data['binlog_replicator'])
91+
self.validate()
5192

5293
@classmethod
5394
def is_pattern_matches(cls, substr, pattern):
@@ -67,3 +108,8 @@ def is_database_matches(self, db_name):
67108

68109
def is_table_matches(self, table_name):
69110
return self.is_pattern_matches(table_name, self.tables)
111+
112+
def validate(self):
113+
self.mysql.validate()
114+
self.clickhouse.validate()
115+
self.binlog_replicator.validate()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mysql-ch-replicator"
3-
version = "0.0.19"
3+
version = "0.0.20"
44
description = "Tool for replication of MySQL databases to ClickHouse"
55
authors = ["Filipp Ozinov <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)