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