From 3ceb04babd131239739cf2420f5a28d4e7fdca1a Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Wed, 12 Jan 2022 12:38:56 -0600 Subject: [PATCH 1/3] Add clean-workspace workflow to see if this fixes npm permission errors --- .github/workflows/tests.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 011775bd..19dd04ed 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -16,6 +16,8 @@ jobs: image: node:lts steps: + - uses: Chia-Network/actions/clean-workspace@main + - name: Checkout Code uses: actions/checkout@v2 From b3adcc7f1db42efcd22ef1351c476683d5903c61 Mon Sep 17 00:00:00 2001 From: Mike Keen Date: Wed, 19 Jan 2022 19:57:21 -0500 Subject: [PATCH 2/3] fix: don't crash for dashes at beginning and end of search queries --- src/models/projects/projects.model.js | 20 +++++++++++++++----- src/models/units/units.model.js | 20 ++++++++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 762cbf0c..a3cf872c 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -170,10 +170,20 @@ class Project extends Model { if (columns.length) { fields = columns.join(', '); } - - // hyphens cause errors in sqlite, but we can replace it with a + and - // the fulltext search will work the same - searchStr = searchStr = searchStr.replaceAll('-', '+'); + + searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash + searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing + searchStr += '*'; // Query should end with asterisk for partial matching + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set + return { + count: 0, + rows: [], + } + } + + if (searchStr.startsWith('+')) { + searchStr = searchStr.replace('+', '') // If query starts with +, replace it + } let sql = `SELECT ${fields} FROM projects_fts WHERE projects_fts MATCH :search`; @@ -181,7 +191,7 @@ class Project extends Model { sql = `${sql} AND orgUid = :orgUid`; } - const replacements = { search: `${searchStr}*`, orgUid }; + const replacements = { search: searchStr, orgUid }; const count = ( await sequelize.query(sql, { diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index c5ff292f..5a887806 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -175,7 +175,7 @@ class Unit extends Model { unitMarketplaceLink, cooresponingAdjustmentDeclaration, correspondingAdjustmentStatus - ) AGAINST ":search" + ) AGAINST '":search"' `; if (orgUid) { @@ -216,15 +216,27 @@ class Unit extends Model { fields = columns.join(', '); } - searchStr = searchStr = searchStr.replaceAll('-', '+'); - + searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash + searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing + searchStr += '*'; // Query should end with asterisk for partial matching + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set + return { + count: 0, + rows: [], + } + } + + if (searchStr.startsWith('+')) { + searchStr = searchStr.replace('+', '') // If query starts with +, replace it + } + let sql = `SELECT ${fields} FROM units_fts WHERE units_fts MATCH :search`; if (orgUid) { sql = `${sql} AND orgUid = :orgUid`; } - const replacements = { search: `${searchStr}*`, orgUid }; + const replacements = { search: searchStr, orgUid }; const count = ( await sequelize.query(sql, { From 0a927305b79b0f92789347e960547c9a3eeb8017 Mon Sep 17 00:00:00 2001 From: Mike Keen Date: Wed, 19 Jan 2022 20:47:29 -0500 Subject: [PATCH 3/3] refactor: common helper --- src/models/database.js | 7 +++++++ src/models/projects/projects.model.js | 7 +++---- src/models/units/units.model.js | 9 ++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/models/database.js b/src/models/database.js index 8576cc96..70fc4d73 100644 --- a/src/models/database.js +++ b/src/models/database.js @@ -19,3 +19,10 @@ export const safeMirrorDbHandler = (callback) => { console.log('Mirror DB not connected'); }); }; + +export const sanitizeSqliteFtsQuery = (query) => { + query = query.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash + query = query.replace('-', ''); //Replace the final dash with nothing + query += '*'; // Query should end with asterisk for partial matching + return query; +} \ No newline at end of file diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index a3cf872c..50bccf40 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -4,7 +4,7 @@ import Sequelize from 'sequelize'; import rxjs from 'rxjs'; const { Model } = Sequelize; -import { sequelize, safeMirrorDbHandler } from '../database'; +import {sequelize, safeMirrorDbHandler, sanitizeSqliteFtsQuery} from '../database'; import { RelatedProject, @@ -171,9 +171,8 @@ class Project extends Model { fields = columns.join(', '); } - searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash - searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing - searchStr += '*'; // Query should end with asterisk for partial matching + searchStr = sanitizeSqliteFtsQuery(searchStr); + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set return { count: 0, diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index 5a887806..a517028f 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -1,7 +1,7 @@ 'use strict'; import Sequelize from 'sequelize'; -import { sequelize, safeMirrorDbHandler } from '../database'; +import {sequelize, safeMirrorDbHandler, sanitizeSqliteFtsQuery} from '../database'; import { Qualification, Vintage } from '../../models'; import { UnitMirror } from './units.model.mirror'; import ModelTypes from './units.modeltypes.cjs'; @@ -215,10 +215,9 @@ class Unit extends Model { if (columns.length) { fields = columns.join(', '); } - - searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash - searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing - searchStr += '*'; // Query should end with asterisk for partial matching + + searchStr = sanitizeSqliteFtsQuery(searchStr); + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set return { count: 0,