feat: bootstrap D1 schema when database is empty
This commit is contained in:
parent
99a511f9f0
commit
62a36acbb6
2 changed files with 421 additions and 353 deletions
34
.github/workflows/deploy-dev.yaml
vendored
34
.github/workflows/deploy-dev.yaml
vendored
|
|
@ -108,6 +108,40 @@ jobs:
|
|||
echo "⏭️ R2_NAME_DEV not set, skipping R2 binding"
|
||||
fi
|
||||
|
||||
- name: Bootstrap D1 schema when database is empty (dev)
|
||||
run: |
|
||||
set -euo pipefail
|
||||
WRANGLER="npx --yes wrangler@${WRANGLER_VERSION}"
|
||||
|
||||
TABLE_COUNT=$($WRANGLER d1 execute vault1 --env dev --remote --command \
|
||||
"SELECT COUNT(*) AS cnt FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT IN ('_cf_METADATA','d1_migrations');" \
|
||||
--json | jq -r '.[0].results[0].cnt // 0')
|
||||
|
||||
echo "Existing user table count: ${TABLE_COUNT}"
|
||||
if [ "${TABLE_COUNT}" = "0" ]; then
|
||||
echo "🆕 Empty D1 database detected; applying sql/schema.sql..."
|
||||
$WRANGLER d1 execute vault1 --env dev --remote --file sql/schema.sql
|
||||
|
||||
echo "📝 Marking existing migrations as applied (schema.sql already includes them)"
|
||||
BOOTSTRAP_SQL="$(mktemp)"
|
||||
cat >"${BOOTSTRAP_SQL}" <<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS d1_migrations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
SQL
|
||||
shopt -s nullglob
|
||||
for f in migrations/*.sql; do
|
||||
echo "INSERT OR IGNORE INTO d1_migrations (name) VALUES ('$(basename "${f}")');" >>"${BOOTSTRAP_SQL}"
|
||||
done
|
||||
|
||||
$WRANGLER d1 execute vault1 --env dev --remote --file "${BOOTSTRAP_SQL}"
|
||||
rm -f "${BOOTSTRAP_SQL}"
|
||||
else
|
||||
echo "⏭️ D1 already has tables; skipping base schema bootstrap"
|
||||
fi
|
||||
|
||||
- name: Apply D1 database migrations (dev)
|
||||
run: |
|
||||
set -euo pipefail # Ensure pipe returns first non-zero exit code
|
||||
|
|
|
|||
34
.github/workflows/push-cloudflare.yaml
vendored
34
.github/workflows/push-cloudflare.yaml
vendored
|
|
@ -118,6 +118,40 @@ jobs:
|
|||
echo "⏭️ R2_NAME not set, skipping R2 binding"
|
||||
fi
|
||||
|
||||
- name: Bootstrap D1 schema when database is empty
|
||||
run: |
|
||||
set -euo pipefail
|
||||
WRANGLER="npx --yes wrangler@${WRANGLER_VERSION}"
|
||||
|
||||
TABLE_COUNT=$($WRANGLER d1 execute vault1 --remote --command \
|
||||
"SELECT COUNT(*) AS cnt FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT IN ('_cf_METADATA','d1_migrations');" \
|
||||
--json | jq -r '.[0].results[0].cnt // 0')
|
||||
|
||||
echo "Existing user table count: ${TABLE_COUNT}"
|
||||
if [ "${TABLE_COUNT}" = "0" ]; then
|
||||
echo "🆕 Empty D1 database detected; applying sql/schema.sql..."
|
||||
$WRANGLER d1 execute vault1 --remote --file sql/schema.sql
|
||||
|
||||
echo "📝 Marking existing migrations as applied (schema.sql already includes them)"
|
||||
BOOTSTRAP_SQL="$(mktemp)"
|
||||
cat >"${BOOTSTRAP_SQL}" <<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS d1_migrations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
SQL
|
||||
shopt -s nullglob
|
||||
for f in migrations/*.sql; do
|
||||
echo "INSERT OR IGNORE INTO d1_migrations (name) VALUES ('$(basename "${f}")');" >>"${BOOTSTRAP_SQL}"
|
||||
done
|
||||
|
||||
$WRANGLER d1 execute vault1 --remote --file "${BOOTSTRAP_SQL}"
|
||||
rm -f "${BOOTSTRAP_SQL}"
|
||||
else
|
||||
echo "⏭️ D1 already has tables; skipping base schema bootstrap"
|
||||
fi
|
||||
|
||||
- name: Apply D1 database migrations
|
||||
run: |
|
||||
set -euo pipefail # Ensure pipe returns first non-zero exit code
|
||||
|
|
|
|||
Loading…
Reference in a new issue