Skip to content

Commit af1e75c

Browse files
committed
Add SHA2 integrity test using SHA-256 (works on MySQL 9.6+)
SHA2() is available on all MySQL versions. This test uses SHA-256 instead of SHA-1, making it the primary integrity test for MySQL 9.6+ where md5() and sha() were removed. Expected values are placeholders pending first CI run to compute them.
1 parent cf7d765 commit af1e75c

2 files changed

Lines changed: 111 additions & 10 deletions

File tree

.github/workflows/ci-mysql.yml

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,18 @@ jobs:
136136
fi
137137
echo "SHA OK ($sha_ok matches)"
138138
139-
- name: Verify row counts (MySQL 9.6+ fallback)
140-
if: steps.features.outputs.has_sha == 'false'
139+
- name: Test SHA2 integrity
141140
run: |
142-
SBDIR=$(ls -d ~/sandboxes/msb_*)
143-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM employees;" employees | grep -q 300024
144-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM departments;" employees | grep -q 9
145-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM dept_manager;" employees | grep -q 24
146-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM dept_emp;" employees | grep -q 331603
147-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM titles;" employees | grep -q 443308
148-
$SBDIR/use -BN -e "SELECT COUNT(*) FROM salaries;" employees | grep -q 2844047
149-
echo "Row counts OK"
141+
EXTRA_ARGS=""
142+
[ "${{ steps.features.outputs.needs_commands }}" == "true" ] && EXTRA_ARGS="--commands"
143+
~/sandboxes/msb_*/use $EXTRA_ARGS -t < test_employees_sha2.sql > /tmp/test_sha2.txt
144+
cat /tmp/test_sha2.txt
145+
sha2_ok=$(grep -iw ok /tmp/test_sha2.txt | wc -l | tr -d ' \t')
146+
if [ "$sha2_ok" != "8" ]; then
147+
echo "SHA2 FAIL - expected 8 OK - found $sha2_ok"
148+
exit 1
149+
fi
150+
echo "SHA2 OK ($sha2_ok matches)"
150151
151152
- name: Load objects (stored procedures/functions)
152153
run: |
@@ -290,6 +291,19 @@ jobs:
290291
fi
291292
echo "MD5 OK ($md5_ok matches)"
292293
294+
- name: Test SHA2 integrity
295+
run: |
296+
EXTRA_ARGS=""
297+
[ "${{ steps.features.outputs.needs_commands }}" == "true" ] && EXTRA_ARGS="--commands"
298+
~/sandboxes/msb_*/use $EXTRA_ARGS -t < test_employees_sha2.sql > /tmp/test_sha2.txt
299+
cat /tmp/test_sha2.txt
300+
sha2_ok=$(grep -iw ok /tmp/test_sha2.txt | wc -l | tr -d ' \t')
301+
if [ "$sha2_ok" != "8" ]; then
302+
echo "SHA2 FAIL - expected 8 OK - found $sha2_ok"
303+
exit 1
304+
fi
305+
echo "SHA2 OK ($sha2_ok matches)"
306+
293307
- name: Cleanup
294308
if: always()
295309
run: |

test_employees_sha2.sql

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)