-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
setup-database.js
83 lines (68 loc) · 2.4 KB
/
setup-database.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* eslint-disable no-console */
import { PrismaClient } from '@prisma/client';
import { execSync, spawnSync } from 'child_process';
const prisma = new PrismaClient();
function checkForNeededMigrations() {
const command = 'npx';
const args = [
'prisma', 'migrate', 'diff',
'--to-schema-datasource', './prisma/schema.prisma',
'--from-schema-datamodel', './prisma/schema.prisma',
'--exit-code'
];
const result = spawnSync(command, args, { encoding: 'utf-8' });
if (result.error) {
console.error('Failed to run command:', result.error);
return false;
}
// Handling the exit code
if (result.status === 0) {
console.log('No differences between DB and schema detected.');
return false;
} else if (result.status === 2) {
console.log('There are differences between the schemas.');
return true;
} else if (result.status === 1) {
console.log('An error occurred.');
return false;
}
}
/**
* This function checks if the database is in a state where the workaround is needed.
*
* The workaround is needed when the database is not empty and the _prisma_migrations
* table does not exist.
*/
async function shouldApplyWorkaround() {
const tables = await prisma.$queryRaw`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'`;
const databaseNotEmpty = tables.length > 0;
const migrationsTableExists = tables.some(table => table.table_name === '_prisma_migrations');
return !migrationsTableExists && databaseNotEmpty;
}
async function handleMigrations() {
try {
if (await shouldApplyWorkaround()) {
console.log('Workaround needed! Running: prisma migrate resolve --applied 0_init');
execSync('npx prisma migrate resolve --applied 0_init', { stdio: 'inherit' });
}
// Determine if there are any migrations to run, by comparing the local schema with the database schema using prisma migrate diff
const needsMigrations = checkForNeededMigrations();
if (needsMigrations) {
console.log('Migrations needed! Running: prisma migrate deploy');
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
} else {
console.log('No migrations needed.');
}
} catch (error) {
console.error('Error during migration process:', error);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
(async () => {
await handleMigrations();
})();