-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-postgres-migration.js
More file actions
42 lines (35 loc) · 1.22 KB
/
create-postgres-migration.js
File metadata and controls
42 lines (35 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { exec } = require('child_process');
const readline = require('readline');
const path = require('path');
const fs = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the name for the new migration: ', (migrationName) => {
const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, '');
const migrationFolderName = `${timestamp}_${migrationName}`;
const migrationsDir = path.join(
__dirname,
'prisma_postgres/migrations',
migrationFolderName,
);
// Create the migration folder
if (!fs.existsSync(migrationsDir)) {
fs.mkdirSync(migrationsDir, { recursive: true });
}
const migrationFilePath = path.join(migrationsDir, 'migration.sql');
const command = `npx prisma migrate diff --from-schema-datasource ./prisma_postgres/schema.prisma --to-schema-datamodel ./prisma_postgres/schema.prisma --script > ${migrationFilePath}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Migration script created: ${migrationFilePath}`);
});
rl.close();
});