Skip to content

Commit 4116789

Browse files
committed
fix: simplify D1 migration step — use wrangler.jsonc directly
1 parent f7eff56 commit 4116789

1 file changed

Lines changed: 18 additions & 67 deletions

File tree

action.yml

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -441,81 +441,32 @@ runs:
441441
CLOUDFLARE_API_TOKEN: ${{ inputs.cloudflare-api-token }}
442442
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.cloudflare-account-id }}
443443
run: |
444-
# Find the merged wrangler config
445-
# The merge step combines base wrangler.jsonc with the environment-specific config
446-
CONFIG=""
447-
for f in ".output/server/wrangler.json" ".output/wrangler.json" "wrangler.jsonc" "wrangler.toml"; do
448-
if [ -f "$f" ]; then
449-
CONFIG="$f"
450-
break
451-
fi
452-
done
444+
if [ ! -d "server/db/migrations" ]; then
445+
echo "::notice::No migrations directory found, skipping D1 migrations"
446+
exit 0
447+
fi
453448
454-
if [ -z "$CONFIG" ]; then
455-
echo "::notice::No wrangler config found, skipping D1 migrations"
449+
if [ ! -f "wrangler.jsonc" ]; then
450+
echo "::notice::No wrangler.jsonc found, skipping D1 migrations"
456451
exit 0
457452
fi
458453
459-
echo "Using config: $CONFIG"
454+
# Get database names from config
455+
DB_NAMES=$(node -e "
456+
const fs = require('fs');
457+
const c = JSON.parse(fs.readFileSync('wrangler.jsonc','utf8').replace(/\/\/.*/g,'').replace(/,\s*([}\]])/g,'\$1'));
458+
(c.d1_databases||[]).forEach(d => console.log(d.database_name||d.name));
459+
")
460460
461-
# Extract D1 database names from config (strip JSONC comments first)
462-
D1_NAMES=$(node --eval '
463-
const fs = require("fs");
464-
function stripJsonComments(str) {
465-
let result = "", inString = false, escape = false;
466-
for (let i = 0; i < str.length; i++) {
467-
const ch = str[i];
468-
if (escape) { result += ch; escape = false; continue; }
469-
if (ch === "\\" && inString) { result += ch; escape = true; continue; }
470-
if (ch === "\"") { inString = !inString; result += ch; continue; }
471-
if (!inString && ch === "/" && str[i+1] === "/") { while (i < str.length && str[i] !== "\n") i++; continue; }
472-
if (!inString && ch === "/" && str[i+1] === "*") { i += 2; while (i < str.length && !(str[i] === "*" && str[i+1] === "/")) i++; i++; continue; }
473-
result += ch;
474-
}
475-
return result;
476-
}
477-
const raw = fs.readFileSync("'"$CONFIG"'", "utf8");
478-
const clean = "'"$CONFIG"'".endsWith(".json") ? raw : stripJsonComments(raw);
479-
const config = JSON.parse(clean);
480-
const dbs = config.d1_databases || [];
481-
dbs.forEach(db => console.log(db.database_name || db.name));
482-
')
483-
484-
if [ -z "$D1_NAMES" ]; then
485-
echo "::notice::No D1 databases found in config, skipping migrations"
461+
if [ -z "$DB_NAMES" ]; then
462+
echo "::notice::No D1 databases in config, skipping migrations"
486463
exit 0
487464
fi
488465
489-
# Inject migrations_dir into the config so wrangler finds migrations in the source tree
490-
# The config is in .output/server/ but migrations live in server/db/migrations/
491-
node --eval '
492-
const fs = require("fs");
493-
const config = JSON.parse(fs.readFileSync("'"$CONFIG"'", "utf8"));
494-
const configDir = require("path").dirname(require("path").resolve("'"$CONFIG"'"));
495-
const cwd = process.cwd();
496-
if (config.d1_databases) {
497-
config.d1_databases.forEach(db => {
498-
if (!db.migrations_dir) {
499-
// Look for migrations in common locations
500-
const dirs = ["server/db/migrations/sqlite", "server/db/migrations", "db/migrations"];
501-
for (const dir of dirs) {
502-
if (fs.existsSync(dir)) {
503-
db.migrations_dir = require("path").relative(configDir, require("path").resolve(cwd, dir));
504-
console.log("Set migrations_dir for " + (db.database_name || db.name) + ": " + db.migrations_dir);
505-
break;
506-
}
507-
}
508-
}
509-
});
510-
fs.writeFileSync("'"$CONFIG"'", JSON.stringify(config, null, 2));
511-
}
512-
'
513-
514-
# Apply migrations for each D1 database
515-
while IFS= read -r DB_NAME; do
516-
echo "Applying migrations for D1 database: $DB_NAME"
517-
npx wrangler d1 migrations apply "$DB_NAME" --remote -c "$CONFIG"
518-
done <<< "$D1_NAMES"
466+
while IFS= read -r DB; do
467+
echo "Applying migrations for: $DB"
468+
npx wrangler d1 migrations apply "$DB" --remote -c wrangler.jsonc
469+
done <<< "$DB_NAMES"
519470
520471
- name: Upload Worker Version
521472
if: ${{ inputs.mode == 'full' || inputs.mode == 'deploy' }}

0 commit comments

Comments
 (0)