@@ -9,10 +9,53 @@ import testUtil from '../../tests/util'
99
1010var should = chai . should ( )
1111
12+ /**
13+ * Add full text index for projects.
14+ */
15+ function addFullTextIndex ( ) {
16+ if ( models . sequelize . options . dialect !== 'postgres' ) {
17+ console . log ( 'Not creating search index, must be using POSTGRES to do this' ) ;
18+ return ;
19+ }
20+
21+ return models . sequelize
22+ . query ( 'ALTER TABLE projects ADD COLUMN "projectFullText" text;' )
23+ . then ( function ( ) {
24+ return models . sequelize
25+ . query ( 'UPDATE projects SET "projectFullText" = lower(' +
26+ 'name || \' \' || coalesce(description, \'\') || \' \' || coalesce(details#>>\'{utm, code}\', \'\'));' ) ;
27+ } ) . then ( function ( ) {
28+ return models . sequelize
29+ . query ( 'CREATE EXTENSION IF NOT EXISTS pg_trgm;' ) ;
30+ } ) . then ( function ( ) {
31+ return models . sequelize
32+ . query ( 'CREATE INDEX project_text_search_idx ON projects USING GIN("projectFullText" gin_trgm_ops);' ) ;
33+ } ) . then ( function ( ) {
34+ return models . sequelize
35+ . query ( 'CREATE OR REPLACE FUNCTION project_text_update_trigger() RETURNS trigger AS $$ ' +
36+ 'begin ' +
37+ 'new."projectFullText" := ' +
38+ 'lower(new.name || \' \' || coalesce(new.description, \'\') || \' \' || coalesce(new.details#>>\'{utm, code}\', \'\')); ' +
39+ 'return new; ' +
40+ 'end ' +
41+ '$$ LANGUAGE plpgsql;' ) ;
42+ } ) . then ( function ( ) {
43+ return models . sequelize
44+ . query ( 'DROP TRIGGER IF EXISTS project_text_update ON projects;' ) ;
45+ } ) . then ( function ( ) {
46+ return models . sequelize
47+ . query ( 'CREATE TRIGGER project_text_update BEFORE INSERT OR UPDATE ON projects' +
48+ ' FOR EACH ROW EXECUTE PROCEDURE project_text_update_trigger();' ) ;
49+ } ) . catch ( function ( err ) {
50+ console . log ( 'Failed: ' , err ) ;
51+ } ) ;
52+ }
53+
1254describe ( 'LIST Project' , ( ) => {
1355 var project1 , project2
1456 before ( done => {
1557 testUtil . clearDb ( )
58+ . then ( ( ) => addFullTextIndex ( ) )
1659 . then ( ( ) => {
1760 var p1 = models . Project . create ( {
1861 type : 'generic' ,
0 commit comments