( id: string, callback: () => void | Promise<void>, onAlreadyComplete?: () => void, )
| 298 | } |
| 299 | |
| 300 | export async function migrate( |
| 301 | id: string, |
| 302 | callback: () => void | Promise<void>, |
| 303 | onAlreadyComplete?: () => void, |
| 304 | ) { |
| 305 | if (process.env.NODE_ENV === "test") { |
| 306 | return await Promise.resolve(callback()); |
| 307 | } |
| 308 | |
| 309 | const migrationsPath = getMigrationsFolderPath(); |
| 310 | const migrationPath = path.join(migrationsPath, id); |
| 311 | |
| 312 | if (!fs.existsSync(migrationPath)) { |
| 313 | try { |
| 314 | console.log(`Running migration: ${id}`); |
| 315 | |
| 316 | fs.writeFileSync(migrationPath, ""); |
| 317 | await Promise.resolve(callback()); |
| 318 | } catch (e) { |
| 319 | console.warn(`Migration ${id} failed`, e); |
| 320 | } |
| 321 | } else if (onAlreadyComplete) { |
| 322 | onAlreadyComplete(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | export function getIndexSqlitePath(): string { |
| 327 | return path.join(getIndexFolderPath(), "index.sqlite"); |
no test coverage detected