0001_api_key_profiles.sql 2.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. PRAGMA foreign_keys=OFF;--> statement-breakpoint
  2. CREATE TABLE `__new_sessions` (
  3. `id` text PRIMARY KEY NOT NULL,
  4. `source` text NOT NULL,
  5. `profile_id` text,
  6. `provider` text NOT NULL,
  7. `model` text NOT NULL,
  8. `permission_mode` text,
  9. `system_prompt_append` text DEFAULT '' NOT NULL,
  10. `provider_session_id` text,
  11. `workspace_dir` text NOT NULL,
  12. `title` text DEFAULT '' NOT NULL,
  13. `last_seq` integer DEFAULT 0 NOT NULL,
  14. `archived` integer DEFAULT false NOT NULL,
  15. `created_at` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) NOT NULL,
  16. `updated_at` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) NOT NULL
  17. );
  18. --> statement-breakpoint
  19. INSERT INTO `__new_sessions`("id", "source", "profile_id", "provider", "model", "permission_mode", "system_prompt_append", "provider_session_id", "workspace_dir", "title", "last_seq", "archived", "created_at", "updated_at") SELECT "id", "source", "profile_id", "provider", "model", "permission_mode", "system_prompt_append", "provider_session_id", "workspace_dir", "title", "last_seq", "archived", "created_at", "updated_at" FROM `sessions`;--> statement-breakpoint
  20. DROP TABLE `sessions`;--> statement-breakpoint
  21. ALTER TABLE `__new_sessions` RENAME TO `sessions`;--> statement-breakpoint
  22. PRAGMA foreign_keys=ON;--> statement-breakpoint
  23. CREATE INDEX `sessions_source_idx` ON `sessions` (`source`,`updated_at`);--> statement-breakpoint
  24. ALTER TABLE `api_keys` ADD `allowed_profile_ids` text DEFAULT '[]' NOT NULL;--> statement-breakpoint
  25. ALTER TABLE `api_keys` ADD `default_profile_id` text;--> statement-breakpoint
  26. ALTER TABLE `profiles` ADD `builtin` integer DEFAULT false NOT NULL;--> statement-breakpoint
  27. -- 数据迁移:自带的"通用助手"标记为内置(不可删除)
  28. UPDATE `profiles` SET `builtin` = 1 WHERE `id` = (SELECT `id` FROM `profiles` ORDER BY (`name` = '通用助手') DESC, `created_at` ASC LIMIT 1);--> statement-breakpoint
  29. -- 已有的 API Key 只开放内置 Profile,并设为默认
  30. UPDATE `api_keys` SET `allowed_profile_ids` = json_array((SELECT `id` FROM `profiles` WHERE `builtin` = 1)), `default_profile_id` = (SELECT `id` FROM `profiles` WHERE `builtin` = 1) WHERE EXISTS (SELECT 1 FROM `profiles` WHERE `builtin` = 1);--> statement-breakpoint
  31. -- 平台创建的会话:权限模式改为跟随 Profile
  32. UPDATE `sessions` SET `permission_mode` = NULL WHERE `source` <> 'admin';--> statement-breakpoint
  33. -- 后台会话:与所用 Profile 相同的权限模式也改为跟随 Profile
  34. UPDATE `sessions` SET `permission_mode` = NULL WHERE `permission_mode` = (SELECT `p`.`permission_mode` FROM `profiles` `p` WHERE `p`.`id` = `sessions`.`profile_id`);