-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not apply block range filter on internal tables (#303)
This commit also refactors out applyBlockFilter helper into database utils to avoid repetition.
- Loading branch information
Showing
5 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import pluralize from 'pluralize'; | ||
import { Knex } from 'knex'; | ||
import { INTERNAL_TABLES } from '../stores/checkpoints'; | ||
|
||
export const getTableName = (name: string) => { | ||
if (name === '_metadata') return '_metadatas'; | ||
|
||
return pluralize(name); | ||
}; | ||
|
||
export function applyBlockFilter(query: Knex.QueryBuilder, tableName: string, block?: number) { | ||
if (INTERNAL_TABLES.includes(tableName)) return query; | ||
|
||
return block !== undefined | ||
? query.andWhereRaw(`${tableName}.block_range @> int8(??)`, [block]) | ||
: query.andWhereRaw(`upper_inf(${tableName}.block_range)`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import knex from 'knex'; | ||
import { getTableName, applyBlockFilter } from '../../../src/utils/database'; | ||
|
||
const mockKnex = knex({ | ||
client: 'sqlite3', | ||
connection: { | ||
filename: ':memory:' | ||
}, | ||
useNullAsDefault: true | ||
}); | ||
|
||
afterAll(async () => { | ||
await mockKnex.destroy(); | ||
}); | ||
|
||
describe('getTableName', () => { | ||
it.each([ | ||
['table', 'tables'], | ||
['user', 'users'], | ||
['post', 'posts'], | ||
['space', 'spaces'], | ||
['vote', 'votes'], | ||
['comment', 'comments'] | ||
])('should return pluralized table name', (name, expected) => { | ||
expect(getTableName(name)).toEqual(expected); | ||
}); | ||
|
||
it('should return hardcoded table name for metadata', () => { | ||
expect(getTableName('_metadata')).toEqual('_metadatas'); | ||
}); | ||
}); | ||
|
||
describe('applyBlockFilter', () => { | ||
it('should not apply filter for internal tables', () => { | ||
const query = mockKnex.select('*').from('_metadatas'); | ||
|
||
const result = applyBlockFilter(query, '_metadatas', 123); | ||
|
||
expect(result.toString()).toBe('select * from `_metadatas`'); | ||
}); | ||
|
||
it('should apply capped block filter if block is provided', () => { | ||
const query = mockKnex.select('*').from('posts'); | ||
|
||
const result = applyBlockFilter(query, 'posts', 123); | ||
|
||
expect(result.toString()).toBe('select * from `posts` where posts.block_range @> int8(123)'); | ||
}); | ||
|
||
it('should apply upper_inf block filter if block is not provided', () => { | ||
const query = mockKnex.select('*').from('posts'); | ||
|
||
const result = applyBlockFilter(query, 'posts'); | ||
|
||
expect(result.toString()).toBe('select * from `posts` where upper_inf(posts.block_range)'); | ||
}); | ||
}); |