Skip to content

Commit

Permalink
🗃️ refactor: add db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 10, 2024
1 parent d737afe commit 6ceb818
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
12 changes: 12 additions & 0 deletions src/database/core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DB_Session } from '@/database/schemas/session';
import { DB_SessionGroup } from '@/database/schemas/sessionGroup';
import { DB_Topic } from '@/database/schemas/topic';
import { DB_User } from '@/database/schemas/user';
import { MigrationLLMSettings } from '@/migrations/FromV3ToV4';
import { uuid } from '@/utils/uuid';

import { migrateSettingsToUser } from './migrations/migrateSettingsToUser';
Expand Down Expand Up @@ -62,6 +63,10 @@ export class LocalDB extends Dexie {
.stores(dbSchemaV7)
.upgrade((trans) => this.upgradeToV7(trans));

this.version(8)
.stores(dbSchemaV7)
.upgrade((trans) => this.upgradeToV8(trans));

this.files = this.table('files');
this.sessions = this.table('sessions');
this.messages = this.table('messages');
Expand Down Expand Up @@ -139,6 +144,13 @@ export class LocalDB extends Dexie {
plugin.id = plugin.identifier;
});
};

upgradeToV8 = async (trans: Transaction) => {
const users = trans.table('users');
users.toCollection().modify((user: DB_User) => {
user.settings = MigrationLLMSettings.migrateSettings(user.settings as any);
});
};
}

export const LocalDBInstance = new LocalDB();
Expand Down
26 changes: 1 addition & 25 deletions src/database/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@ import { z } from 'zod';
import { AgentSchema } from '@/database/schemas/session';
import { LobeMetaDataSchema } from '@/types/meta';

const modelProviderSchema = z.object({
openai: z.object({
OPENAI_API_KEY: z.string().optional(),
azureApiVersion: z.string().optional(),
customModelName: z.string().optional(),
endpoint: z.string().optional(),
models: z.array(z.string()).optional(),
useAzure: z.boolean().optional(),
}),
// zhipu: z.object({
// ZHIPU_API_KEY: z.string().optional(),
// enabled: z.boolean().default(false),
// }),
});

const settingsSchema = z.object({
defaultAgent: z.object({
config: AgentSchema,
meta: LobeMetaDataSchema,
}),
fontSize: z.number().default(14),
language: z.string(),
languageModel: modelProviderSchema.partial(),
languageModel: z.any().optional(),
password: z.string(),
themeMode: z.string(),
tts: z.object({
Expand All @@ -38,19 +23,10 @@ const settingsSchema = z.object({
}),
});

// const patchSchema = z.array(
// z.object({
// op: z.string(),
// path: z.string(),
// value: z.any(),
// }),
// );

export const DB_UserSchema = z.object({
avatar: z.string().optional(),
settings: settingsSchema.partial(),
uuid: z.string(),
// settings: patchSchema,
});

export type DB_User = z.infer<typeof DB_UserSchema>;
Expand Down
2 changes: 2 additions & 0 deletions src/migrations/FromV3ToV4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ export class MigrationV3ToV4 implements Migration {
};
};
}

export const MigrationLLMSettings = MigrationV3ToV4;

0 comments on commit 6ceb818

Please sign in to comment.