@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
57 lines (56 loc) • 2.08 kB
JavaScript
import { createInspector } from '@directus/schema';
import { useLogger } from '../../logger/index.js';
import { getDatabaseClient } from '../index.js';
export async function up(knex) {
const isMysql = getDatabaseClient(knex) === 'mysql';
if (isMysql) {
await dropConstraint(knex);
}
await knex.schema.alterTable('directus_shares', (table) => {
table.dropNullable('collection');
table.dropNullable('item');
});
if (isMysql) {
await recreateConstraint(knex);
}
}
export async function down(knex) {
const isMysql = getDatabaseClient(knex) === 'mysql';
if (isMysql) {
await dropConstraint(knex);
}
await knex.schema.alterTable('directus_shares', (table) => {
table.setNullable('collection');
table.setNullable('item');
});
if (isMysql) {
await recreateConstraint(knex);
}
}
/**
* Temporarily drop foreign key constraint for MySQL instances, see https://github.com/directus/directus/issues/19399
*/
async function dropConstraint(knex) {
const logger = useLogger();
const inspector = createInspector(knex);
const foreignKeys = await inspector.foreignKeys('directus_shares');
const collectionForeignKeys = foreignKeys.filter((fk) => fk.column === 'collection');
const constraintName = collectionForeignKeys[0]?.constraint_name;
if (constraintName && collectionForeignKeys.length === 1) {
await knex.schema.alterTable('directus_shares', (table) => {
table.dropForeign('collection', constraintName);
});
}
else {
logger.warn(`Unexpected number of foreign key constraints on 'directus_shares.collection':`);
logger.warn(JSON.stringify(collectionForeignKeys, null, 4));
}
}
/**
* Recreate foreign key constraint for MySQL instances, from 20211211A-add-shares.ts
*/
async function recreateConstraint(knex) {
return knex.schema.alterTable('directus_shares', async (table) => {
table.foreign('collection').references('directus_collections.collection').onDelete('CASCADE');
});
}