Skip to content

Commit f619869

Browse files
authored
Validate primary key not null (#23)
1 parent 1579642 commit f619869

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mysql_ch_replicator/db_replicator.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .mysql_api import MySQLApi
1212
from .clickhouse_api import ClickhouseApi
1313
from .converter import MysqlToClickhouseConverter, strip_sql_name, strip_sql_comments
14-
from .table_structure import TableStructure
14+
from .table_structure import TableStructure, TableField
1515
from .binlog_replicator import DataReader, LogEvent, EventType
1616
from .utils import GracefulKiller, touch_all_files
1717

@@ -147,6 +147,17 @@ def validate_database_settings(self):
147147
'Otherwise you will get DUPLICATES in your SELECT queries\n\n\n'
148148
)
149149

150+
def validate_mysql_structure(self, mysql_structure: TableStructure):
151+
primary_field: TableField = mysql_structure.fields[mysql_structure.primary_key_idx]
152+
if 'not null' not in primary_field.parameters.lower():
153+
logger.warning('primary key validation failed')
154+
logger.warning(
155+
f'\n\n\n !!! WARNING - PRIMARY KEY NULLABLE (field "{primary_field.name}", table "{mysql_structure.table_name}") !!!\n\n'
156+
'There could be errors replicating nullable primary key\n'
157+
'Please ensure all tables has NOT NULL parameter for primary key\n'
158+
'Or mark tables as skipped, see "exclude_tables" option\n\n\n'
159+
)
160+
150161
def run(self):
151162
try:
152163
logger.info('launched db_replicator')
@@ -199,6 +210,7 @@ def create_initial_structure_table(self, table_name):
199210
mysql_structure = self.converter.parse_mysql_table_structure(
200211
mysql_create_statement, required_table_name=table_name,
201212
)
213+
self.validate_mysql_structure(mysql_structure)
202214
clickhouse_structure = self.converter.convert_table_structure(mysql_structure)
203215
self.state.tables_structure[table_name] = (mysql_structure, clickhouse_structure)
204216
self.clickhouse_api.create_table(clickhouse_structure)

0 commit comments

Comments
 (0)