diff --git a/app/pages/api/databases/[name]/clone.ts b/app/pages/api/databases/[name]/clone.ts index d15906c..f35b0d0 100644 --- a/app/pages/api/databases/[name]/clone.ts +++ b/app/pages/api/databases/[name]/clone.ts @@ -20,7 +20,11 @@ export default async function handler( await connectToDatabase(); - const databaseName = request.query.name; + const databaseName = Array.isArray(request.query.name) ? request.query.name[0] : request.query.name; + + if (!databaseName) { + return res.status(400).json({ error: 'Database name is required.' }); + } const timestamp = new Date().toISOString().replace(/T/, '_').replace(/:/g, '_').split('.')[0]; const newDatabaseName = `${databaseName}_${timestamp}`; @@ -43,66 +47,66 @@ export default async function handler( ); await new Promise((resolve, reject) => { - createDatabaseChild.on('exit', (code) => { - if (code !== 0) { - return reject(new Error(`Failed to create database: ${newDatabaseName}`)); - } - resolve(null); - }); + createDatabaseChild.on('exit', (code) => { + if (code !== 0) { + return reject(new Error(`Failed to create database: ${newDatabaseName}`)); + } + resolve(null); }); + }); -// Clone the current database into the new database -const cloneDatabaseChild = spawn( - 'mysqldump', - [ - `-u${process.env.MYSQL_USERNAME}`, - `-p${process.env.MYSQL_PASSWORD}`, - `-h${process.env.MYSQL_HOST}`, - '--databases', - databaseName, - '--no-create-db', - ], - { - stdio: ['pipe', 'pipe', 'pipe'], - shell: true, - } - ); + // Clone the current database into the new database + const cloneDatabaseChild = spawn( + 'mysqldump', + [ + `-u${process.env.MYSQL_USERNAME}`, + `-p${process.env.MYSQL_PASSWORD}`, + `-h${process.env.MYSQL_HOST}`, + '--databases', + databaseName, + '--no-create-db', + ], + { + stdio: ['pipe', 'pipe', 'pipe'], + shell: true, + } + ); - const importChild = spawn( - 'mysql', - [ - `-u${process.env.MYSQL_USERNAME}`, - `-p${process.env.MYSQL_PASSWORD}`, - `-h${process.env.MYSQL_HOST}`, - newDatabaseName, - ], - { - stdio: ['pipe', 'pipe', 'pipe'], - shell: true, - } - ); + const importChild = spawn( + 'mysql', + [ + `-u${process.env.MYSQL_USERNAME}`, + `-p${process.env.MYSQL_PASSWORD}`, + `-h${process.env.MYSQL_HOST}`, + newDatabaseName, + ], + { + stdio: ['pipe', 'pipe', 'pipe'], + shell: true, + } + ); - // Pipe the dump output to the new database - cloneDatabaseChild.stdout.pipe(importChild.stdin); - cloneDatabaseChild.stderr.on('data', (error) => { - throw new Error(error); - }); + // Pipe the dump output to the new database + cloneDatabaseChild.stdout.pipe(importChild.stdin); + cloneDatabaseChild.stderr.on('data', (error) => { + throw new Error(error); + }); - await new Promise((resolve, reject) => { - importChild.on('exit', (code) => { - if (code !== 0) { - return reject(new Error(`Failed to import data into database: ${newDatabaseName}`)); - } - resolve(null); + await new Promise((resolve, reject) => { + importChild.on('exit', (code) => { + if (code !== 0) { + return reject(new Error(`Failed to import data into database: ${newDatabaseName}`)); + } + resolve(null); + }); }); - }); - res.status(200).json({ - message: `Database cloned successfully: ${newDatabaseName}`, - }); -} catch (error) { - res.status(500).json({ - error: (error as Error).message, - }); -} + res.status(200).json({ + message: `Database cloned successfully: ${newDatabaseName}`, + }); + } catch (error) { + res.status(500).json({ + error: (error as Error).message, + }); + } } \ No newline at end of file