Skip to content

Commit ca5bf91

Browse files
committed
fix: change check for cascade and upload plugin
1 parent 778a90b commit ca5bf91

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,6 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
507507
return result;
508508
}
509509

510-
async checkCascadeWhenUploadPlugin(resource: AdminForthResource, config: AdminForthConfig) {
511-
const currentResource = config.resources.find(r => r.resourceId === resource.resourceId);
512-
if (!currentResource) return;
513-
const hasUploadPlugin = currentResource.plugins?.some(p => p.className === "UploadPlugin");
514-
515-
if (hasUploadPlugin) {
516-
const tableName = (resource.table);
517-
afLogger.warn(`Table "${tableName}" has ON DELETE CASCADE, which may conflict with adminForth UploadPlugin.`);
518-
}
519-
}
520-
521510
getRecordByPrimaryKey(resource: AdminForthResource, recordId: string): Promise<any> {
522511
return this.getRecordByPrimaryKeyWithOriginalTypes(resource, recordId).then((record) => {
523512
const newRecord = {};

adminforth/dataConnectors/mysql.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
8080
if (!cascadeColumn) return false;
8181

8282
const parentResource = config.resources.find(r => r.resourceId === cascadeColumn.foreignResource.resourceId);
83-
8483
if (!parentResource) return false;
8584

8685
const [rows] = await this.client.execute(
@@ -95,18 +94,16 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
9594
[parentResource.table]
9695
);
9796

98-
const hasCascade = (rows as any[]).length > 0;
97+
const hasCascadeOnTable = (rows as any[]).length > 0;
98+
99+
const isUploadPluginInstalled = resource.plugins?.some(p => p.className === "UploadPlugin");
99100

100-
if (hasCascade) {
101-
afLogger.warn(`Table "${parentResource.table}" has ON DELETE CASCADE, which may conflict with adminForth cascade deletion.`);
101+
if (hasCascadeOnTable && isUploadPluginInstalled) {
102+
afLogger.warn(`Table "${resource.table}" has ON DELETE CASCADE and UploadPlugin installed, which may conflict with adminForth cascade deletion`);
102103
}
103-
104-
return hasCascade;
105104
}
106105

107106
async discoverFields(resource: AdminForthResource, config: AdminForthConfig) {
108-
await this.checkCascadeWhenUploadPlugin(resource, config);
109-
110107
const [results] = await this.client.execute("SHOW COLUMNS FROM " + resource.table);
111108
await this.hasMySQLCascadeFk(resource, config);
112109
const fieldTypes = {};

adminforth/dataConnectors/postgres.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
7171

7272
async checkForeignResourceCascade(resource: AdminForthResource, config: AdminForthConfig, schema = 'public'): Promise<boolean> {
7373
const cascadeColumn = resource.columns.find(c => c.foreignResource?.onDelete === 'cascade');
74-
if (!cascadeColumn) return;
74+
if (!cascadeColumn) return;
7575

7676
const parentResource = config.resources.find(r => r.resourceId === cascadeColumn.foreignResource.resourceId);
77+
if (!parentResource) return;
78+
7779
const res = await this.client.query(
7880
`
7981
SELECT 1
@@ -86,19 +88,16 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
8688
[parentResource.table, schema]
8789
);
8890

89-
const hasCascade = res.rowCount > 0;
90-
if (hasCascade) {
91-
afLogger.warn(
92-
`Table "${parentResource.table}" has ON DELETE CASCADE, which may conflict with adminForth cascade deletion`
93-
);
94-
}
91+
const hasCascadeOnTable = res.rowCount > 0;
92+
const isUploadPluginInstalled = resource.plugins?.some(p => p.className === "UploadPlugin");
9593

96-
return hasCascade;
94+
if (hasCascadeOnTable && isUploadPluginInstalled) {
95+
afLogger.warn(`Table "${resource.table}" has ON DELETE CASCADE and installed upload plugin, which may conflict with adminForth cascade deletion`);
96+
}
9797
}
9898

9999
async discoverFields(resource: AdminForthResource, config: AdminForthConfig) {
100100
await this.checkForeignResourceCascade(resource, config);
101-
await this.checkCascadeWhenUploadPlugin(resource, config);
102101

103102
const tableName = resource.table;
104103
const stmt = await this.client.query(`

adminforth/dataConnectors/sqlite.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,34 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
3838
}));
3939
}
4040

41-
async hasSQLiteCascadeFk(resource: AdminForthResource, config: AdminForthConfig, fkMap: { [colName: string]: boolean }): Promise<boolean> {
41+
async hasSQLiteCascadeFk(resource: AdminForthResource, config: AdminForthConfig): Promise<boolean> {
42+
const cascadeColumn = resource.columns?.find(c => c.foreignResource?.onDelete === 'cascade');
43+
if (!cascadeColumn) return false;
4244

43-
const hasAdminCascade = resource.columns?.some(c => c.foreignResource?.onDelete === 'cascade');
44-
if (!hasAdminCascade) return false;
45+
const parentResource = config.resources.find(r => r.resourceId === cascadeColumn.foreignResource.resourceId);
46+
if (!parentResource) return false;
4547

46-
const hasDbCascade = Object.values(fkMap).some(v => v);
48+
const fkStmt = this.client.prepare(`PRAGMA foreign_key_list(${resource.table})`);
49+
const fkRows = await fkStmt.all();
50+
const fkMap: { [colName: string]: boolean } = {};
51+
fkRows.forEach(fk => { fkMap[fk.from] = fk.on_delete?.toUpperCase() === 'CASCADE'; });
4752

48-
if (hasDbCascade) {
49-
const tableName = (resource.table);
50-
afLogger.warn(
51-
52-
`Table "${tableName}" has ON DELETE CASCADE, which may conflict with adminForth cascade deletion`
53-
);
53+
const hasCascadeOnTable = fkMap[cascadeColumn.name] || false;
54+
const isUploadPluginInstalled = resource.plugins?.some(p => p.className === "UploadPlugin");
55+
56+
if (hasCascadeOnTable && isUploadPluginInstalled) {
57+
afLogger.warn(`Table "${resource.table}" has ON DELETE CASCADE and UploadPlugin installed, which may conflict with adminForth cascade deletion`);
5458
}
5559

56-
return hasDbCascade;
60+
return hasCascadeOnTable;
5761
}
5862

5963
async discoverFields(resource: AdminForthResource, config: AdminForthConfig): Promise<{[key: string]: AdminForthResourceColumn}> {
60-
await this.checkCascadeWhenUploadPlugin(resource, config);
6164

6265
const tableName = resource.table;
6366
const stmt = this.client.prepare(`PRAGMA table_info(${tableName})`);
64-
const rows = await stmt.all();
65-
const fkStmt = this.client.prepare(`PRAGMA foreign_key_list(${tableName})`);
66-
const fkRows = await fkStmt.all();
67-
const fkMap: { [colName: string]: boolean } = {};
68-
fkRows.forEach(fk => {fkMap[fk.from] = fk.on_delete?.toUpperCase() === 'CASCADE';})
69-
await this.hasSQLiteCascadeFk(resource, config, fkMap);
67+
const rows = await stmt.all();
68+
await this.hasSQLiteCascadeFk(resource, config);
7069
const fieldTypes = {};
7170
rows.forEach((row) => {
7271
const field: any = {};
@@ -112,7 +111,6 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
112111
field.required = row.notnull == 1;
113112
field.primaryKey = row.pk == 1;
114113

115-
field.cascade = fkMap[row.name] || false;
116114
field.default = row.dflt_value;
117115
fieldTypes[row.name] = field
118116
});

0 commit comments

Comments
 (0)