-
Notifications
You must be signed in to change notification settings - Fork 3
/
1722517212441-nested-product-categories.ts
82 lines (77 loc) · 2.77 KB
/
1722517212441-nested-product-categories.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* @module
* @hidden
*/
import { MigrationInterface, QueryRunner, Table, TableColumn, TableForeignKey } from 'typeorm';
export class NestedProductCategories1722517212441 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn('product_category', new TableColumn({
name: 'parentId',
type: 'integer',
isNullable: true,
}));
await queryRunner.createForeignKey('product_category', new TableForeignKey({
columnNames: ['parentId'],
referencedColumnNames: ['id'],
referencedTableName: 'product_category',
}));
await queryRunner.createTable(new Table({
name: 'product_category_closure',
columns: [{
name: 'id_ancestor',
type: 'integer',
isNullable: false,
isPrimary: true,
}, {
name: 'id_descendant',
type: 'integer',
isNullable: false,
isPrimary: true,
}],
}));
await queryRunner.createForeignKeys('product_category_closure', [
new TableForeignKey({
columnNames: ['id_ancestor'],
referencedColumnNames: ['id'],
referencedTableName: 'product_category',
onDelete: 'CASCADE',
}),
new TableForeignKey({
columnNames: ['id_descendant'],
referencedColumnNames: ['id'],
referencedTableName: 'product_category',
onDelete: 'CASCADE',
}),
]);
await queryRunner.query(`
INSERT INTO product_category_closure (id_ancestor, id_descendant)
SELECT id, id FROM product_category
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
const categoryTable = await queryRunner.getTable('product_category');
const parentForeignKeys = categoryTable.foreignKeys.filter((fk) => fk.columnNames.includes('parentId'));
await queryRunner.dropForeignKeys(categoryTable, parentForeignKeys);
await queryRunner.dropColumn(categoryTable, 'parentId');
await queryRunner.dropTable('product_category_closure');
}
}