Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sql regexp to default to empty string when field is null #3480

Merged
merged 8 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function asyncTransaction(
}

function regexp(regex: string, text: string | null) {
return new RegExp(regex).test(text) ? 1 : 0;
return new RegExp(regex).test(text || '') ? 1 : 0;
}

export function openDatabase(pathOrBuffer: string | Buffer) {
Expand Down
49 changes: 37 additions & 12 deletions packages/loot-core/src/platform/server/sqlite/index.web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ beforeAll(() => {

const initSQL = `
CREATE TABLE numbers (id TEXT PRIMARY KEY, number INTEGER);
CREATE TABLE textstrings (id TEXT PRIMARY KEY, string TEXT);
`;

interface NumberRowType {
id: string;
number: number;
}

interface StringRowType {
id: string;
string: string;
}

describe('Web sqlite', () => {
it('should rollback transactions', async () => {
const db = await openDatabase();
Expand All @@ -27,8 +38,7 @@ describe('Web sqlite', () => {

let rows = runQuery(db, 'SELECT * FROM numbers', null, true);
expect(rows.length).toBe(1);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[0].number).toBe(4);
expect((rows[0] as NumberRowType).number).toBe(4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 warning: ‏manual type coercions hide the type issue rather than address it. Furthermore: they can and do often lead to subtle, hard-to-catch bugs.

If we cannot patch this properly (i.e. by explicitly stating the runQuery return type), then the previous solution of using @ts-expect-error is actually better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I disagree that expect-error is better, adding linter exceptions creates a culture of accepting the problem, instead of fixing it at the level you are able, as well as having other issues. But, I also understand it's not my call, and code consistency matters, so I'll revert those changes.


const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
expect(() => {
Expand All @@ -44,8 +54,7 @@ describe('Web sqlite', () => {
// Nothing should have changed in the db
rows = runQuery(db, 'SELECT * FROM numbers', null, true);
expect(rows.length).toBe(1);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[0].number).toBe(4);
expect((rows[0] as NumberRowType).number).toBe(4);
});

it('should support nested transactions', async () => {
Expand All @@ -56,8 +65,7 @@ describe('Web sqlite', () => {

let rows = runQuery(db, 'SELECT * FROM numbers', null, true);
expect(rows.length).toBe(1);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[0].number).toBe(4);
expect((rows[0] as NumberRowType).number).toBe(4);

transaction(db, () => {
runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id2', 5)");
Expand All @@ -78,11 +86,28 @@ describe('Web sqlite', () => {
// Nothing should have changed in the db
rows = runQuery(db, 'SELECT * FROM numbers', null, true);
expect(rows.length).toBe(3);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[0].number).toBe(4);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[1].number).toBe(5);
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[2].number).toBe(6);
expect((rows[0] as NumberRowType).number).toBe(4);
expect((rows[1] as NumberRowType).number).toBe(5);
expect((rows[2] as NumberRowType).number).toBe(6);
});

it('should match regex on text fields', async () => {
const db = await openDatabase();
execQuery(db, initSQL);

runQuery(
db,
"INSERT INTO textstrings (id, string) VALUES ('id1', 'not empty string')",
);
runQuery(db, "INSERT INTO textstrings (id) VALUES ('id2')");

const rows = runQuery(
db,
'SELECT id FROM textstrings where REGEXP("n.", string)',
null,
true,
);
expect(rows.length).toBe(1);
expect((rows[0] as StringRowType).id).toBe('id1');
});
});
2 changes: 1 addition & 1 deletion packages/loot-core/src/platform/server/sqlite/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function asyncTransaction(db: Database, fn: () => Promise<void>) {
}

function regexp(regex: string, text: string) {
return new RegExp(regex).test(text) ? 1 : 0;
return new RegExp(regex).test(text || '') ? 1 : 0;
}

export async function openDatabase(pathOrBuffer?: string | Buffer) {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3480.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [qedi-r]
---

Fix 'matches' operator incorrectly matching empty strings.
Loading