Skip to content

Commit b80e798

Browse files
authored
Table editor cast column type to text if filtering with like based filter (supabase#37341)
* Table editor cast column type to text if filtering with like based filter * Update pg-meta tests * Fix test cases for ~~ in advanced-query.test * Fix tests in table-row-query.test
1 parent 31b8fbe commit b80e798

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

packages/pg-meta/src/query/Query.utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ident, literal, format } from '../pg-format'
2-
import type { Filter, QueryPagination, QueryTable, Sort, Dictionary } from './types'
1+
import { format, ident, literal } from '../pg-format'
2+
import type { Dictionary, Filter, QueryPagination, QueryTable, Sort } from './types'
33

44
export function countQuery(
55
table: QueryTable,
@@ -171,6 +171,11 @@ function applyFilters(query: string, filters: Filter[]) {
171171
return inFilterSql(filter)
172172
case 'is':
173173
return isFilterSql(filter)
174+
case '~~':
175+
case '~~*':
176+
case '!~~':
177+
case '!~~*':
178+
return castColumnToText(filter)
174179
default:
175180
return `${ident(filter.column)} ${filter.operator} ${filterLiteral(filter.value)}`
176181
}
@@ -203,6 +208,10 @@ function isFilterSql(filter: Filter) {
203208
}
204209
}
205210

211+
function castColumnToText(filter: Filter) {
212+
return `${ident(filter.column)}::text ${filter.operator} ${filterLiteral(filter.value)}`
213+
}
214+
206215
function filterLiteral(value: any) {
207216
if (typeof value === 'string') {
208217
if (value?.startsWith('ARRAY[') && value?.endsWith(']')) {

packages/pg-meta/test/query/advanced-query.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { expect, test, describe, afterAll } from 'vitest'
1+
import { afterAll, describe, expect, test } from 'vitest'
22
import { Query } from '../../src/query/Query'
3-
import { createTestDatabase, cleanupRoot } from '../db/utils'
3+
import { cleanupRoot, createTestDatabase } from '../db/utils'
44

55
type TestDb = Awaited<ReturnType<typeof createTestDatabase>>
66

@@ -343,7 +343,7 @@ describe('Advanced Query Tests', () => {
343343
.toSql()
344344

345345
expect(sql).toMatchInlineSnapshot(
346-
`"select id, name from public.normal_table where id > 10 and name ~~ '%John%' order by normal_table.name asc nulls last limit 10 offset 0;"`
346+
`"select id, name from public.normal_table where id > 10 and name::text ~~ '%John%' order by normal_table.name asc nulls last limit 10 offset 0;"`
347347
)
348348

349349
const result = await validateSql(db, sql)
@@ -444,7 +444,7 @@ describe('Advanced Query Tests', () => {
444444
.toSql()
445445

446446
expect(sql).toMatchInlineSnapshot(
447-
'"select count(*) from public.normal_table where name ~~ \'%John%\';"'
447+
'"select count(*) from public.normal_table where name::text ~~ \'%John%\';"'
448448
)
449449

450450
const result = await validateSql(db, sql)

packages/pg-meta/test/query/query.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ describe('End-to-end query chaining', () => {
515515
.filter('name', '~~', '%John%')
516516
.toSql()
517517

518-
expect(sql).toBe("select id, name, email from public.users where id > 10 and name ~~ '%John%';")
518+
expect(sql).toBe(
519+
"select id, name, email from public.users where id > 10 and name::text ~~ '%John%';"
520+
)
519521
})
520522

521523
test('should correctly build a select query with match criteria', () => {

packages/pg-meta/test/query/table-row-query.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { expect, test, describe, afterAll, beforeAll } from 'vitest'
2-
import { createTestDatabase, cleanupRoot } from '../db/utils'
1+
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
32
import pgMeta from '../../src/index'
3+
import { Filter, Sort } from '../../src/query'
44
import { getDefaultOrderByColumns, getTableRowsSql } from '../../src/query/table-row-query'
5-
import { Sort, Filter } from '../../src/query'
5+
import { cleanupRoot, createTestDatabase } from '../db/utils'
66

77
beforeAll(async () => {
88
// Any global setup if needed
@@ -1098,7 +1098,7 @@ describe('Table Row Query', () => {
10981098
// Verify SQL generation with snapshot
10991099
expect(sql).toMatchInlineSnapshot(
11001100
`
1101-
"with _base_query as (select * from public.test_sql_filter where name ~~ 'Test%' and category = 'A' order by test_sql_filter.id asc nulls last limit 10 offset 0)
1101+
"with _base_query as (select * from public.test_sql_filter where name::text ~~ 'Test%' and category = 'A' order by test_sql_filter.id asc nulls last limit 10 offset 0)
11021102
select id,case
11031103
when octet_length(name::text) > 10240
11041104
then left(name::text, 10240) || '...'

0 commit comments

Comments
 (0)