Skip to content

Commit 282b9e2

Browse files
committed
Improve Migrator file URL docs and test
1 parent 3663a53 commit 282b9e2

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

‎packages/effect/src/unstable/sql/Migrator.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ export const fromRecord = (migrations: Record<string, Effect.Effect<void, unknow
401401
* files named `<id>_<name>.js`, `<id>_<name>.ts`,
402402
* `<id>_<name>.mjs`, or `<id>_<name>.mts`, and sorts migrations by id.
403403
*
404+
* **Details**
405+
*
406+
* Requires a `Path` service appropriate for the migration directory's path
407+
* syntax. On Windows, prefer a platform-aware implementation such as
408+
* `NodePath.layer`; the core `Path.layer` uses POSIX semantics and does not
409+
* preserve Windows drive-letter paths.
410+
*
404411
* @category loaders
405412
* @since 4.0.0
406413
*/

‎packages/effect/test/Migrator.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,16 @@ describe("Migrator", () => {
7070
it.effect("fromFileSystem imports migrations through the file URL the platform resolves", () =>
7171
Effect.gen(function*() {
7272
const posix = yield* Effect.provide(Path.Path, Path.layer)
73+
const migrationUrl = new URL("./fixtures/migrator/0001_first.js", import.meta.url)
74+
let resolvedPath: string | undefined
7375
// stand in for a Windows platform layer, which core has no implementation of
7476
const windowsLike = Layer.succeed(Path.Path)({
7577
...posix,
7678
join: (...segments: ReadonlyArray<string>) => segments.join("\\"),
77-
toFileUrl: (path: string) => Effect.succeed(new URL(`file:///${path.replaceAll("\\", "/")}`))
79+
toFileUrl: (path: string) => {
80+
resolvedPath = path
81+
return Effect.succeed(migrationUrl)
82+
}
7883
})
7984

8085
const migrations = yield* Migrator.fromFileSystem("C:\\migrations").pipe(
@@ -87,13 +92,10 @@ describe("Migrator", () => {
8792
// the loader is lazy, and `ResolvedMigration` declares a `SqlClient`
8893
// requirement this loader never uses
8994
const load = migrations[0]![2] as Effect.Effect<unknown, unknown>
90-
const specifier = yield* load.pipe(
91-
Effect.catchDefect((defect) => Effect.succeed(String(defect))),
92-
Effect.orElseSucceed(() => "")
93-
)
95+
const imported = yield* load
9496

95-
// the raw path would have been rejected as protocol "c:"
96-
assert.include(specifier, "file:///C:/migrations/0001_first.js")
97+
assert.strictEqual(resolvedPath, "C:\\migrations\\0001_first.js")
98+
assert.strictEqual((imported as { readonly marker: string }).marker, "loaded")
9799
}))
98100

99101
it.effect("fromFileSystem surfaces file URL failures as import errors", () =>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const marker = "loaded"

0 commit comments

Comments
 (0)