Skip to content

Commit 1594382

Browse files
authored
Fixed parsing dates (bakwc#51)
1 parent a4bf013 commit 1594382

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

mysql_ch_replicator/clickhouse_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ def insert(self, table_name, records, table_structure: TableStructure = None):
129129
for record in records:
130130
new_record = []
131131
for i, e in enumerate(record):
132+
if isinstance(e, datetime.date) and not isinstance(e, datetime.datetime):
133+
try:
134+
e = datetime.datetime.combine(e, datetime.time())
135+
except ValueError:
136+
e = datetime.datetime(1970, 1, 1)
132137
if isinstance(e, datetime.datetime):
133138
try:
134139
e.timestamp()

test_mysql_ch_replicator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,14 @@ def test_datetime_exception():
562562
id int NOT NULL AUTO_INCREMENT,
563563
name varchar(255),
564564
modified_date DateTime(3) NOT NULL,
565+
test_date date NOT NULL,
565566
PRIMARY KEY (id)
566567
);
567568
''')
568569

569570
mysql.execute(
570-
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date) VALUES ('Ivan', '0000-00-00 00:00:00');",
571+
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date, test_date) "
572+
f"VALUES ('Ivan', '0000-00-00 00:00:00', '2015-05-28');",
571573
commit=True,
572574
)
573575

@@ -584,14 +586,18 @@ def test_datetime_exception():
584586
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 1)
585587

586588
mysql.execute(
587-
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date) VALUES ('Alex', '0000-00-00 00:00:00');",
589+
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date, test_date) "
590+
f"VALUES ('Alex', '0000-00-00 00:00:00', '2015-06-02');",
588591
commit=True,
589592
)
590593
mysql.execute(
591-
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date) VALUES ('Givi', '2023-01-08 03:11:09');",
594+
f"INSERT INTO {TEST_TABLE_NAME} (name, modified_date, test_date) "
595+
f"VALUES ('Givi', '2023-01-08 03:11:09', '2015-06-02');",
592596
commit=True,
593597
)
594598
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 3)
599+
assert_wait(lambda: str(ch.select(TEST_TABLE_NAME, where="name='Alex'")[0]['test_date']) == '2015-06-02')
600+
assert_wait(lambda: str(ch.select(TEST_TABLE_NAME, where="name='Ivan'")[0]['test_date']) == '2015-05-28')
595601

596602

597603
def test_different_types_1():

0 commit comments

Comments
 (0)