-
-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathmigrations.ts
More file actions
179 lines (154 loc) · 5.34 KB
/
Copy pathmigrations.ts
File metadata and controls
179 lines (154 loc) · 5.34 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
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: "workspace-goals",
up: migrateWorkspaceGoals,
},
];
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 migrateWorkspaceGoals(sqlite: Database.Database): void {
sqlite.exec(`
create table if not exists workspace_goals (
workspace_session_id text primary key,
goal_id text not null,
objective text not null,
status text not null check(status in ('active', 'blocked', 'complete')),
created_at text not null,
updated_at text not null,
completed_at text,
foreign key (workspace_session_id)
references workspace_sessions(id)
on delete cascade
);
create index if not exists workspace_goals_goal_id_idx
on workspace_goals(goal_id);
create index if not exists workspace_goals_status_idx
on workspace_goals(status, updated_at desc);
`);
}
function addColumnIfMissing(
sqlite: Database.Database,
table: "workspace_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}`);
}