Skip to content

Commit

Permalink
Fix index query for collection name
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Apr 9, 2024
1 parent c4b01fd commit a383aa3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions libs/external-db-postgres/src/postgres_index_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ export default class IndexProvider implements IIndexProvider {
SELECT query
FROM pg_stat_activity
WHERE
-- get only the queries that are creating indexes
(query ILIKE 'CREATE INDEX%' OR query ILIKE 'CREATE UNIQUE INDEX%')
-- get only the queries that are creating indexes on collectionName table
AND (query LIKE '%${collectionName}(%')
AND (query LIKE '%${escapeIdentifier(collectionName)}(%')
-- get only the queries that are active
AND state = 'active'
GROUP BY query;
Expand Down
15 changes: 14 additions & 1 deletion libs/external-db-postgres/src/postgres_utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ describe('Postgres utils', () => {

describe('Index utils functions', () => {
test('extract DomainIndex from index query on 1 column', () => {
const indexQuery = 'CREATE INDEX idx_table_col1 ON table(col1)'
const result = extractIndexFromIndexQueryForCollection(indexQuery)
const expected = {
name: 'idx_table_col1',
columns: ['col1'],
isUnique: false,
caseInsensitive: true,
order: 'ASC',
status: DomainIndexStatus.BUILDING
}
expect(result).toEqual(expected)
})
test('extract DomainIndex from unique index query on 1 column', () => {
const indexQuery = 'CREATE UNIQUE INDEX idx_table_col1 ON table(col1)'
const result = extractIndexFromIndexQueryForCollection(indexQuery)
const expected = {
Expand All @@ -42,7 +55,7 @@ describe('Postgres utils', () => {
expect(result).toEqual(expected)
})

test('extract DomainIndex from index query on 2 column', () => {
test('extract DomainIndex from unique index query on 2 column', () => {
const indexQuery = 'CREATE UNIQUE INDEX idx_table_col1_col2 ON table(col1, col2)'
const result = extractIndexFromIndexQueryForCollection(indexQuery)
const expected = {
Expand Down

0 comments on commit a383aa3

Please sign in to comment.