2023-07-27 19:48:40 +03:00
|
|
|
// check-db.ts
|
|
|
|
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
|
|
const schemaDatabaseExists = async (databaseName: string) => {
|
|
|
|
try {
|
|
|
|
const result = await prisma.$queryRawUnsafe<[any]>(
|
|
|
|
`SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return result.length > 0;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-16 04:41:10 +03:00
|
|
|
const main = async () => {
|
2023-07-27 19:48:40 +03:00
|
|
|
const databaseName = 'tests';
|
|
|
|
// Check if schema exists
|
|
|
|
const databaseExistsResult = await schemaDatabaseExists(databaseName);
|
|
|
|
|
|
|
|
if (!databaseExistsResult) {
|
|
|
|
throw new Error(`Schema ${databaseName} does not exist`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if database is initialized
|
|
|
|
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
|
2023-09-16 04:41:10 +03:00
|
|
|
};
|
2023-07-27 19:48:40 +03:00
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
process.exit(0);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
process.exit(1);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await prisma.$disconnect();
|
|
|
|
});
|