Skip to content

Commit 21c09c1

Browse files
committed
feat: Ensure migration lock directory exists before writing lock file
1 parent 455eccb commit 21c09c1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

apps/api/src/migrations/migrations.service.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { glob } from 'glob'
33
import chalk from 'chalk'
44
import { ModuleRef } from '@nestjs/core'
55
import { startLoader, stopLoader } from './migration-loader.function'
6-
import { readFile, writeFile } from 'fs/promises'
7-
import { posix } from 'path'
6+
import { readFile, writeFile, mkdir } from 'fs/promises'
7+
import { posix, dirname } from 'path'
88
import { ConfigService } from '@nestjs/config'
99
import { Connection } from 'mongoose'
1010
import { InjectConnection } from '@nestjs/mongoose'
@@ -41,7 +41,8 @@ export class MigrationsService implements OnModuleInit {
4141
private readonly moduleRef: ModuleRef,
4242
private readonly config: ConfigService,
4343
) {
44-
this.lockLocation = posix.join(this.config.get('factorydrive.options.disks.local.config.root', '/tmp'), 'migrations.lock')
44+
const storageRoot = this.config.get<string>('factorydrive.options.disks.local.config.root', '/tmp')
45+
this.lockLocation = posix.join(storageRoot, 'migrations.lock')
4546
}
4647

4748
/**
@@ -93,13 +94,17 @@ export class MigrationsService implements OnModuleInit {
9394
if (dbMigration) {
9495
try {
9596
this.logger.warn(chalk.yellow('No migration lock file found. Creating one with the last migration timestamp...'))
97+
const lockDir = dirname(this.lockLocation)
98+
await mkdir(lockDir, { recursive: true })
9699
await writeFile(this.lockLocation, dbMigration.timestamp.toString())
97100
this.logger.log(chalk.green('Migration lock file created.'))
98101
} catch (error) {
99102
this.logger.error(chalk.red('Error while creating migration lock file !'))
100103
}
101104
} else {
102105
try {
106+
const lockDir = dirname(this.lockLocation)
107+
await mkdir(lockDir, { recursive: true })
103108
await writeFile(this.lockLocation, currentTimestamp.toString())
104109
this.logger.log(chalk.green('Migration lock file created.'))
105110
} catch (error) {
@@ -232,6 +237,10 @@ export class MigrationsService implements OnModuleInit {
232237
*/
233238
private async _writeMigrationLockFile(migrationKey: string, migrationTimestamp: string): Promise<void> {
234239
try {
240+
// Ensure the directory exists before writing the file
241+
const lockDir = dirname(this.lockLocation)
242+
await mkdir(lockDir, { recursive: true })
243+
235244
await writeFile(this.lockLocation, migrationTimestamp)
236245
await this.mongo.collection('migrations').insertOne({
237246
timestamp: parseInt(migrationTimestamp),

0 commit comments

Comments
 (0)