| 35 | FileSystem | Path | SqliteClient | Client.SqlClient | CommandExecutor | R2 |
| 36 | > = Migrator.make({ |
| 37 | dumpSchema(path, table) { |
| 38 | const dump = (args: Array<string>) => |
| 39 | Effect.gen(function*() { |
| 40 | const sql = yield* SqliteClient |
| 41 | const dump = yield* pipe( |
| 42 | Command.make("sqlite3", (sql as SqliteClient).config.filename, ...args), |
| 43 | Command.string |
| 44 | ) |
| 45 | return dump.replace(/^create table sqlite_sequence\(.*$/im, "") |
| 46 | .replace(/\n{2,}/gm, "\n\n") |
| 47 | .trim() |
| 48 | }).pipe( |
| 49 | Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message })) |
| 50 | ) |
| 51 | |
| 52 | const dumpSchema = dump([".schema"]) |
| 53 | |
| 54 | const dumpMigrations = dump([ |
| 55 | "--cmd", |
| 56 | `.mode insert ${table}`, |
| 57 | `select * from ${table}` |
| 58 | ]) |
| 59 | |
| 60 | const dumpAll = Effect.map( |
| 61 | Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }), |
| 62 | ([schema, migrations]) => schema + "\n\n" + migrations |
| 63 | ) |
| 64 | |
| 65 | const dumpFile = (file: string) => |
| 66 | Effect.gen(function*() { |
| 67 | const fs = yield* FileSystem |
| 68 | const path = yield* Path |
| 69 | const dump = yield* dumpAll |
| 70 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }) |
| 71 | yield* fs.writeFileString(file, dump) |
| 72 | }).pipe( |
| 73 | Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message })) |
| 74 | ) |
| 75 | |
| 76 | return dumpFile(path) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | /** |