|
| 1 | +-- Test employees database integrity using SHA-256 checksums |
| 2 | +-- Uses SHA2() which is available on all MySQL versions (8.0+) |
| 3 | +-- This test works on MySQL 9.6+ where md5() and sha() have been removed |
| 4 | + |
| 5 | +USE employees; |
| 6 | + |
| 7 | +SELECT 'TESTING INSTALLATION' as 'INFO'; |
| 8 | + |
| 9 | +DROP TABLE IF EXISTS expected_values, found_values; |
| 10 | +CREATE TABLE expected_values ( |
| 11 | + table_name varchar(30) not null primary key, |
| 12 | + recs int not null, |
| 13 | + crc_sha2 varchar(100) not null |
| 14 | +); |
| 15 | + |
| 16 | + |
| 17 | +CREATE TABLE found_values (LIKE expected_values); |
| 18 | + |
| 19 | +-- Expected SHA-256 checksums (computed from the canonical data set) |
| 20 | +INSERT INTO `expected_values` VALUES |
| 21 | +('employees', 300024, 'PLACEHOLDER'), |
| 22 | +('departments', 9, 'PLACEHOLDER'), |
| 23 | +('dept_manager', 24, 'PLACEHOLDER'), |
| 24 | +('dept_emp', 331603, 'PLACEHOLDER'), |
| 25 | +('titles', 443308, 'PLACEHOLDER'), |
| 26 | +('salaries', 2844047, 'PLACEHOLDER'); |
| 27 | +SELECT table_name, recs AS expected_records, crc_sha2 AS expected_crc FROM expected_values; |
| 28 | + |
| 29 | +DROP TABLE IF EXISTS tchecksum; |
| 30 | +CREATE TABLE tchecksum (chk char(100)); |
| 31 | + |
| 32 | +SET @crc= ''; |
| 33 | +INSERT INTO tchecksum |
| 34 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, |
| 35 | + emp_no,birth_date,first_name,last_name,gender,hire_date), 256) |
| 36 | + FROM employees ORDER BY emp_no; |
| 37 | +INSERT INTO found_values VALUES ('employees', (SELECT COUNT(*) FROM employees), @crc); |
| 38 | + |
| 39 | +SET @crc = ''; |
| 40 | +INSERT INTO tchecksum |
| 41 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, dept_no,dept_name), 256) |
| 42 | + FROM departments ORDER BY dept_no; |
| 43 | +INSERT INTO found_values VALUES ('departments', (SELECT COUNT(*) FROM departments), @crc); |
| 44 | + |
| 45 | +SET @crc = ''; |
| 46 | +INSERT INTO tchecksum |
| 47 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, dept_no,emp_no, from_date,to_date), 256) |
| 48 | + FROM dept_manager ORDER BY dept_no,emp_no; |
| 49 | +INSERT INTO found_values VALUES ('dept_manager', (SELECT COUNT(*) FROM dept_manager), @crc); |
| 50 | + |
| 51 | +SET @crc = ''; |
| 52 | +INSERT INTO tchecksum |
| 53 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, dept_no,emp_no, from_date,to_date), 256) |
| 54 | + FROM dept_emp ORDER BY dept_no,emp_no; |
| 55 | +INSERT INTO found_values VALUES ('dept_emp', (SELECT COUNT(*) FROM dept_emp), @crc); |
| 56 | + |
| 57 | +SET @crc = ''; |
| 58 | +INSERT INTO tchecksum |
| 59 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, emp_no, title, from_date,to_date), 256) |
| 60 | + FROM titles ORDER BY emp_no,title, from_date; |
| 61 | +INSERT INTO found_values VALUES ('titles', (SELECT COUNT(*) FROM titles), @crc); |
| 62 | + |
| 63 | +SET @crc = ''; |
| 64 | +INSERT INTO tchecksum |
| 65 | + SELECT @crc := SHA2(CONCAT_WS('#',@crc, emp_no, salary, from_date,to_date), 256) |
| 66 | + FROM salaries ORDER BY emp_no,from_date,to_date; |
| 67 | +INSERT INTO found_values VALUES ('salaries', (SELECT COUNT(*) FROM salaries), @crc); |
| 68 | + |
| 69 | +DROP TABLE tchecksum; |
| 70 | + |
| 71 | +SELECT table_name, recs AS found_records, crc_sha2 AS found_crc FROM found_values; |
| 72 | + |
| 73 | +SELECT |
| 74 | + e.table_name, |
| 75 | + IF(e.recs=f.recs,'OK', 'not ok') AS records_match, |
| 76 | + IF(e.crc_sha2=f.crc_sha2,'ok','not ok') AS crc_match |
| 77 | +FROM |
| 78 | + expected_values e INNER JOIN found_values f USING (table_name); |
| 79 | + |
| 80 | +SET @crc_fail=(SELECT COUNT(*) FROM expected_values e INNER JOIN found_values f ON (e.table_name=f.table_name) WHERE f.crc_sha2 != e.crc_sha2); |
| 81 | +SET @count_fail=(SELECT COUNT(*) FROM expected_values e INNER JOIN found_values f ON (e.table_name=f.table_name) WHERE f.recs != e.recs); |
| 82 | + |
| 83 | +SELECT 'CRC' AS summary, IF(@crc_fail = 0, "OK", "FAIL") AS `result` |
| 84 | +UNION ALL |
| 85 | +SELECT 'count', IF(@count_fail = 0, "OK", "FAIL") AS `count`; |
| 86 | + |
| 87 | +DROP TABLE expected_values, found_values; |
0 commit comments