feat: 添加评论文章链接字段并优化自动迁移脚本
- 在 Comment 表中添加 post_url 字段,用于存储文章完整 URL - 更新前后端 API 以返回 post_url 字段 - 管理后台评论列表显示文章链接(优先显示 post_url) - 移除前端调试日志并优化自动迁移脚本输出信息 - 通过脚本入口文件统一管理迁移任务
This commit is contained in:
@@ -89,7 +89,9 @@
|
|||||||
target="_blank"
|
target="_blank"
|
||||||
class="cell-path"
|
class="cell-path"
|
||||||
:title="item.postSlug"
|
:title="item.postSlug"
|
||||||
>{{ item.postSlug }}</a
|
>
|
||||||
|
{{ item.postUrl || item.postSlug }}
|
||||||
|
</a
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
<div class="table-cell table-cell-status">
|
<div class="table-cell table-cell-status">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "cwd-api",
|
"name": "cwd-api",
|
||||||
"version": "0.0.18",
|
"version": "0.0.18",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"deploy": "node scripts/auto_migrate.js && wrangler deploy",
|
"deploy": "node scripts/index.js && wrangler deploy",
|
||||||
"dev": "wrangler dev",
|
"dev": "wrangler dev",
|
||||||
"start": "wrangler dev",
|
"start": "wrangler dev",
|
||||||
"test": "vitest",
|
"test": "vitest",
|
||||||
|
|||||||
@@ -39,17 +39,11 @@ function findDatabaseName() {
|
|||||||
|
|
||||||
function run() {
|
function run() {
|
||||||
try {
|
try {
|
||||||
console.log('🔍 [Auto-Migrate] Detecting D1 database...');
|
|
||||||
const dbName = findDatabaseName();
|
const dbName = findDatabaseName();
|
||||||
if (!dbName) {
|
if (!dbName) {
|
||||||
console.warn('⚠️ [Auto-Migrate] Could not detect database_name from wrangler config. Skipping.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(`✅ [Auto-Migrate] Found database: ${dbName}`);
|
|
||||||
|
|
||||||
console.log('🔍 [Auto-Migrate] Checking schema status...');
|
|
||||||
try {
|
try {
|
||||||
// Check if post_url column exists
|
|
||||||
const checkCmd = `npx wrangler d1 execute ${dbName} --command "SELECT count(*) as count FROM pragma_table_info('Comment') WHERE name='post_url'" --remote --json`;
|
const checkCmd = `npx wrangler d1 execute ${dbName} --command "SELECT count(*) as count FROM pragma_table_info('Comment') WHERE name='post_url'" --remote --json`;
|
||||||
const output = execSync(checkCmd, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] });
|
const output = execSync(checkCmd, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||||
|
|
||||||
@@ -57,16 +51,12 @@ function run() {
|
|||||||
const count = result[0]?.results?.[0]?.count;
|
const count = result[0]?.results?.[0]?.count;
|
||||||
|
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
console.log('✅ [Auto-Migrate] Schema is up to date.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('⚠️ [Auto-Migrate] Check failed (Database might be new or network error). Skipping migration to be safe.');
|
|
||||||
console.warn(e.message);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('🚀 [Auto-Migrate] Applying migration...');
|
|
||||||
const sql = `
|
const sql = `
|
||||||
ALTER TABLE Comment ADD COLUMN post_url TEXT;
|
ALTER TABLE Comment ADD COLUMN post_url TEXT;
|
||||||
UPDATE Comment SET post_url = post_slug;
|
UPDATE Comment SET post_url = post_slug;
|
||||||
@@ -82,10 +72,10 @@ function run() {
|
|||||||
const migrateCmd = `npx wrangler d1 execute ${dbName} --command "${flatSql}" --remote --yes`;
|
const migrateCmd = `npx wrangler d1 execute ${dbName} --command "${flatSql}" --remote --yes`;
|
||||||
|
|
||||||
execSync(migrateCmd, { stdio: 'inherit' });
|
execSync(migrateCmd, { stdio: 'inherit' });
|
||||||
console.log('✅ [Auto-Migrate] Completed successfully.');
|
console.log('[Auto-Migrate] Migration applied for Comment.post_url.');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ [Auto-Migrate] Failed:', error.message);
|
console.error('[Auto-Migrate] Failed:', error.message);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
cwd-api/scripts/index.js
Normal file
1
cwd-api/scripts/index.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
require('./auto_migrate');
|
||||||
@@ -54,6 +54,7 @@ export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
|
|||||||
name: row.name,
|
name: row.name,
|
||||||
email: row.email,
|
email: row.email,
|
||||||
postSlug: row.post_slug,
|
postSlug: row.post_slug,
|
||||||
|
postUrl: row.post_url,
|
||||||
url: row.url,
|
url: row.url,
|
||||||
ipAddress: row.ip_address,
|
ipAddress: row.ip_address,
|
||||||
contentText: row.content_text,
|
contentText: row.content_text,
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
|
|||||||
let query = `
|
let query = `
|
||||||
SELECT id, name, email, url, content_text as contentText,
|
SELECT id, name, email, url, content_text as contentText,
|
||||||
content_html as contentHtml, created, parent_id as parentId,
|
content_html as contentHtml, created, parent_id as parentId,
|
||||||
post_slug as postSlug, priority, COALESCE(likes, 0) as likes
|
post_slug as postSlug, post_url as postUrl, priority, COALESCE(likes, 0) as likes
|
||||||
FROM Comment
|
FROM Comment
|
||||||
WHERE status = "approved" AND post_slug = ?
|
WHERE status = "approved" AND post_slug = ?
|
||||||
ORDER BY priority DESC, created DESC
|
ORDER BY priority DESC, created DESC
|
||||||
@@ -46,7 +46,7 @@ export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
|
|||||||
query = `
|
query = `
|
||||||
SELECT id, name, email, url, content_text as contentText,
|
SELECT id, name, email, url, content_text as contentText,
|
||||||
content_html as contentHtml, created, parent_id as parentId,
|
content_html as contentHtml, created, parent_id as parentId,
|
||||||
post_slug as postSlug, priority, COALESCE(likes, 0) as likes
|
post_slug as postSlug, post_url as postUrl, priority, COALESCE(likes, 0) as likes
|
||||||
FROM Comment
|
FROM Comment
|
||||||
WHERE status = "approved" AND post_slug IN (${placeholders})
|
WHERE status = "approved" AND post_slug IN (${placeholders})
|
||||||
ORDER BY priority DESC, created DESC
|
ORDER BY priority DESC, created DESC
|
||||||
|
|||||||
@@ -188,7 +188,6 @@ export class CWDComments {
|
|||||||
: this.config.commentPlaceholder;
|
: this.config.commentPlaceholder;
|
||||||
|
|
||||||
const api = createApiClient(this.config);
|
const api = createApiClient(this.config);
|
||||||
console.log(this.config)
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
if (this.hostElement && this.mountPoint) {
|
if (this.hostElement && this.mountPoint) {
|
||||||
this.store = createCommentStore(
|
this.store = createCommentStore(
|
||||||
|
|||||||
Reference in New Issue
Block a user