feat: harden pocketbase migration readiness checks

This commit is contained in:
Codex
2026-06-30 00:51:20 +08:00
parent db86fa3705
commit 889a53c90f
7 changed files with 348 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
export interface PocketBaseField {
name: string;
@@ -24,8 +25,20 @@ export interface PocketBaseCollection {
deleteRule?: string;
}
export function readPocketBaseSchema(schemaPath = process.env.PB_SCHEMA_PATH || '../../docs/pb_schema.json') {
const absPath = path.resolve(process.cwd(), schemaPath);
function defaultSchemaPath() {
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
const candidates = [
path.resolve(process.cwd(), process.env.PB_SCHEMA_PATH || ''),
path.resolve(process.cwd(), '../../docs/pb_schema.json'),
path.resolve(process.cwd(), 'docs/pb_schema.json'),
path.resolve(moduleDir, '../../../docs/pb_schema.json'),
].filter(candidate => candidate && candidate !== process.cwd());
return candidates.find(candidate => fs.existsSync(candidate)) || candidates[candidates.length - 1];
}
export function readPocketBaseSchema(schemaPath = process.env.PB_SCHEMA_PATH || '') {
const absPath = schemaPath ? path.resolve(process.cwd(), schemaPath) : defaultSchemaPath();
const raw = JSON.parse(fs.readFileSync(absPath, 'utf8'));
const collections = (Array.isArray(raw) ? raw : raw.collections || []) as PocketBaseCollection[];
const byId = new Map(collections.map(c => [c.id, c]));