freshyo/apps/backend/scripts/populate_localdb.sh
2026-11-05 23:33:30 +05:30

66 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
DUMP_FILE="$ROOT_DIR/dumps/latest.sql"
# Split files live under .wrangler (gitignored) so a pull never adds untracked
# artefacts next to the tracked dumps.
SPLIT_DIR="$ROOT_DIR/.wrangler/pull"
SCHEMA_FILE="$SPLIT_DIR/schema.sql"
DATA_FILE="$SPLIT_DIR/data.sql"
if [ "${1:-}" = "--dev" ]; then
WRANGLER_CONFIG="$ROOT_DIR/wrangler.dev.toml"
DB_NAME="freshyo-backend-dev"
else
WRANGLER_CONFIG="$ROOT_DIR/wrangler.prod.toml"
DB_NAME="freshyo-dev"
fi
if [ ! -f "$DUMP_FILE" ]; then
echo "Dump file not found: $DUMP_FILE"
exit 1
fi
if [ ! -f "$WRANGLER_CONFIG" ]; then
echo "Wrangler config not found: $WRANGLER_CONFIG"
exit 1
fi
mkdir -p "$SPLIT_DIR"
# `wrangler d1 export` emits tables in sqlite_master rowid order (the order they
# were historically created), so a table can be created and filled before the
# parent it references exists — e.g. `notif_creds`/`unlogged_user_tokens` ->
# `regions`, or `product_skus` -> `product_info` after the SKU-split rebuild.
#
# Local D1 pins `foreign_keys=ON` and runs the whole file as one transaction, so
# neither `PRAGMA foreign_keys=OFF` (a no-op) nor the dump's own
# `defer_foreign_keys` avoids `no such table: main.<parent>`.
#
# `CREATE TABLE` is allowed to reference a table that does not exist yet (FKs are
# resolved at DML time), so replaying every CREATE first and every INSERT second
# removes the "no such table" failure. See wrangler-commands.md.
#
# The dump's row order is not FK-safe either — child rows can precede their
# parent rows (e.g. `product_skus` before `product_info`). D1 enforces FKs per
# statement, so the data pass runs with deferred enforcement: every row is
# checked once at COMMIT, by which point the whole dump is loaded. (Verified:
# `PRAGMA foreign_key_check` on the imported DB reports no violations.)
awk -v schema="$SCHEMA_FILE" -v data="$DATA_FILE" '
BEGIN { print "PRAGMA defer_foreign_keys=TRUE;" > data }
/^CREATE/ { in_create = 1 }
in_create {
print > schema
# Multi-line CREATE TABLE blocks end with a line that is just `);`.
if ($0 ~ /\);[[:space:]]*$/) in_create = 0
next
}
/^PRAGMA/ { print > schema; next }
{ print > data }
' "$DUMP_FILE"
wrangler d1 execute "$DB_NAME" --local --file "$SCHEMA_FILE" --config "$WRANGLER_CONFIG"
wrangler d1 execute "$DB_NAME" --local --file "$DATA_FILE" --config "$WRANGLER_CONFIG"
echo "Local D1 database populated from $DUMP_FILE"