11const { sql } = require ( 'slonik' ) ;
2- const { curry, equals, slice } = require ( 'ramda' ) ;
2+ const { ascend , curry, equals, prop , slice, sortWith } = require ( 'ramda' ) ;
33
44const { testContainer } = require ( '../setup' ) ;
55
@@ -36,14 +36,23 @@ describe('database indexes', () => {
3636 WHERE pg_constraint.contype = 'f'
3737 ` ) ;
3838
39+ const fkIndexes = [ ] ;
3940 const missingIndexes = foreignKeys
4041 . filter ( fk => {
4142 const colsMatch = startsWith ( fk . local_col_indexes ) ;
42- return ! existingIndexes . find ( idx => { // eslint-disable-line arrow-body-style
43+ const foundIdx = existingIndexes . find ( idx => { // eslint-disable-line arrow-body-style
4344 return idx . tbl_schema === fk . local_tbl_schema &&
4445 idx . tbl_name === fk . local_tbl_name && // eslint-disable-line no-multi-spaces
4546 colsMatch ( idx . indexed_columns ) ;
4647 } ) ;
48+ if ( ! foundIdx ) return true ;
49+
50+ fkIndexes . push ( {
51+ 'FK table' : fk . local_tbl_name ,
52+ 'foreign key' : fk . fk_name ,
53+ 'database index' : foundIdx . idx_name ,
54+ } ) ;
55+ return false ;
4756 } ) ;
4857
4958 await Promise . all ( missingIndexes
@@ -61,13 +70,24 @@ describe('database indexes', () => {
6170 } ) ,
6271 ) ;
6372
64- missingIndexes . length . should . eql ( 0 , `${ missingIndexes . length } foreign key indexes are missing from the database.
73+ if ( missingIndexes . length ) {
74+ console . log ( '\n┌── Foreign Key Reverse Indexes ───────┐' ) ; // eslint-disable-line no-console
75+ console . table ( // eslint-disable-line no-console
76+ sortWith ( [
77+ ascend ( prop ( 'FK table' ) ) ,
78+ ascend ( prop ( 'foreign key' ) ) ,
79+ ascend ( prop ( 'database index' ) ) ,
80+ ] ) ( fkIndexes ) ,
81+ ) ;
6582
66- Either: exceptions should be added to: ${ __filename }
67- Or: a database migration should be added with the following indexes:
83+ missingIndexes . length . should . eql ( 0 , `${ missingIndexes . length } foreign key indexes are missing from the database.
6884
69- ${ missingIndexes . map ( createIdxStatement ) . sort ( ) . join ( '\n ' ) }
70- ` ) ;
85+ Either: exceptions should be added to: ${ __filename }
86+ Or: a database migration should be added with the following indexes:
87+
88+ ${ missingIndexes . map ( createIdxStatement ) . sort ( ) . join ( '\n ' ) }
89+ ` ) ;
90+ }
7191 } ) ) ;
7292} ) ;
7393
0 commit comments