schema.gen.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { Effect } from "effect"
  2. import type { DatabaseMigration } from "./migration"
  3. export default {
  4. up(tx) {
  5. return Effect.gen(function* () {
  6. yield* tx.run(`
  7. CREATE TABLE \`workspace\` (
  8. \`id\` text PRIMARY KEY,
  9. \`type\` text NOT NULL,
  10. \`name\` text DEFAULT '' NOT NULL,
  11. \`branch\` text,
  12. \`directory\` text,
  13. \`extra\` text,
  14. \`project_id\` text NOT NULL,
  15. \`time_used\` integer NOT NULL,
  16. CONSTRAINT \`fk_workspace_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
  17. );
  18. `)
  19. yield* tx.run(`
  20. CREATE TABLE \`data_migration\` (
  21. \`name\` text PRIMARY KEY,
  22. \`time_completed\` integer NOT NULL
  23. );
  24. `)
  25. yield* tx.run(`
  26. CREATE TABLE \`account_state\` (
  27. \`id\` integer PRIMARY KEY,
  28. \`active_account_id\` text,
  29. \`active_org_id\` text,
  30. CONSTRAINT \`fk_account_state_active_account_id_account_id_fk\` FOREIGN KEY (\`active_account_id\`) REFERENCES \`account\`(\`id\`) ON DELETE SET NULL
  31. );
  32. `)
  33. yield* tx.run(`
  34. CREATE TABLE \`account\` (
  35. \`id\` text PRIMARY KEY,
  36. \`email\` text NOT NULL,
  37. \`url\` text NOT NULL,
  38. \`access_token\` text NOT NULL,
  39. \`refresh_token\` text NOT NULL,
  40. \`token_expiry\` integer,
  41. \`time_created\` integer NOT NULL,
  42. \`time_updated\` integer NOT NULL
  43. );
  44. `)
  45. yield* tx.run(`
  46. CREATE TABLE \`control_account\` (
  47. \`email\` text NOT NULL,
  48. \`url\` text NOT NULL,
  49. \`access_token\` text NOT NULL,
  50. \`refresh_token\` text NOT NULL,
  51. \`token_expiry\` integer,
  52. \`active\` integer NOT NULL,
  53. \`time_created\` integer NOT NULL,
  54. \`time_updated\` integer NOT NULL,
  55. CONSTRAINT \`control_account_pk\` PRIMARY KEY(\`email\`, \`url\`)
  56. );
  57. `)
  58. yield* tx.run(`
  59. CREATE TABLE \`credential\` (
  60. \`id\` text PRIMARY KEY,
  61. \`integration_id\` text,
  62. \`label\` text NOT NULL,
  63. \`value\` text NOT NULL,
  64. \`connector_id\` text,
  65. \`method_id\` text,
  66. \`active\` integer,
  67. \`time_created\` integer NOT NULL,
  68. \`time_updated\` integer NOT NULL
  69. );
  70. `)
  71. yield* tx.run(`
  72. CREATE TABLE \`event_sequence\` (
  73. \`aggregate_id\` text PRIMARY KEY,
  74. \`seq\` integer NOT NULL,
  75. \`owner_id\` text
  76. );
  77. `)
  78. yield* tx.run(`
  79. CREATE TABLE \`event\` (
  80. \`id\` text PRIMARY KEY,
  81. \`aggregate_id\` text NOT NULL,
  82. \`seq\` integer NOT NULL,
  83. \`type\` text NOT NULL,
  84. \`data\` text NOT NULL,
  85. CONSTRAINT \`fk_event_aggregate_id_event_sequence_aggregate_id_fk\` FOREIGN KEY (\`aggregate_id\`) REFERENCES \`event_sequence\`(\`aggregate_id\`) ON DELETE CASCADE
  86. );
  87. `)
  88. yield* tx.run(`
  89. CREATE TABLE \`permission\` (
  90. \`id\` text PRIMARY KEY,
  91. \`project_id\` text NOT NULL,
  92. \`action\` text NOT NULL,
  93. \`resource\` text NOT NULL,
  94. \`time_created\` integer NOT NULL,
  95. \`time_updated\` integer NOT NULL,
  96. CONSTRAINT \`fk_permission_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
  97. );
  98. `)
  99. yield* tx.run(`
  100. CREATE TABLE \`project_directory\` (
  101. \`project_id\` text NOT NULL,
  102. \`directory\` text NOT NULL,
  103. \`type\` text,
  104. \`strategy\` text,
  105. \`time_created\` integer NOT NULL,
  106. CONSTRAINT \`project_directory_pk\` PRIMARY KEY(\`project_id\`, \`directory\`),
  107. CONSTRAINT \`fk_project_directory_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
  108. );
  109. `)
  110. yield* tx.run(`
  111. CREATE TABLE \`project\` (
  112. \`id\` text PRIMARY KEY,
  113. \`worktree\` text NOT NULL,
  114. \`vcs\` text,
  115. \`name\` text,
  116. \`icon_url\` text,
  117. \`icon_url_override\` text,
  118. \`icon_color\` text,
  119. \`time_created\` integer NOT NULL,
  120. \`time_updated\` integer NOT NULL,
  121. \`time_initialized\` integer,
  122. \`sandboxes\` text NOT NULL,
  123. \`commands\` text
  124. );
  125. `)
  126. yield* tx.run(`
  127. CREATE TABLE \`message\` (
  128. \`id\` text PRIMARY KEY,
  129. \`session_id\` text NOT NULL,
  130. \`time_created\` integer NOT NULL,
  131. \`time_updated\` integer NOT NULL,
  132. \`data\` text NOT NULL,
  133. CONSTRAINT \`fk_message_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  134. );
  135. `)
  136. yield* tx.run(`
  137. CREATE TABLE \`part\` (
  138. \`id\` text PRIMARY KEY,
  139. \`message_id\` text NOT NULL,
  140. \`session_id\` text NOT NULL,
  141. \`time_created\` integer NOT NULL,
  142. \`time_updated\` integer NOT NULL,
  143. \`data\` text NOT NULL,
  144. CONSTRAINT \`fk_part_message_id_message_id_fk\` FOREIGN KEY (\`message_id\`) REFERENCES \`message\`(\`id\`) ON DELETE CASCADE
  145. );
  146. `)
  147. yield* tx.run(`
  148. CREATE TABLE \`session_context_epoch\` (
  149. \`session_id\` text PRIMARY KEY,
  150. \`baseline\` text NOT NULL,
  151. \`snapshot\` text NOT NULL,
  152. \`baseline_seq\` integer NOT NULL,
  153. CONSTRAINT \`fk_session_context_epoch_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  154. );
  155. `)
  156. yield* tx.run(`
  157. CREATE TABLE \`session_input\` (
  158. \`id\` text PRIMARY KEY,
  159. \`session_id\` text NOT NULL,
  160. \`prompt\` text NOT NULL,
  161. \`delivery\` text NOT NULL,
  162. \`admitted_seq\` integer NOT NULL,
  163. \`promoted_seq\` integer,
  164. \`time_created\` integer NOT NULL,
  165. CONSTRAINT \`fk_session_input_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  166. );
  167. `)
  168. yield* tx.run(`
  169. CREATE TABLE \`session_message\` (
  170. \`id\` text PRIMARY KEY,
  171. \`session_id\` text NOT NULL,
  172. \`type\` text NOT NULL,
  173. \`seq\` integer NOT NULL,
  174. \`time_created\` integer NOT NULL,
  175. \`time_updated\` integer NOT NULL,
  176. \`data\` text NOT NULL,
  177. CONSTRAINT \`fk_session_message_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  178. );
  179. `)
  180. yield* tx.run(`
  181. CREATE TABLE \`session\` (
  182. \`id\` text PRIMARY KEY,
  183. \`project_id\` text NOT NULL,
  184. \`workspace_id\` text,
  185. \`parent_id\` text,
  186. \`slug\` text NOT NULL,
  187. \`directory\` text NOT NULL,
  188. \`path\` text,
  189. \`title\` text NOT NULL,
  190. \`version\` text NOT NULL,
  191. \`share_url\` text,
  192. \`summary_additions\` integer,
  193. \`summary_deletions\` integer,
  194. \`summary_files\` integer,
  195. \`summary_diffs\` text,
  196. \`metadata\` text,
  197. \`cost\` real DEFAULT 0 NOT NULL,
  198. \`tokens_input\` integer DEFAULT 0 NOT NULL,
  199. \`tokens_output\` integer DEFAULT 0 NOT NULL,
  200. \`tokens_reasoning\` integer DEFAULT 0 NOT NULL,
  201. \`tokens_cache_read\` integer DEFAULT 0 NOT NULL,
  202. \`tokens_cache_write\` integer DEFAULT 0 NOT NULL,
  203. \`revert\` text,
  204. \`permission\` text,
  205. \`agent\` text,
  206. \`model\` text,
  207. \`time_created\` integer NOT NULL,
  208. \`time_updated\` integer NOT NULL,
  209. \`time_compacting\` integer,
  210. \`time_archived\` integer,
  211. CONSTRAINT \`fk_session_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
  212. );
  213. `)
  214. yield* tx.run(`
  215. CREATE TABLE \`todo\` (
  216. \`session_id\` text NOT NULL,
  217. \`content\` text NOT NULL,
  218. \`status\` text NOT NULL,
  219. \`priority\` text NOT NULL,
  220. \`position\` integer NOT NULL,
  221. \`time_created\` integer NOT NULL,
  222. \`time_updated\` integer NOT NULL,
  223. CONSTRAINT \`todo_pk\` PRIMARY KEY(\`session_id\`, \`position\`),
  224. CONSTRAINT \`fk_todo_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  225. );
  226. `)
  227. yield* tx.run(`
  228. CREATE TABLE \`session_share\` (
  229. \`session_id\` text PRIMARY KEY,
  230. \`id\` text NOT NULL,
  231. \`secret\` text NOT NULL,
  232. \`url\` text NOT NULL,
  233. \`time_created\` integer NOT NULL,
  234. \`time_updated\` integer NOT NULL,
  235. CONSTRAINT \`fk_session_share_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
  236. );
  237. `)
  238. yield* tx.run(`CREATE UNIQUE INDEX \`event_aggregate_seq_idx\` ON \`event\` (\`aggregate_id\`,\`seq\`);`)
  239. yield* tx.run(`CREATE INDEX \`event_aggregate_type_seq_idx\` ON \`event\` (\`aggregate_id\`,\`type\`,\`seq\`);`)
  240. yield* tx.run(
  241. `CREATE UNIQUE INDEX \`permission_project_action_resource_idx\` ON \`permission\` (\`project_id\`,\`action\`,\`resource\`);`,
  242. )
  243. yield* tx.run(
  244. `CREATE INDEX \`message_session_time_created_id_idx\` ON \`message\` (\`session_id\`,\`time_created\`,\`id\`);`,
  245. )
  246. yield* tx.run(`CREATE INDEX \`part_message_id_id_idx\` ON \`part\` (\`message_id\`,\`id\`);`)
  247. yield* tx.run(`CREATE INDEX \`part_session_idx\` ON \`part\` (\`session_id\`);`)
  248. yield* tx.run(
  249. `CREATE INDEX \`session_input_session_pending_delivery_seq_idx\` ON \`session_input\` (\`session_id\`,\`promoted_seq\`,\`delivery\`,\`admitted_seq\`);`,
  250. )
  251. yield* tx.run(
  252. `CREATE UNIQUE INDEX \`session_input_session_admitted_seq_idx\` ON \`session_input\` (\`session_id\`,\`admitted_seq\`);`,
  253. )
  254. yield* tx.run(
  255. `CREATE UNIQUE INDEX \`session_input_session_promoted_seq_idx\` ON \`session_input\` (\`session_id\`,\`promoted_seq\`);`,
  256. )
  257. yield* tx.run(
  258. `CREATE UNIQUE INDEX \`session_message_session_seq_idx\` ON \`session_message\` (\`session_id\`,\`seq\`);`,
  259. )
  260. yield* tx.run(
  261. `CREATE INDEX \`session_message_session_type_seq_idx\` ON \`session_message\` (\`session_id\`,\`type\`,\`seq\`);`,
  262. )
  263. yield* tx.run(
  264. `CREATE INDEX \`session_message_session_time_created_id_idx\` ON \`session_message\` (\`session_id\`,\`time_created\`,\`id\`);`,
  265. )
  266. yield* tx.run(`CREATE INDEX \`session_message_time_created_idx\` ON \`session_message\` (\`time_created\`);`)
  267. yield* tx.run(`CREATE INDEX \`session_project_idx\` ON \`session\` (\`project_id\`);`)
  268. yield* tx.run(`CREATE INDEX \`session_workspace_idx\` ON \`session\` (\`workspace_id\`);`)
  269. yield* tx.run(`CREATE INDEX \`session_parent_idx\` ON \`session\` (\`parent_id\`);`)
  270. yield* tx.run(`CREATE INDEX \`todo_session_idx\` ON \`todo\` (\`session_id\`);`)
  271. })
  272. },
  273. } satisfies Omit<DatabaseMigration.Migration, "id">