Skip to content

Commit cd7dddc

Browse files
khoiproclaude
andauthored
fix(wp-migration): parse table_prefix correctly, not as ';' (closes #25) (#26)
The previous parser used `sed "s/.*'//; s/'.*//"` which is greedy: for a normal line `$table_prefix = 'wp_';`, the first sed matches up to and including the closing single-quote, leaving just `;`. This broke step 6 silently — the URL-update query became `SELECT ... FROM ;options`, mysql errored out under suppressed stderr, and the staging URL never applied to the destination database. Switch to `awk -F"'" '/^\$table_prefix[[:space:]]*=/ {print $2; exit}'` which is robust against whitespace variants and unaffected by the greedy-matching pitfall. Add empty-check and `[A-Za-z0-9_]+` validation since the prefix is interpolated unquoted into SQL during step 6. Bumps VERSION to 0.0.1.3. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f16debb commit cd7dddc

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
## [0.0.1.3] - 2026-05-02
8+
9+
### Fixed
10+
11+
- `wp-migration.sh`: `$table_prefix` is now parsed correctly. The previous `sed "s/.*'//; s/'.*//"` was greedy and captured the trailing `;` instead of the prefix value, producing `prefix: ;` in logs and silently breaking step 6 (URL update would query `;options` and fail). Replaced with `awk -F"'"` + validation that the prefix matches `[A-Za-z0-9_]+` (#25).
12+
713
## [0.0.1.2] - 2026-05-02
814

915
### Fixed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1.2
1+
0.0.1.3

wp-migration.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,28 @@ DB_USER=$(extract_wp_config "DB_USER")
207207
DB_PASS=$(extract_wp_config "DB_PASSWORD")
208208
DB_HOST=$(extract_wp_config "DB_HOST")
209209

210-
# Read table prefix
211-
TABLE_PREFIX=$(grep '^\$table_prefix' "$SRC_PATH/wp-config.php" \
212-
| sed "s/.*'//; s/'.*//" || echo "wp_")
210+
# Read table prefix. wp-config.php convention: $table_prefix = 'wp_';
211+
# Split the line on single-quotes and take the second field — robust across
212+
# whitespace variations and avoids the greedy `.*'` sed pitfall that captured
213+
# the trailing semicolon.
214+
TABLE_PREFIX=$(awk -F"'" '/^\$table_prefix[[:space:]]*=/ {print $2; exit}' "$SRC_PATH/wp-config.php")
213215

214216
if [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASS" ]; then
215217
error "Could not read DB credentials from wp-config.php"
216218
exit 1
217219
fi
218220

221+
if [ -z "$TABLE_PREFIX" ]; then
222+
error "Could not parse \$table_prefix from $SRC_PATH/wp-config.php"
223+
exit 1
224+
fi
225+
226+
# Validate the prefix — used unquoted in SQL queries during step 6
227+
if ! [[ "$TABLE_PREFIX" =~ ^[A-Za-z0-9_]+$ ]]; then
228+
error "Invalid table prefix: '$TABLE_PREFIX' (must match [A-Za-z0-9_]+)"
229+
exit 1
230+
fi
231+
219232
info "Source: $SRC_PATH"
220233
info "Destination: $DEST_USER@$DEST_HOST:$DEST_PATH (port $DEST_PORT)"
221234
info "Database: $DB_NAME (prefix: $TABLE_PREFIX)"

0 commit comments

Comments
 (0)