fix: merge tags did not update disc

This commit is contained in:
2026-02-15 21:08:41 +01:00
parent 97c0ee6877
commit 8b70214d15
3 changed files with 49 additions and 0 deletions

View File

@@ -519,6 +519,38 @@ export class PostEngine extends EventEmitter {
return this.dbRowToPostData(dbPost, ''); return this.dbRowToPostData(dbPost, '');
} }
/**
* Sync a published post's file with current database metadata (e.g., tags).
* This is needed when metadata changes outside of normal post editing flow,
* such as tag merge or rename operations.
*
* @param postId - The post ID to sync
* @returns true if file was updated, false if post is not published or doesn't exist
*/
async syncPublishedPostFile(postId: string): Promise<boolean> {
const db = getDatabase().getLocal();
const dbPost = await db.select().from(posts).where(eq(posts.id, postId)).get();
if (!dbPost || !dbPost.filePath) {
// Not a published post or doesn't exist
return false;
}
// Read content from the existing file
const fileData = await this.readPostFile(dbPost.filePath);
if (!fileData) {
return false;
}
// Build the full post data with DB metadata (tags) and file content
const postData = this.dbRowToPostData(dbPost, fileData.content);
// Re-write the file with updated metadata
await this.writePostFile(postData);
return true;
}
async getAllPosts(options?: PaginationOptions): Promise<PaginatedResult<PostData>> { async getAllPosts(options?: PaginationOptions): Promise<PaginatedResult<PostData>> {
const db = getDatabase().getLocal(); const db = getDatabase().getLocal();
const limit = options?.limit ?? 500; const limit = options?.limit ?? 500;

View File

@@ -7,6 +7,7 @@ import { eq, and, asc, sql, like } from 'drizzle-orm';
import { getDatabase } from '../database'; import { getDatabase } from '../database';
import { tags, posts } from '../database/schema'; import { tags, posts } from '../database/schema';
import { taskManager } from './TaskManager'; import { taskManager } from './TaskManager';
import { getPostEngine } from './PostEngine';
/** /**
* Tag data stored in the database * Tag data stored in the database
@@ -379,6 +380,9 @@ export class TagEngine extends EventEmitter {
}) })
.where(eq(posts.id, postId)); .where(eq(posts.id, postId));
// Sync published post's file with updated tags
await getPostEngine().syncPublishedPostFile(postId);
updated++; updated++;
onProgress((updated / total) * 80, `Updated ${updated}/${total} posts...`); onProgress((updated / total) * 80, `Updated ${updated}/${total} posts...`);
} }
@@ -486,6 +490,9 @@ export class TagEngine extends EventEmitter {
}) })
.where(eq(posts.id, postId)); .where(eq(posts.id, postId));
// Sync published post's file with updated tags
await getPostEngine().syncPublishedPostFile(postId);
totalPostsUpdated++; totalPostsUpdated++;
} }
} }
@@ -601,6 +608,9 @@ export class TagEngine extends EventEmitter {
}) })
.where(eq(posts.id, postId)); .where(eq(posts.id, postId));
// Sync published post's file with updated tags
await getPostEngine().syncPublishedPostFile(postId);
updated++; updated++;
onProgress((updated / total) * 80, `Updated ${updated}/${total} posts...`); onProgress((updated / total) * 80, `Updated ${updated}/${total} posts...`);
} }

View File

@@ -123,6 +123,13 @@ vi.mock('../../src/main/engine/TaskManager', () => ({
}, },
})); }));
// Mock PostEngine - only mock the syncPublishedPostFile method used by TagEngine
vi.mock('../../src/main/engine/PostEngine', () => ({
getPostEngine: vi.fn(() => ({
syncPublishedPostFile: vi.fn(async () => true),
})),
}));
describe('TagEngine', () => { describe('TagEngine', () => {
let tagEngine: TagEngine; let tagEngine: TagEngine;