fix: support replies on legacy databases
This commit is contained in:
1 parent
68b58851e8
commit
3840bb3c54
4 files changed
+145
-24
No files matched your search
+9
-2
@@ -77,10 +77,17 @@ export const API = {
|
||||
|
||||
// 回复
|
||||
getReplies(postId) { return apiFetch('/posts/' + postId + '/replies'); },
|
||||
createReply(postId, content, captcha, replyToId) {
|
||||
createReply(postId, content, captcha, replyToId, replyToUsername) {
|
||||
return apiFetch('/posts/' + postId + '/replies', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig, reply_to_id: replyToId || null }),
|
||||
body: JSON.stringify({
|
||||
content,
|
||||
captcha_id: captcha?.id,
|
||||
captcha_answer: captcha?.answer,
|
||||
captcha_sig: captcha?.sig,
|
||||
reply_to_id: replyToId || null,
|
||||
reply_to_username: replyToUsername || null,
|
||||
}),
|
||||
});
|
||||
},
|
||||
deleteReply(replyId) { return apiFetch('/replies/' + replyId, { method: 'DELETE' }); },
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// 回复表兼容层:存量数据库不会因 CREATE TABLE IF NOT EXISTS 自动获得新列。
|
||||
// 仅读取现有结构,不在用户请求中执行 DDL;旧库降级到
|
||||
// reply_to_username 或不记录回复目标,避免读取和普通回复整体失败。
|
||||
import pool from '@/lib/db';
|
||||
|
||||
const REPLY_COLUMNS_SQL = `
|
||||
SELECT attname AS column_name
|
||||
FROM pg_attribute
|
||||
WHERE attrelid = to_regclass('replies')
|
||||
AND attnum > 0
|
||||
AND NOT attisdropped
|
||||
AND attname IN ('reply_to_id', 'reply_to_username')
|
||||
`;
|
||||
|
||||
async function inspectReplyColumns() {
|
||||
const result = await pool.query(REPLY_COLUMNS_SQL);
|
||||
const columns = new Set(result.rows.map((row) => row.column_name));
|
||||
return {
|
||||
replyToId: columns.has('reply_to_id'),
|
||||
replyToUsername: columns.has('reply_to_username'),
|
||||
};
|
||||
}
|
||||
|
||||
export function getReplySchemaCapabilities() {
|
||||
if (!globalThis.__forumlifyReplySchemaPromise) {
|
||||
globalThis.__forumlifyReplySchemaPromise = inspectReplyColumns().catch((error) => {
|
||||
globalThis.__forumlifyReplySchemaPromise = null;
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return globalThis.__forumlifyReplySchemaPromise;
|
||||
}
|
||||
Reference in new issue
Block a user