mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 09:56:44 +00:00 
			
		
		
		
	fix: postgres timeout error due to complex join queries to fect foreign key references
This commit is contained in:
		
							parent
							
								
									86e57e17f4
								
							
						
					
					
						commit
						e00b8f048d
					
				| @ -40,28 +40,40 @@ const SQL_QUERIES = { | |||||||
|   `,
 |   `,
 | ||||||
|   FOREIGN_KEY_LIST: /* sql */ ` |   FOREIGN_KEY_LIST: /* sql */ ` | ||||||
|     SELECT |     SELECT | ||||||
|       tco."constraint_name" as constraint_name, |       tco."constraint_name" as constraint_name | ||||||
|       kcu."column_name" as column_name, |  | ||||||
|       rel_kcu."table_name" as foreign_table, |  | ||||||
|       rel_kcu."column_name" as fk_column_name, |  | ||||||
|       rco.update_rule as on_update, |  | ||||||
|       rco.delete_rule as on_delete |  | ||||||
|     FROM information_schema.table_constraints tco |     FROM information_schema.table_constraints tco | ||||||
|     JOIN information_schema.key_column_usage kcu |  | ||||||
|       ON tco.constraint_schema = kcu.constraint_schema |  | ||||||
|       AND tco.constraint_name = kcu.constraint_name |  | ||||||
|     JOIN information_schema.referential_constraints rco |  | ||||||
|       ON tco.constraint_schema = rco.constraint_schema |  | ||||||
|       AND tco.constraint_name = rco.constraint_name |  | ||||||
|     JOIN information_schema.key_column_usage rel_kcu |  | ||||||
|       ON rco.unique_constraint_schema = rel_kcu.constraint_schema |  | ||||||
|       AND rco.unique_constraint_name = rel_kcu.constraint_name |  | ||||||
|       AND kcu.ordinal_position = rel_kcu.ordinal_position |  | ||||||
|     WHERE |     WHERE | ||||||
|       tco.constraint_type = 'FOREIGN KEY' |       tco.constraint_type = 'FOREIGN KEY' | ||||||
|       AND tco.constraint_schema = ? |       AND tco.constraint_schema = ? | ||||||
|       AND tco.table_name = ? |       AND tco.table_name = ? | ||||||
|     ORDER BY kcu.table_schema, kcu.table_name, kcu.ordinal_position, kcu.constraint_name; |   `,
 | ||||||
|  |   FOREIGN_KEY_REFERENCES: /* sql */ ` | ||||||
|  |     SELECT | ||||||
|  |       kcu."constraint_name" as constraint_name, | ||||||
|  |       kcu."column_name" as column_name | ||||||
|  |       | ||||||
|  |     FROM information_schema.key_column_usage kcu | ||||||
|  |     WHERE kcu.constraint_name=ANY(?) | ||||||
|  |     AND kcu.table_schema = ? | ||||||
|  |     AND kcu.table_name = ?; | ||||||
|  |   `,
 | ||||||
|  | 
 | ||||||
|  |   FOREIGN_KEY_REFERENCES_CONSTRAIN: /* sql */ ` | ||||||
|  |   SELECT | ||||||
|  |   rco.update_rule as on_update, | ||||||
|  |   rco.delete_rule as on_delete, | ||||||
|  |   rco."unique_constraint_name" as unique_constraint_name | ||||||
|  |   FROM information_schema.referential_constraints rco | ||||||
|  |   WHERE rco.constraint_name=ANY(?) | ||||||
|  |   AND rco.constraint_schema = ? | ||||||
|  | `,
 | ||||||
|  |   FOREIGN_KEY_REFERENCES_CONSTRAIN_RFERENCE: /* sql */ ` | ||||||
|  |   SELECT | ||||||
|  |   rel_kcu."table_name" as foreign_table, | ||||||
|  |   rel_kcu."column_name" as fk_column_name | ||||||
|  |     FROM information_schema.key_column_usage rel_kcu | ||||||
|  |     WHERE rel_kcu.constraint_name=? | ||||||
|  |     AND rel_kcu.table_schema = ? | ||||||
| `,
 | `,
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -210,18 +222,52 @@ class PostgresqlSchemaInspector { | |||||||
|     const ret = {}; |     const ret = {}; | ||||||
| 
 | 
 | ||||||
|     for (const fk of rows) { |     for (const fk of rows) { | ||||||
|       if (!ret[fk.constraint_name]) { |  | ||||||
|       ret[fk.constraint_name] = { |       ret[fk.constraint_name] = { | ||||||
|         name: fk.constraint_name, |         name: fk.constraint_name, | ||||||
|           columns: [fk.column_name], |         columns: [], | ||||||
|           referencedColumns: [fk.fk_column_name], |         referencedColumns: [], | ||||||
|           referencedTable: fk.foreign_table, |         referencedTable: null, | ||||||
|           onUpdate: fk.on_update.toUpperCase(), |         onUpdate: null, | ||||||
|           onDelete: fk.on_delete.toUpperCase(), |         onDelete: null, | ||||||
|       }; |       }; | ||||||
|       } else { |     } | ||||||
|         ret[fk.constraint_name].columns.push(fk.column_name); |     const constraintNames = Object.keys(ret); | ||||||
|         ret[fk.constraint_name].referencedColumns.push(fk.fk_column_name); |     const dbSchema = this.getDatabaseSchema(); | ||||||
|  |     if (constraintNames.length > 0) { | ||||||
|  |       const { | ||||||
|  |         rows: fkReferences, | ||||||
|  |       } = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_REFERENCES, [ | ||||||
|  |         [constraintNames], | ||||||
|  |         dbSchema, | ||||||
|  |         tableName, | ||||||
|  |       ]); | ||||||
|  | 
 | ||||||
|  |       for (const fkReference of fkReferences) { | ||||||
|  |         ret[fkReference.constraint_name].columns.push(fkReference.column_name); | ||||||
|  | 
 | ||||||
|  |         const { | ||||||
|  |           rows: fkReferencesConstraint, | ||||||
|  |         } = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_REFERENCES_CONSTRAIN, [ | ||||||
|  |           [fkReference.constraint_name], | ||||||
|  |           dbSchema, | ||||||
|  |         ]); | ||||||
|  | 
 | ||||||
|  |         for (const fkReferenceC of fkReferencesConstraint) { | ||||||
|  |           const { | ||||||
|  |             rows: fkReferencesConstraintReferece, | ||||||
|  |           } = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_REFERENCES_CONSTRAIN_RFERENCE, [ | ||||||
|  |             fkReferenceC.unique_constraint_name, | ||||||
|  |             dbSchema, | ||||||
|  |           ]); | ||||||
|  |           for (const fkReferenceConst of fkReferencesConstraintReferece) { | ||||||
|  |             ret[fkReference.constraint_name].referencedTable = fkReferenceConst.foreign_table; | ||||||
|  |             ret[fkReference.constraint_name].referencedColumns.push( | ||||||
|  |               fkReferenceConst.fk_column_name | ||||||
|  |             ); | ||||||
|  |           } | ||||||
|  |           ret[fkReference.constraint_name].onUpdate = fkReferenceC.on_update.toUpperCase(); | ||||||
|  |           ret[fkReference.constraint_name].onDelete = fkReferenceC.on_delete.toUpperCase(); | ||||||
|  |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 meherchandan
						meherchandan