| 20 | ]; |
| 21 | |
| 22 | export function migrateDatabase(sqlite: Database.Database): void { |
| 23 | const migrate = sqlite.transaction(() => { |
| 24 | sqlite.exec(` |
| 25 | create table if not exists devspace_schema_migrations ( |
| 26 | version integer primary key, |
| 27 | name text not null, |
| 28 | applied_at text not null |
| 29 | ); |
| 30 | `); |
| 31 | |
| 32 | const applied = new Set( |
| 33 | ( |
| 34 | sqlite.prepare("select version from devspace_schema_migrations").all() as Array<{ |
| 35 | version: number; |
| 36 | }> |
| 37 | ).map((row) => row.version), |
| 38 | ); |
| 39 | const recordMigration = sqlite.prepare( |
| 40 | "insert into devspace_schema_migrations (version, name, applied_at) values (?, ?, ?)", |
| 41 | ); |
| 42 | |
| 43 | for (const migration of migrations) { |
| 44 | if (applied.has(migration.version)) continue; |
| 45 | migration.up(sqlite); |
| 46 | recordMigration.run(migration.version, migration.name, new Date().toISOString()); |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | migrate.immediate(); |
| 51 | } |
| 52 | |
| 53 | function migrateWorkspaceState(sqlite: Database.Database): void { |
| 54 | sqlite.exec(` |