directus

Форк
0
/
20230721A-require-shares-fields.ts 
71 строка · 2.0 Кб
1
import { createInspector } from '@directus/schema';
2
import type { Knex } from 'knex';
3
import { useLogger } from '../../logger.js';
4
import { getHelpers } from '../helpers/index.js';
5

6
export async function up(knex: Knex): Promise<void> {
7
	const helper = getHelpers(knex).schema;
8
	const isMysql = helper.isOneOfClients(['mysql']);
9

10
	if (isMysql) {
11
		await dropConstraint(knex);
12
	}
13

14
	await knex.schema.alterTable('directus_shares', (table) => {
15
		table.dropNullable('collection');
16
		table.dropNullable('item');
17
	});
18

19
	if (isMysql) {
20
		await recreateConstraint(knex);
21
	}
22
}
23

24
export async function down(knex: Knex): Promise<void> {
25
	const helper = getHelpers(knex).schema;
26
	const isMysql = helper.isOneOfClients(['mysql']);
27

28
	if (isMysql) {
29
		await dropConstraint(knex);
30
	}
31

32
	await knex.schema.alterTable('directus_shares', (table) => {
33
		table.setNullable('collection');
34
		table.setNullable('item');
35
	});
36

37
	if (isMysql) {
38
		await recreateConstraint(knex);
39
	}
40
}
41

42
/**
43
 * Temporarily drop foreign key constraint for MySQL instances, see https://github.com/directus/directus/issues/19399
44
 */
45
async function dropConstraint(knex: Knex) {
46
	const logger = useLogger();
47

48
	const inspector = createInspector(knex);
49

50
	const foreignKeys = await inspector.foreignKeys('directus_shares');
51
	const collectionForeignKeys = foreignKeys.filter((fk) => fk.column === 'collection');
52
	const constraintName = collectionForeignKeys[0]?.constraint_name;
53

54
	if (constraintName && collectionForeignKeys.length === 1) {
55
		await knex.schema.alterTable('directus_shares', (table) => {
56
			table.dropForeign('collection', constraintName);
57
		});
58
	} else {
59
		logger.warn(`Unexpected number of foreign key constraints on 'directus_shares.collection':`);
60
		logger.warn(JSON.stringify(collectionForeignKeys, null, 4));
61
	}
62
}
63

64
/**
65
 * Recreate foreign key constraint for MySQL instances, from 20211211A-add-shares.ts
66
 */
67
async function recreateConstraint(knex: Knex) {
68
	return knex.schema.alterTable('directus_shares', async (table) => {
69
		table.foreign('collection').references('directus_collections.collection').onDelete('CASCADE');
70
	});
71
}
72

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.