-
-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathmigrations.ts
More file actions
248 lines (217 loc) · 7.5 KB
/
Copy pathmigrations.ts
File metadata and controls
248 lines (217 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import type Database from "better-sqlite3";
interface Migration {
version: number;
name: string;
up(sqlite: Database.Database): void;
}
const migrations: Migration[] = [
{
version: 1,
name: "workspace-state",
up: migrateWorkspaceState,
},
{
version: 2,
name: "oauth-state",
up: migrateOAuthState,
},
{
version: 3,
name: "local-agent-sessions",
up: migrateLocalAgentSessions,
},
{
version: 4,
name: "workspace-conversation-bindings",
up: migrateWorkspaceConversationBindings,
},
{
version: 5,
name: "local-agent-structured-errors",
up: migrateLocalAgentStructuredErrors,
},
{
version: 6,
name: "local-agent-effort-rename",
up: migrateLocalAgentEffortRename,
},
];
export function migrateDatabase(sqlite: Database.Database): void {
const migrate = sqlite.transaction(() => {
sqlite.exec(`
create table if not exists devspace_schema_migrations (
version integer primary key,
name text not null,
applied_at text not null
);
`);
const applied = new Set(
(
sqlite.prepare("select version from devspace_schema_migrations").all() as Array<{
version: number;
}>
).map((row) => row.version),
);
const recordMigration = sqlite.prepare(
"insert into devspace_schema_migrations (version, name, applied_at) values (?, ?, ?)",
);
for (const migration of migrations) {
if (applied.has(migration.version)) continue;
migration.up(sqlite);
recordMigration.run(migration.version, migration.name, new Date().toISOString());
}
});
migrate.immediate();
}
function migrateWorkspaceState(sqlite: Database.Database): void {
sqlite.exec(`
create table if not exists workspace_sessions (
id text primary key,
root text not null,
status text not null default 'active',
mode text not null default 'checkout',
source_root text,
base_ref text,
base_sha text,
managed text not null default 'false',
created_at text not null,
last_used_at text not null
);
create index if not exists workspace_sessions_root_idx
on workspace_sessions(root, last_used_at desc);
create index if not exists workspace_sessions_status_idx
on workspace_sessions(status, last_used_at desc);
create table if not exists loaded_agent_files (
workspace_session_id text not null,
path text not null,
content_hash text not null,
content text not null,
loaded_at text not null,
last_seen_at text not null,
primary key (workspace_session_id, path),
foreign key (workspace_session_id)
references workspace_sessions(id)
on delete cascade
);
create index if not exists loaded_agent_files_path_idx
on loaded_agent_files(path);
`);
addColumnIfMissing(sqlite, "workspace_sessions", "mode", "text not null default 'checkout'");
addColumnIfMissing(sqlite, "workspace_sessions", "source_root", "text");
addColumnIfMissing(sqlite, "workspace_sessions", "base_ref", "text");
addColumnIfMissing(sqlite, "workspace_sessions", "base_sha", "text");
addColumnIfMissing(sqlite, "workspace_sessions", "managed", "text not null default 'false'");
}
function migrateOAuthState(sqlite: Database.Database): void {
sqlite.exec(`
create table if not exists oauth_clients (
client_id text primary key,
client_json text not null,
issued_at integer not null
);
create index if not exists oauth_clients_issued_at_idx
on oauth_clients(issued_at desc);
create table if not exists oauth_access_tokens (
token_hash text primary key,
client_id text not null,
scopes_json text not null,
expires_at integer not null,
resource text,
foreign key (client_id) references oauth_clients(client_id) on delete cascade
);
create index if not exists oauth_access_tokens_client_id_idx
on oauth_access_tokens(client_id);
create index if not exists oauth_access_tokens_expires_at_idx
on oauth_access_tokens(expires_at);
create table if not exists oauth_refresh_tokens (
token_hash text primary key,
client_id text not null,
scopes_json text not null,
expires_at integer not null,
resource text,
foreign key (client_id) references oauth_clients(client_id) on delete cascade
);
create index if not exists oauth_refresh_tokens_client_id_idx
on oauth_refresh_tokens(client_id);
create index if not exists oauth_refresh_tokens_expires_at_idx
on oauth_refresh_tokens(expires_at);
`);
}
function migrateLocalAgentSessions(sqlite: Database.Database): void {
sqlite.exec(`
create table if not exists local_agent_sessions (
id text primary key,
workspace_id text,
workspace_root text not null,
profile_name text not null,
provider text not null,
model text,
effort text,
provider_session_id text,
status text not null,
latest_response text,
error text,
created_at text not null,
updated_at text not null
);
create index if not exists local_agent_sessions_workspace_id_idx
on local_agent_sessions(workspace_id, updated_at desc);
create index if not exists local_agent_sessions_workspace_root_idx
on local_agent_sessions(workspace_root, updated_at desc);
create index if not exists local_agent_sessions_provider_session_id_idx
on local_agent_sessions(provider_session_id);
`);
addColumnIfMissing(sqlite, "local_agent_sessions", "effort", "text");
}
function migrateWorkspaceConversationBindings(sqlite: Database.Database): void {
sqlite.exec(`
create table if not exists workspace_conversation_bindings (
conversation_scope_id text not null,
target_key text not null,
workspace_session_id text not null,
created_at text not null,
last_used_at text not null,
primary key (conversation_scope_id, target_key),
foreign key (workspace_session_id)
references workspace_sessions(id)
on delete cascade
);
create index if not exists workspace_conversation_bindings_workspace_idx
on workspace_conversation_bindings(workspace_session_id);
`);
}
function migrateLocalAgentStructuredErrors(sqlite: Database.Database): void {
addColumnIfMissing(sqlite, "local_agent_sessions", "error_code", "text");
addColumnIfMissing(sqlite, "local_agent_sessions", "error_retryable", "text");
}
function migrateLocalAgentEffortRename(sqlite: Database.Database): void {
const columns = sqlite.prepare("pragma table_info(local_agent_sessions)").all() as Array<{
name: string;
}>;
const names = new Set(columns.map((column) => column.name));
if (names.has("effort")) {
if (names.has("thinking")) {
sqlite.exec(`
update local_agent_sessions
set effort = thinking
where effort is null and thinking is not null
`);
}
return;
}
if (!names.has("thinking")) {
addColumnIfMissing(sqlite, "local_agent_sessions", "effort", "text");
return;
}
sqlite.exec("alter table local_agent_sessions rename column thinking to effort");
}
function addColumnIfMissing(
sqlite: Database.Database,
table: "workspace_sessions" | "local_agent_sessions",
column: string,
definition: string,
): void {
const columns = sqlite.prepare(`pragma table_info(${table})`).all() as Array<{ name: string }>;
if (columns.some((existingColumn) => existingColumn.name === column)) return;
sqlite.exec(`alter table ${table} add column ${column} ${definition}`);
}