-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
502 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import * as Either from 'fp-ts/Either'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import * as TaskEither from 'fp-ts/TaskEither'; | ||
import { lock } from 'proper-lockfile'; | ||
import { isValidationErrorLike } from 'zod-validation-error'; | ||
|
||
import { | ||
AcquireLockError, | ||
InvalidMigrationHistoryLogError, | ||
MigrationHistoryLogWriteError, | ||
ReleaseLockError, | ||
} from '../../errors'; | ||
import { History } from '../../models'; | ||
import type { MigrationHistoryLog } from '../../ports'; | ||
import { FileSystemWriteError, writeFile } from '../../utils/fs'; | ||
import { makeReadHistory } from './readHistory'; | ||
import type { FileMigrationHistoryLogContext } from './types'; | ||
import { toMigrationHistoryLogWriteError } from './utils/toMigrationHistoryLogWriteError'; | ||
|
||
export function makeAcquireLock( | ||
ctx: FileMigrationHistoryLogContext | ||
): MigrationHistoryLog['acquireLock'] { | ||
const { filePath } = ctx; | ||
|
||
return function acquireLock() { | ||
return pipe( | ||
TaskEither.tryCatch( | ||
() => lock(filePath.toString()), | ||
(err) => | ||
new AcquireLockError( | ||
`Unable to acquire lock on "${filePath}"`, | ||
err instanceof Error ? { cause: err } : undefined | ||
) | ||
), | ||
TaskEither.bindTo('releaseLockAsync'), | ||
TaskEither.bindW('defaultValue', makeReadHistory(ctx)), | ||
TaskEither.map(({ defaultValue, releaseLockAsync }) => { | ||
let value = defaultValue; | ||
let isLockReleased = false; | ||
|
||
return { | ||
// use getter to prevent mutation | ||
get currentValue() { | ||
return value; | ||
}, | ||
persistHistory: (history) => | ||
pipe( | ||
// ensure lock has not been released | ||
isLockReleased, | ||
Either.fromPredicate( | ||
(flag) => flag === false, | ||
() => | ||
new MigrationHistoryLogWriteError( | ||
`Unable to write to migration history-log; lock has been released` | ||
) | ||
), | ||
// update history on disk | ||
Either.flatMap(() => History.serialize(history)), | ||
TaskEither.fromEither, | ||
TaskEither.flatMap((contents) => writeFile(filePath, contents)), | ||
// handle errors | ||
TaskEither.mapLeft((err) => { | ||
if (isValidationErrorLike(err)) { | ||
return new InvalidMigrationHistoryLogError(err.message, { | ||
cause: err, | ||
}); | ||
} | ||
|
||
if (err instanceof FileSystemWriteError) { | ||
return toMigrationHistoryLogWriteError(err); | ||
} | ||
|
||
return err; | ||
}), | ||
// update value with new history | ||
TaskEither.tap(() => { | ||
value = history; | ||
return TaskEither.of(void 0); | ||
}) | ||
), | ||
releaseLock: () => | ||
pipe( | ||
// ensure lock has not been released | ||
isLockReleased, | ||
TaskEither.fromPredicate( | ||
(flag) => flag === false, | ||
() => new ReleaseLockError(`Lock has already been released`) | ||
), | ||
// release lock | ||
TaskEither.flatMap(() => | ||
TaskEither.tryCatch( | ||
() => releaseLockAsync(), | ||
(err) => | ||
new ReleaseLockError( | ||
`Unable to release lock on "${filePath}"`, | ||
err instanceof Error ? { cause: err } : undefined | ||
) | ||
) | ||
), | ||
// set flag to true | ||
TaskEither.tap(() => { | ||
isLockReleased = true; | ||
return TaskEither.of(undefined); | ||
}) | ||
), | ||
}; | ||
}) | ||
); | ||
}; | ||
} |
42 changes: 0 additions & 42 deletions
42
lib/adapters/fileMigrationHistoryLog/addExecutedMigration.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 14 additions & 12 deletions
26
lib/adapters/fileMigrationHistoryLog/mock/history-log.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
[ | ||
{ | ||
"checksum": "f524ee4ad943b8312921906d9ef52b1b", | ||
"id": "20240101-one", | ||
"executedAt": "2024-01-01T00:00:00Z" | ||
}, | ||
{ | ||
"checksum": "8c8878ce2d3db81ed8bfdadfca8f5b6a", | ||
"id": "20240103-two", | ||
"executedAt": "2024-01-03T09:54:32Z" | ||
} | ||
] | ||
{ | ||
"entries": [ | ||
{ | ||
"checksum": "f524ee4ad943b8312921906d9ef52b1b", | ||
"id": "20240101-one", | ||
"executedAt": "2024-01-01T00:00:00Z" | ||
}, | ||
{ | ||
"checksum": "8c8878ce2d3db81ed8bfdadfca8f5b6a", | ||
"id": "20240103-two", | ||
"executedAt": "2024-01-03T09:54:32Z" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import type { PathLike } from 'fs'; | ||
|
||
export type FileMigrationHistoryLogContext = { | ||
filePath: string; | ||
filePath: PathLike; | ||
}; |
Oops, something went wrong.