Skip to content

Commit d0c62bb

Browse files
committed
extend debezium demo to add a new column every 30s
1 parent b851818 commit d0c62bb

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

debezium/debezium-xtdb/.mise.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ echo "Deployed to $DEBEZIUM_DIR/lib/"
5050

5151
[tasks.mariadb-writer]
5252
description = "Continuously write to MariaDB (generates CDC events)"
53-
run = "scripts/mysql-writer.sh"
53+
run = "scripts/mariadb-writer.sh"
5454

5555
[tasks.xtdb-poller]
5656
description = "Poll XTDB to show updates flowing in"

debezium/debezium-xtdb/scripts/mysql-writer.sh renamed to debezium/debezium-xtdb/scripts/mariadb-writer.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,37 @@ if ! command -v mariadb &>/dev/null; then
1515
fi
1616

1717
counter=100
18+
column_counter=1
19+
last_column_time=$(date +%s)
20+
COLUMN_INTERVAL="${COLUMN_INTERVAL:-30}"
1821

1922
echo "Writing to MariaDB every ${INTERVAL}s (Ctrl+C to stop)"
23+
echo "Adding new column every ${COLUMN_INTERVAL}s to demonstrate schema evolution"
2024
echo "Host: $MYSQL_HOST:$MYSQL_PORT"
2125
echo ""
2226

2327
mysql_cmd() {
2428
mariadb -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DB" -e "$1" 2>&1
2529
}
2630

31+
add_column_if_due() {
32+
now=$(date +%s)
33+
elapsed=$((now - last_column_time))
34+
if [ $elapsed -ge $COLUMN_INTERVAL ]; then
35+
col_name="custom_field_${column_counter}"
36+
echo ""
37+
echo "[$(date +%H:%M:%S)] *** SCHEMA CHANGE: Adding column '$col_name' ***"
38+
mysql_cmd "ALTER TABLE users ADD COLUMN $col_name VARCHAR(100)"
39+
echo "[$(date +%H:%M:%S)] *** XTDB will automatically adapt to the new schema ***"
40+
echo ""
41+
column_counter=$((column_counter + 1))
42+
last_column_time=$now
43+
fi
44+
}
45+
2746
while true; do
47+
add_column_if_due
48+
2849
username="user_${counter}"
2950
email="${username}@example.com"
3051

@@ -33,9 +54,14 @@ while true; do
3354

3455
case $op in
3556
0)
36-
# INSERT new user
57+
# INSERT new user (include value for latest custom field if any exist)
3758
echo "[$(date +%H:%M:%S)] INSERT: $username"
38-
mysql_cmd "INSERT INTO users (id, email, username, created_at) VALUES ($counter, '$email', '$username', NOW())"
59+
if [ $column_counter -gt 1 ]; then
60+
latest_col="custom_field_$((column_counter - 1))"
61+
mysql_cmd "INSERT INTO users (id, email, username, created_at, $latest_col) VALUES ($counter, '$email', '$username', NOW(), 'value_${counter}')"
62+
else
63+
mysql_cmd "INSERT INTO users (id, email, username, created_at) VALUES ($counter, '$email', '$username', NOW())"
64+
fi
3965
;;
4066
1)
4167
# UPDATE existing user (previous one)

0 commit comments

Comments
 (0)