MCPcopy
hub / github.com/directus/directus / validateDatabaseCharset

Function validateDatabaseCharset

api/src/database/index.ts:345–389  ·  view source on GitHub ↗
(database?: Knex)

Source from the content-addressed store, hash-verified

343}
344
345async function validateDatabaseCharset(database?: Knex): Promise<void> {
346 const env = useEnv();
347 database = database ?? getDatabase();
348 const logger = useLogger();
349
350 if (getDatabaseClient(database) === 'mysql') {
351 const { collation } = await database.select(database.raw(`@@collation_database as collation`)).first();
352
353 const tables = await database('information_schema.tables')
354 .select({ name: 'TABLE_NAME', collation: 'TABLE_COLLATION' })
355 .where({ TABLE_SCHEMA: env['DB_DATABASE'] });
356
357 const columns = await database('information_schema.columns')
358 .select({ table_name: 'TABLE_NAME', name: 'COLUMN_NAME', collation: 'COLLATION_NAME' })
359 .where({ TABLE_SCHEMA: env['DB_DATABASE'] })
360 .whereNot({ COLLATION_NAME: collation });
361
362 const excludedTables: string[] = toArray(env['DB_EXCLUDE_TABLES']);
363
364 let inconsistencies = '';
365
366 for (const table of tables) {
367 if (excludedTables.includes(table.name)) continue;
368
369 const tableColumns = columns.filter((column) => column.table_name === table.name);
370 const tableHasInvalidCollation = table.collation !== collation;
371
372 if (tableHasInvalidCollation || tableColumns.length > 0) {
373 inconsistencies += `\t\t- Table "${table.name}": "${table.collation}"\n`;
374
375 for (const column of tableColumns) {
376 inconsistencies += `\t\t - Column "${column.name}": "${column.collation}"\n`;
377 }
378 }
379 }
380
381 if (inconsistencies) {
382 logger.warn(
383 `Some tables and columns do not match your database's default collation (${collation}):\n${inconsistencies}`,
384 );
385 }
386 }
387
388 return;
389}

Callers 1

getDatabaseFunction · 0.85

Calls 6

useEnvFunction · 0.90
getDatabaseFunction · 0.85
useLoggerFunction · 0.85
databaseFunction · 0.85
toArrayFunction · 0.85
getDatabaseClientFunction · 0.70

Tested by

no test coverage detected