Skip to content

Commit 5bb7c14

Browse files
Create docker environment
1 parent 4cd17f1 commit 5bb7c14

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.sql text eol=lf
2+
*.csv text eol=lf
3+
*.sh text eol=lf
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: SQL Bootstrap Expected
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
record-expected:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: write
11+
pull-requests: write
12+
13+
services:
14+
mysql:
15+
image: mysql:8
16+
env:
17+
MYSQL_ROOT_PASSWORD: rootpw
18+
ports:
19+
- 3306:3306
20+
options: >-
21+
--health-cmd="mysqladmin ping -h 127.0.0.1 -prootpw || exit 1"
22+
--health-interval=5s
23+
--health-timeout=3s
24+
--health-retries=60
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install MySQL client
30+
run: |
31+
sudo apt-get update
32+
sudo apt-get install -y mysql-client
33+
chmod +x tests/run_tests.sh
34+
35+
- name: Record expected CSVs
36+
env:
37+
DB_HOST: 127.0.0.1
38+
DB_PORT: 3306
39+
DB_USER: root
40+
DB_PASS: rootpw
41+
DB_NAME: testdb
42+
RECORD: "1"
43+
run: ./tests/run_tests.sh
44+
45+
- name: Commit changes
46+
run: |
47+
git config user.name "github-actions[bot]"
48+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
49+
git checkout -b bootstrap-expected
50+
git add tests/expected/*.csv || true
51+
if git diff --cached --quiet; then
52+
echo "No expected files to commit."
53+
else
54+
git commit -m "Bootstrap expected CSVs"
55+
git push -u origin bootstrap-expected
56+
fi
57+
58+
- name: Create pull request
59+
uses: peter-evans/create-pull-request@v6
60+
with:
61+
token: ${{ secrets.GITHUB_TOKEN }}
62+
branch: bootstrap-expected
63+
title: "Bootstrap expected CSVs"
64+
body: "Erzeugt Expected-Dateien aus aktuellen SQL-Ausgaben."
65+
delete-branch: true

.github/workflows/sql-ci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: SQL CI (MySQL)
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test-sql:
9+
runs-on: ubuntu-latest
10+
11+
services:
12+
mysql:
13+
image: mysql:8
14+
env:
15+
MYSQL_ROOT_PASSWORD: rootpw
16+
ports:
17+
- 3306:3306
18+
options: >-
19+
--health-cmd="mysqladmin ping -h 127.0.0.1 -prootpw || exit 1"
20+
--health-interval=5s
21+
--health-timeout=3s
22+
--health-retries=60
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
- name: Install MySQL client
28+
run: |
29+
sudo apt-get update
30+
sudo apt-get install -y mysql-client
31+
chmod +x tests/run_tests.sh
32+
33+
- name: Run SQL snapshot tests
34+
env:
35+
DB_HOST: 127.0.0.1
36+
DB_PORT: 3306
37+
DB_USER: root
38+
DB_PASS: rootpw
39+
DB_NAME: testdb
40+
run: ./tests/run_tests.sh
41+
42+
- name: Upload outputs on failure
43+
if: failure()
44+
uses: actions/upload-artifact@v4
45+
with:
46+
name: sql-test-outputs
47+
path: tests/out/*.csv

src/sql/foo.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT id, name, price
2+
FROM products
3+
WHERE category = 'tools'
4+
ORDER BY price, id;

tests/fixtures/seed.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DROP TABLE IF EXISTS products;
2+
CREATE TABLE products (
3+
id INT PRIMARY KEY,
4+
name VARCHAR(100) NOT NULL,
5+
price DECIMAL(10,2) NOT NULL,
6+
category VARCHAR(50) NOT NULL
7+
);
8+
9+
INSERT INTO products (id, name, price, category) VALUES
10+
(1, 'Widget A', 10.00, 'tools'),
11+
(2, 'Widget B', 12.50, 'tools'),
12+
(3, 'Cable X', 5.90, 'cables'),
13+
(4, 'Cable Y', 8.10, 'cables'),
14+
(5, 'Adapter', 7.00, 'accessories');

tests/run_tests.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
DB_HOST="${DB_HOST:-127.0.0.1}"
5+
DB_PORT="${DB_PORT:-3306}"
6+
DB_USER="${DB_USER:-root}"
7+
DB_PASS="${DB_PASS:-rootpw}"
8+
DB_NAME="${DB_NAME:-testdb}"
9+
RECORD="${RECORD:-0}" # 1 = Expected-Dateien neu schreiben
10+
11+
CLI="mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USER} -p${DB_PASS} --batch --skip-column-names"
12+
13+
# Nur SELECTs zulassen (Heuristik)
14+
for sql in src/sql/*.sql; do
15+
if grep -Eiq '(^|[^a-z])(insert|update|delete|merge|replace|create|alter|drop)\b' "$sql"; then
16+
echo "❌ $sql enthält schreibende/DDL-Statements. Erwartet werden reine SELECTs."
17+
exit 3
18+
fi
19+
done
20+
21+
# DB vorbereiten
22+
$CLI -e "CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` /*\!40100 DEFAULT CHARACTER SET utf8mb4 */;"
23+
$CLI -e "SET GLOBAL sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';"
24+
$CLI -e "SET GLOBAL time_zone='+00:00';"
25+
$CLI "${DB_NAME}" < tests/fixtures/seed.sql
26+
27+
mkdir -p tests/out tests/expected
28+
fail=0
29+
30+
for sql in src/sql/*.sql; do
31+
name="$(basename "$sql" .sql)"
32+
out="tests/out/${name}.csv"
33+
exp="tests/expected/${name}.csv"
34+
35+
$CLI "${DB_NAME}" < "$sql" > "$out"
36+
37+
sed -i 's/\r$//' "$out"
38+
sed -i 's/[[:space:]]\+$//' "$out"
39+
40+
if [ "$RECORD" = "1" ]; then
41+
cp -f "$out" "$exp"
42+
echo "📝 Recorded: $exp"
43+
else
44+
if ! diff -u "$exp" "$out"; then
45+
echo "❌ Test failed: ${name}"
46+
fail=1
47+
else
48+
echo "✅ Test passed: ${name}"
49+
fi
50+
fi
51+
done
52+
53+
exit $fail

0 commit comments

Comments
 (0)