Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): classify missing applied migrations

nothingyuancando88d36fb43dc7
diff --git a/apps/desktop/src/main/localDb/__tests__/migrationCompatibility.test.ts b/apps/desktop/src/main/localDb/__tests__/migrationCompatibility.test.ts
index d7c6ec4ba3..51a4a8cb50 100644
--- a/apps/desktop/src/main/localDb/__tests__/migrationCompatibility.test.ts
+++ b/apps/desktop/src/main/localDb/__tests__/migrationCompatibility.test.ts
@@ -7,6 +7,7 @@ import { afterEach, describe, expect, it } from 'vitest';
import {
checkMigrationCompatibility,
hashMigrationFile,
+ MigrationRuntimeManifestError,
prepareMigrationRuntimeManifest,
} from '../migrationRunner';
@@ -212,6 +213,26 @@ describe('checkMigrationCompatibility', () => {
);
});
+ it('classifies an application missing an applied migration without weakening the guard', () => {
+ const drizzleDir = createDrizzleDir();
+ const dbFilePath = path.join(drizzleDir, 'shared.db');
+ prepareMigrationRuntimeManifest(dbFilePath, drizzleDir, 1);
+ rmSync(path.join(drizzleDir, '0001_second.sql'));
+
+ try {
+ prepareMigrationRuntimeManifest(dbFilePath, drizzleDir, 1);
+ throw new Error('expected a missing applied migration failure');
+ } catch (error) {
+ expect(error).toBeInstanceOf(MigrationRuntimeManifestError);
+ expect(error).toMatchObject({
+ code: 'applied-migration-missing-from-application',
+ seq: 1,
+ fileName: '0001_second.sql',
+ });
+ expect((error as Error).message).toContain('current application is missing applied migration');
+ }
+ });
+
it('normalizes the known bad 0062 companion identity back to canonical', () => {
const sourceDrizzleDir = path.resolve(__dirname, '../../../../drizzle');
const drizzleDir = mkdtempSync(path.join(tmpdir(), 'cindy-runtime-identity-repair-'));
diff --git a/apps/desktop/src/main/localDb/migrationRunner.ts b/apps/desktop/src/main/localDb/migrationRunner.ts
index 2a5f24fd78..0422b3e399 100644
--- a/apps/desktop/src/main/localDb/migrationRunner.ts
+++ b/apps/desktop/src/main/localDb/migrationRunner.ts
@@ -46,6 +46,29 @@ export interface MigrationRuntimeManifest {
migrations: MigrationRuntimeIdentity[];
}
+export type MigrationRuntimeManifestErrorCode =
+ | 'applied-migration-missing-from-application';
+
+/**
+ * A stable, structured failure for callers that need to distinguish an older
+ * application package from migration identity drift. Both cases remain
+ * fail-closed: old code must never open a newer local schema.
+ */
+export class MigrationRuntimeManifestError extends Error {
+ readonly name = 'MigrationRuntimeManifestError';
+
+ constructor(
+ readonly code: MigrationRuntimeManifestErrorCode,
+ readonly seq: number,
+ readonly fileName: string,
+ ) {
+ super(
+ `current application is missing applied migration at seq ${seq} (${fileName}); `
+ + 'the local database was upgraded by a newer application version',
+ );
+ }
+}
+
export type MigrationCompatibilityIssue =
| {
kind: 'schema-version-behind' | 'schema-version-ahead';
@@ -233,7 +256,14 @@ export function prepareMigrationRuntimeManifest(
for (const identity of existing.migrations) {
if (identity.seq > databaseVersion) continue;
const current = expectedBySeq.get(identity.seq);
- if (!current || !runtimeIdentityMatches(identity, current)) {
+ if (!current) {
+ throw new MigrationRuntimeManifestError(
+ 'applied-migration-missing-from-application',
+ identity.seq,
+ identity.fileName,
+ );
+ }
+ if (!runtimeIdentityMatches(identity, current)) {
throw new Error(
`applied migration runtime identity changed at seq ${identity.seq} (${identity.fileName})`,
);