Skip to content

Commit ffd01cc

Browse files
huangqunparthshah
authored andcommitted
automatically create project search index in test code (#27)
1 parent b27b334 commit ffd01cc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/routes/projects/list.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,53 @@ import testUtil from '../../tests/util'
99

1010
var 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+
1254
describe('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

Comments
 (0)