Skip to content

Commit

Permalink
Update clone.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
grantfitzsimmons committed Oct 30, 2024
1 parent b54bc72 commit 48ed463
Showing 1 changed file with 60 additions and 56 deletions.
116 changes: 60 additions & 56 deletions app/pages/api/databases/[name]/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -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,
});
}
}

0 comments on commit 48ed463

Please sign in to comment.