Skip to content

Commit b0acb77

Browse files
committed
fix: change query for check cascade
1 parent 058283c commit b0acb77

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

adminforth/dataConnectors/mysql.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,28 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
7474
}));
7575
}
7676

77+
private async hasPgCascadeFk(tableName: string): Promise<void> {
78+
const [fkResults] = await this.client.execute(
79+
`
80+
SELECT
81+
TABLE_NAME AS child_table,
82+
CONSTRAINT_NAME
83+
FROM information_schema.REFERENTIAL_CONSTRAINTS
84+
WHERE CONSTRAINT_SCHEMA = DATABASE()
85+
AND REFERENCED_TABLE_NAME = ?
86+
AND DELETE_RULE = 'CASCADE'
87+
`,
88+
[tableName]
89+
);
90+
91+
for (const fk of fkResults as any[]) {
92+
afLogger.warn(`The database has ON DELETE CASCADE, which may conflict with adminForth cascade deletion and upload logic. Please remove it.`);
93+
}
94+
}
95+
7796
async discoverFields(resource) {
7897
const [results] = await this.client.execute("SHOW COLUMNS FROM " + resource.table);
79-
const [fkResults] = await this.client.execute(`
80-
SELECT
81-
kcu.COLUMN_NAME,
82-
kcu.TABLE_NAME AS child_table,
83-
rc.DELETE_RULE
84-
FROM information_schema.KEY_COLUMN_USAGE AS kcu
85-
JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc
86-
ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
87-
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
88-
WHERE kcu.REFERENCED_TABLE_NAME = ?
89-
AND kcu.TABLE_SCHEMA = DATABASE()
90-
`, [resource.table]);
91-
92-
for (const fk of fkResults as any[]) {
93-
if (fk.DELETE_RULE?.toUpperCase() === 'CASCADE') {
94-
afLogger.warn(`The database has ON DELETE CASCADE, which may conflict with adminForth cascade deletion and upload logic. Please remove it.`);
95-
}
96-
}
98+
await this.hasPgCascadeFk(resource.table);
9799
const fieldTypes = {};
98100
results.forEach((row) => {
99101
const field: any = {};

adminforth/dataConnectors/postgres.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
7070
}
7171

7272
private async hasPgCascadeFk(tableName: string, schema = 'public'): Promise<boolean> {
73-
const res = await this.client.query(
74-
`
75-
SELECT 1
76-
FROM pg_constraint
77-
WHERE contype = 'f'
78-
AND confrelid = ($2 || '.' || $1)::regclass
79-
AND confdeltype = 'c'
80-
LIMIT 1
81-
`,
82-
[tableName, schema]
83-
);
84-
85-
return res.rowCount > 0;
86-
}
73+
const res = await this.client.query(
74+
`
75+
SELECT 1
76+
FROM pg_constraint
77+
WHERE contype = 'f'
78+
AND confrelid = ($2 || '.' || $1)::regclass
79+
AND confdeltype = 'c'
80+
LIMIT 1
81+
`,
82+
[tableName, schema]
83+
);
84+
85+
return res.rowCount > 0;
86+
}
8787

8888
async discoverFields(resource) {
8989

0 commit comments

Comments
 (0)