mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Improve error reporting on CLI bootstrap command (#6977)
* Show full DB error on bootstrap connection issue * Show full DB error on bootstrap
This commit is contained in:
@@ -5,19 +5,16 @@ import env from '../../../env';
|
||||
import logger from '../../../logger';
|
||||
import { getSchema } from '../../../utils/get-schema';
|
||||
import { RolesService, UsersService, SettingsService } from '../../../services';
|
||||
import getDatabase, { isInstalled, hasDatabaseConnection } from '../../../database';
|
||||
import getDatabase, { isInstalled, hasDatabaseConnection, validateDBConnection } from '../../../database';
|
||||
import { SchemaOverview } from '../../../types';
|
||||
|
||||
export default async function bootstrap({ skipAdminInit }: { skipAdminInit?: boolean }): Promise<void> {
|
||||
logger.info('Initializing bootstrap...');
|
||||
|
||||
if ((await isDatabaseAvailable()) === false) {
|
||||
logger.error(`Can't connect to the database`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const database = getDatabase();
|
||||
|
||||
await validateDBConnection(database);
|
||||
|
||||
if ((await isInstalled()) === false) {
|
||||
logger.info('Installing Directus system tables...');
|
||||
|
||||
@@ -48,21 +45,6 @@ export default async function bootstrap({ skipAdminInit }: { skipAdminInit?: boo
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
async function isDatabaseAvailable() {
|
||||
const tries = 5;
|
||||
const secondsBetweenTries = 5;
|
||||
|
||||
for (let i = 0; i < tries; i++) {
|
||||
if (await hasDatabaseConnection()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, secondsBetweenTries * 1000));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async function createDefaultAdmin(schema: SchemaOverview) {
|
||||
logger.info('Setting up first admin role...');
|
||||
const rolesService = new RolesService({ schema });
|
||||
|
||||
@@ -94,8 +94,8 @@ export function getSchemaInspector(): ReturnType<typeof SchemaInspector> {
|
||||
return inspector;
|
||||
}
|
||||
|
||||
export async function hasDatabaseConnection(): Promise<boolean> {
|
||||
const database = getDatabase();
|
||||
export async function hasDatabaseConnection(database?: Knex): Promise<boolean> {
|
||||
database = database ?? getDatabase();
|
||||
|
||||
try {
|
||||
if (env.DB_CLIENT === 'oracledb') {
|
||||
@@ -103,15 +103,22 @@ export async function hasDatabaseConnection(): Promise<boolean> {
|
||||
} else {
|
||||
await database.raw('SELECT 1');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateDBConnection(): Promise<void> {
|
||||
export async function validateDBConnection(database?: Knex): Promise<void> {
|
||||
database = database ?? getDatabase();
|
||||
|
||||
try {
|
||||
await hasDatabaseConnection();
|
||||
if (env.DB_CLIENT === 'oracledb') {
|
||||
await database.raw('select 1 from DUAL');
|
||||
} else {
|
||||
await database.raw('SELECT 1');
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Can't connect to the database.`);
|
||||
logger.error(error);
|
||||
|
||||
Reference in New Issue
Block a user