@@ -3,8 +3,8 @@ import { glob } from 'glob'
33import chalk from 'chalk'
44import { ModuleRef } from '@nestjs/core'
55import { 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'
88import { ConfigService } from '@nestjs/config'
99import { Connection } from 'mongoose'
1010import { 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