fix: remove node after collection removed (#1095)
This commit is contained in:
parent
f8f9b8cc9f
commit
c5fcc62bed
@ -15,6 +15,28 @@ pgOnly()('collection inherits', () => {
|
|||||||
await db.close();
|
await db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should remove Node after collection destroy', async () => {
|
||||||
|
const table1 = db.collection({
|
||||||
|
name: 'table1',
|
||||||
|
fields: [{ type: 'string', name: 'name' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
db.collection({
|
||||||
|
name: 'table2',
|
||||||
|
fields: [{ type: 'string', name: 'integer' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const collection3 = db.collection({
|
||||||
|
name: 'table3',
|
||||||
|
inherits: ['table1', 'table2'],
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.removeCollection(collection3.name);
|
||||||
|
|
||||||
|
expect(db.inheritanceMap.getNode('table3')).toBeUndefined();
|
||||||
|
expect(table1.isParent()).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
it('can update relation with child table', async () => {
|
it('can update relation with child table', async () => {
|
||||||
const A = db.collection({
|
const A = db.collection({
|
||||||
name: 'a',
|
name: 'a',
|
||||||
|
@ -262,6 +262,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|||||||
transaction: options.transaction,
|
transaction: options.transaction,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on('afterRemoveCollection', (collection) => {
|
||||||
|
this.inheritanceMap.removeNode(collection.name);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addMigration(item: MigrationItem) {
|
addMigration(item: MigrationItem) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import lodash from 'lodash';
|
import lodash from 'lodash';
|
||||||
|
import Database from './database';
|
||||||
|
|
||||||
class TableNode {
|
class TableNode {
|
||||||
name: string;
|
name: string;
|
||||||
@ -14,6 +15,21 @@ class TableNode {
|
|||||||
export default class InheritanceMap {
|
export default class InheritanceMap {
|
||||||
nodes: Map<string, TableNode> = new Map<string, TableNode>();
|
nodes: Map<string, TableNode> = new Map<string, TableNode>();
|
||||||
|
|
||||||
|
removeNode(name: string) {
|
||||||
|
const node = this.nodes.get(name);
|
||||||
|
if (!node) return;
|
||||||
|
|
||||||
|
for (const parent of node.parents) {
|
||||||
|
parent.children.delete(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const child of node.children) {
|
||||||
|
child.parents.delete(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.nodes.delete(name);
|
||||||
|
}
|
||||||
|
|
||||||
getOrCreateNode(name: string) {
|
getOrCreateNode(name: string) {
|
||||||
if (!this.nodes.has(name)) {
|
if (!this.nodes.has(name)) {
|
||||||
this.nodes.set(name, new TableNode(name));
|
this.nodes.set(name, new TableNode(name));
|
||||||
|
Loading…
Reference in New Issue
Block a user