The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Joshenlim/fe 4373 add ilike comparator for pathname in unified logs (#50386)
Joshen Lime296d0ae4f84
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.tsindex 3aff9b46fd1..82fb7753a96 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts@@ -9,6 +9,7 @@ import dayjs from 'dayjs'import { DEFAULT_LOG_TYPES } from './UnifiedLogs.constants'import { groupLogsFiltersByColumn, parseLogsFilterUrlParams } from './UnifiedLogs.filters'import { QuerySearchParamsType, SearchParamsType } from './UnifiedLogs.types'+import { wrapIlikePattern } from './UnifiedLogs.utils'import {joinSqlFragments,analyticsLiteral as lit,@@ -73,16 +74,18 @@ const buildConditions = (const likeOp = isNeq ? NOT_LIKE_OP : LIKE_OPconst joinAndOr = isNeq ? ' AND ' : ' OR '- if (key === 'event_message' && (operator === '~~*' || operator === '!~~*')) {+ if (+ (key === 'event_message' || key === 'pathname') &&+ (operator === '~~*' || operator === '!~~*')+ ) {// BigQuery has no ILIKE; emulate via LOWER(col) (NOT) LIKE LOWER('%v%').// Auto-wrap with `%…%` unless the user already included one. Multiple// ILIKE values join with OR; NOT ILIKE joins with AND (row must contain// none of the given substrings).- const pattern = (v: string) => (v.includes('%') ? v : '%' + v + '%')const likeKeyword = operator === '!~~*' ? safeSql`NOT LIKE` : safeSql`LIKE`const join = operator === '!~~*' ? ' AND ' : ' OR 'const branches = values.map(- (v) => safeSql`LOWER(${col}) ${likeKeyword} LOWER(${lit(pattern(v))})`+ (v) => safeSql`LOWER(${col}) ${likeKeyword} LOWER(${lit(wrapIlikePattern(v))})`)conditions.push(safeSql`(${joinSqlFragments(branches, join)})`)} else if (key === 'host' || key === 'pathname') {diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.tsindex ee37c99163d..3eee4bca3bf 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts@@ -126,6 +126,37 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {expect(sql).toContain(`log_attributes['request.url'] NOT LIKE '%cdn.foo%'`)})+ it('emits ILIKE for pathname `~~*`, case-insensitive unlike the `=` LIKE behavior', () => {+ const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:Customers'))+ expect(sql).toContain(`log_attributes['request.path'] ILIKE '%Customers%'`)+ })++ it('emits NOT ILIKE for pathname `!~~*` so rows matching the term are excluded', () => {+ const sql = getUnifiedLogsQuery(withFilters('pathname:notilike:health'))+ expect(sql).toContain(`log_attributes['request.path'] NOT ILIKE '%health%'`)+ })++ it('joins multiple pathname NOT ILIKE values with AND (row must match none)', () => {+ const sql = getUnifiedLogsQuery(+ withFilters('pathname:notilike:health', 'pathname:notilike:metrics')+ )+ expect(sql).toMatch(+ /log_attributes\['request\.path'\] NOT ILIKE '%health%' AND log_attributes\['request\.path'\] NOT ILIKE '%metrics%'/+ )+ })++ it('passes through user-supplied `%` wildcards on pathname ILIKE without double-wrapping', () => {+ const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:foo%'))+ expect(sql).toContain(`log_attributes['request.path'] ILIKE 'foo%'`)+ expect(sql).not.toContain(`'%foo%%'`)+ })++ it('passes through user-supplied `_` wildcards on pathname ILIKE without wrapping', () => {+ const sql = getUnifiedLogsQuery(withFilters('pathname:ilike:fo_bar'))+ expect(sql).toContain(`log_attributes['request.path'] ILIKE 'fo_bar'`)+ expect(sql).not.toContain(`'%fo_bar%'`)+ })+it('emits ILIKE with auto-wrapped `%…%` for event_message `~~*`', () => {const sql = getUnifiedLogsQuery(withFilters('event_message:ilike:Permission Denied'))expect(sql).toContain(`event_message ILIKE '%Permission Denied%'`)@@ -151,6 +182,12 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {expect(sql).not.toContain(`'%error%%'`)})+ it('passes through user-supplied `_` wildcards on event_message ILIKE without wrapping', () => {+ const sql = getUnifiedLogsQuery(withFilters('event_message:ilike:foo_bar'))+ expect(sql).toContain(`event_message ILIKE 'foo_bar'`)+ expect(sql).not.toContain(`'%foo_bar%'`)+ })+it('excludes connection log messages when show_connection_logs=false', () => {const sql = getUnifiedLogsQuery({ ...baseSearch, show_connection_logs: false } as any)expect(sql).toContain("source != 'postgres_logs'")@@ -457,4 +494,47 @@ describe('UnifiedLogs.queries.bq', () => {expect(sql).toContain("LOWER(`event_message`) NOT LIKE LOWER('%cron%')")expect(sql).not.toMatch(/\bILIKE\b/)})++ it('emulates ILIKE with LOWER()/LOWER() for pathname `~~*`', () => {+ const sql = getUnifiedLogsQueryBQ(withFilters('pathname:ilike:Customers'))+ expect(sql).toContain("LOWER(`pathname`) LIKE LOWER('%Customers%')")+ expect(sql).not.toMatch(/\bILIKE\b/)+ })++ it('emulates NOT ILIKE with LOWER()/NOT LIKE/LOWER() for pathname `!~~*`', () => {+ const sql = getUnifiedLogsQueryBQ(withFilters('pathname:notilike:health'))+ expect(sql).toContain("LOWER(`pathname`) NOT LIKE LOWER('%health%')")+ expect(sql).not.toMatch(/\bILIKE\b/)+ })++ it('passes through user-supplied `%` wildcards on pathname ILIKE without double-wrapping', () => {+ const sql = getUnifiedLogsQueryBQ(withFilters('pathname:ilike:foo%'))+ expect(sql).toContain("LOWER(`pathname`) LIKE LOWER('foo%')")+ expect(sql).not.toContain("LOWER('%foo%%')")+ })++ it('passes through user-supplied `_` wildcards on pathname ILIKE without wrapping', () => {+ const sql = getUnifiedLogsQueryBQ(withFilters('pathname:ilike:fo_bar'))+ expect(sql).toContain("LOWER(`pathname`) LIKE LOWER('fo_bar')")+ expect(sql).not.toContain("LOWER('%fo_bar%')")+ })+})++describe('pathname ILIKE prefix matching (cross-builder)', () => {+ // Both query builders must treat an explicit `%` the same way — a value+ // like `foo%` is a prefix match, not a "contains" search with a stray+ // trailing wildcard — otherwise switching the `otelUnifiedLogs` flag would+ // silently change what a saved/shared filter matches.+ it('produces equivalent prefix-matching patterns for `pathname:ilike:foo%` on both backends', () => {+ const clickhouseSql = getUnifiedLogsQuery(withFilters('pathname:ilike:foo%'))+ const bqSql = getUnifiedLogsQueryBQ(withFilters('pathname:ilike:foo%'))++ expect(clickhouseSql).toContain(`log_attributes['request.path'] ILIKE 'foo%'`)+ expect(bqSql).toContain("LOWER(`pathname`) LIKE LOWER('foo%')")++ // Neither backend should have double-wrapped the wildcard into a+ // "contains" pattern.+ expect(clickhouseSql).not.toContain(`'%foo%%'`)+ expect(bqSql).not.toContain("LOWER('%foo%%')")+ })})diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.tsindex 8da74e7b222..973e24e75b3 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts@@ -7,6 +7,7 @@ import {type LogsFilterOperator,} from './UnifiedLogs.filters'import { QuerySearchParamsType, SearchParamsType } from './UnifiedLogs.types'+import { wrapIlikePattern } from './UnifiedLogs.utils'import {joinSqlFragments,analyticsLiteral as lit,@@ -179,11 +180,20 @@ const translateFilter = (// Postgres SQLSTATE for postgres rows. Inline STATUS_EXPR so e.g.// filtering on '00000' picks up postgres success rows.return safeSql`(${STATUS_EXPR}) ${inOp} ${inList(values)}`- case 'pathname':+ case 'pathname': {+ if (operator === '~~*' || operator === '!~~*') {+ const op = operator === '~~*' ? ILIKE_OP : NOT_ILIKE_OP+ const join = operator === '!~~*' ? ' AND ' : ' OR '+ return safeSql`(${joinSqlFragments(+ values.map((v) => safeSql`${ATTR.path} ${op} ${lit(wrapIlikePattern(v))}`),+ join+ )})`+ }return safeSql`(${joinSqlFragments(values.map((v) => safeSql`${ATTR.path} ${likeOp} ${lit('%' + v + '%')}`),joinAndOr)})`+ }case 'host':// Best-effort: use full request URL since `host` isn't a top-level field.return safeSql`(${joinSqlFragments(@@ -199,9 +209,8 @@ const translateFilter = (if (operator === '~~*' || operator === '!~~*') {const op = operator === '~~*' ? ILIKE_OP : NOT_ILIKE_OPconst join = operator === '!~~*' ? ' AND ' : ' OR '- const pattern = (v: string) => (v.includes('%') ? v : '%' + v + '%')return safeSql`(${joinSqlFragments(- values.map((v) => safeSql`event_message ${op} ${lit(pattern(v))}`),+ values.map((v) => safeSql`event_message ${op} ${lit(wrapIlikePattern(v))}`),join)})`}diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.test.tsindex d57936b28b6..96e55493dac 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.test.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.test.ts@@ -8,6 +8,7 @@ import {getEventMessageDisplay,getRawLogData,parseMultigresEventMessage,+ wrapIlikePattern,} from './UnifiedLogs.utils'describe('buildUnifiedLogsUrl', () => {@@ -260,3 +261,18 @@ describe('getComputeLogsAvailability', () => {})})})++describe('wrapIlikePattern', () => {+ it('wraps a bare value in %...% for a contains match', () => {+ expect(wrapIlikePattern('foo')).toBe('%foo%')+ })++ it('leaves the value untouched when it already contains a % wildcard', () => {+ expect(wrapIlikePattern('foo%')).toBe('foo%')+ expect(wrapIlikePattern('%foo')).toBe('%foo')+ })++ it('leaves the value untouched when it already contains a _ wildcard', () => {+ expect(wrapIlikePattern('fo_bar')).toBe('fo_bar')+ })+})diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.tsindex bda880a754a..3027e48b6c7 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts@@ -261,6 +261,15 @@ export function gateLogTypeOptions<T extends { value: string; options?: Option[]})}+/**+ * Wraps a raw ILIKE/NOT ILIKE search term in `%...%` for a "contains" match,+ * unless it already includes a `%` or `_` wildcard — in which case the user+ * has crafted their own pattern and it's passed through unchanged.+ */+export function wrapIlikePattern(value: string): string {+ return value.includes('%') || value.includes('_') ? value : `%${value}%`+}+export function gateLogTypeFilters(filters: string[] | null | undefined,visibility: Partial<Record<UnifiedLogType, boolean>>diff --git a/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.test.ts b/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.test.tsindex 5a9828e42f6..177bb2953e3 100644--- a/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.test.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.test.ts@@ -20,6 +20,7 @@ describe('buildFilterProperties', () => {options: [{ label: 'Postgres', value: 'postgres' }],},{ label: 'Event message', value: 'event_message', type: 'input' },+ { label: 'Pathname', value: 'pathname', type: 'checkbox' },]it('produces properties that all satisfy the FilterProperty schema', () => {@@ -30,7 +31,7 @@ describe('buildFilterProperties', () => {it('drops timerange fields and appends the synthetic user property last', () => {const names = buildFilterProperties({ fields }).map((property) => property.name)expect(names).not.toContain('date')- expect(names).toEqual(['log_type', 'event_message', USER_PROPERTY])+ expect(names).toEqual(['log_type', 'event_message', 'pathname', USER_PROPERTY])})it('gives the event_message column pattern (ILIKE) operators', () => {@@ -53,6 +54,18 @@ describe('buildFilterProperties', () => {])})+ it('gives pathname both comparison and pattern (ILIKE) operators', () => {+ const pathname = buildFilterProperties({ fields }).find(+ (property) => property.name === 'pathname'+ )+ expect(pathname?.operators).toEqual([+ { label: 'Equals', value: '=', group: 'comparison' },+ { label: 'Not equal', value: '<>', group: 'comparison' },+ { label: 'iLike', value: '~~*', group: 'pattern' },+ { label: 'Not iLike', value: '!~~*', group: 'pattern' },+ ])+ })+it('defaults a column without options to an empty array', () => {const eventMessage = buildFilterProperties({ fields }).find((property) => property.name === 'event_message'diff --git a/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.ts b/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.tsindex 302d1e424f2..4616e4a8643 100644--- a/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/components/LogsFilterBar.utils.ts@@ -31,6 +31,16 @@ export const filterPropertySchema = z.object({operators: z.array(filterOperatorSchema).optional(),})+const COMPARISON_OPERATORS: FilterProperty['operators'] = [+ { label: 'Equals', value: '=', group: 'comparison' },+ { label: 'Not equal', value: '<>', group: 'comparison' },+]++const PATTERN_OPERATORS: FilterProperty['operators'] = [+ { label: 'iLike', value: '~~*', group: 'pattern' },+ { label: 'Not iLike', value: '!~~*', group: 'pattern' },+]+export const buildFilterProperties = ({fields,userOptions,@@ -41,24 +51,23 @@ export const buildFilterProperties = ({return [...fields.filter((field) => field.type !== 'timerange')- .map(- (field): FilterProperty => ({+ .map((field): FilterProperty => {+ // event_message only supports substring matching (it's free text); pathname+ // supports both exact segment matching and substring matching.+ const operators =+ field.value === 'event_message'+ ? PATTERN_OPERATORS+ : field.value === 'pathname'+ ? [...COMPARISON_OPERATORS, ...PATTERN_OPERATORS]+ : COMPARISON_OPERATORS+ return {label: field.label,name: field.value,type: 'string',options: field.options ?? [],- operators:- field.value === 'event_message'- ? [- { label: 'iLike', value: '~~*', group: 'pattern' },- { label: 'Not iLike', value: '!~~*', group: 'pattern' },- ]- : [- { label: 'Equals', value: '=', group: 'comparison' },- { label: 'Not equal', value: '<>', group: 'comparison' },- ],- })- ),+ operators,+ }+ }),{label: 'User',name: USER_PROPERTY,diff --git a/packages/ui-patterns/src/FilterBar/menuItems.test.ts b/packages/ui-patterns/src/FilterBar/menuItems.test.tsindex 60be56a664c..52c1a95b984 100644--- a/packages/ui-patterns/src/FilterBar/menuItems.test.ts+++ b/packages/ui-patterns/src/FilterBar/menuItems.test.ts@@ -31,7 +31,22 @@ const booleanProperty: FilterProperty = {],}-const filterProperties: FilterProperty[] = [stringProperty, booleanProperty]+const pathProperty: FilterProperty = {+ label: 'Pathname',+ name: 'pathname',+ type: 'string',+ options: [+ { label: '/auth/v1/health', value: '/auth/v1/health' },+ { label: '/rest-admin/v1/ready', value: '/rest-admin/v1/ready' },+ ],+ operators: [+ { value: '=', label: 'Equals', group: 'comparison' as const },+ { value: '~~*', label: 'iLike', group: 'pattern' as const },+ { value: '!~~*', label: 'Not iLike', group: 'pattern' as const },+ ],+}++const filterProperties: FilterProperty[] = [stringProperty, booleanProperty, pathProperty]describe('buildOperatorItems', () => {it('returns matching operators for operator draft text', () => {@@ -145,6 +160,50 @@ describe('buildValueItems', () => {{ value: 'bob', label: 'Bob' },])})++ it.each(['~~*', '!~~*'])(+ 'suppresses value suggestions for pattern-group operator `%s`',+ (operator) => {+ const filters: FilterGroup = {+ logicalOperator: 'AND',+ conditions: [{ propertyName: 'pathname', operator, value: '' }],+ }++ const items = buildValueItems(+ { type: 'value', path: [0] },+ filters,+ filterProperties,+ {},+ {},+ '',+ false+ )++ expect(items).toEqual([])+ }+ )++ it('still returns options for pathname with a comparison-group operator', () => {+ const filters: FilterGroup = {+ logicalOperator: 'AND',+ conditions: [{ propertyName: 'pathname', operator: '=', value: '' }],+ }++ const items = buildValueItems(+ { type: 'value', path: [0] },+ filters,+ filterProperties,+ {},+ {},+ '',+ false+ )++ expect(items).toEqual([+ { value: '/auth/v1/health', label: '/auth/v1/health' },+ { value: '/rest-admin/v1/ready', label: '/rest-admin/v1/ready' },+ ])+ })})describe('buildPropertyItems', () => {diff --git a/packages/ui-patterns/src/FilterBar/menuItems.ts b/packages/ui-patterns/src/FilterBar/menuItems.tsindex 34e8687190e..8ed83c386d2 100644--- a/packages/ui-patterns/src/FilterBar/menuItems.ts+++ b/packages/ui-patterns/src/FilterBar/menuItems.ts@@ -146,6 +146,18 @@ export function buildValueItems(return getIsOperatorValueItems(property, inputValue, hasTypedSinceFocus)}+ // Pattern-matching operators (e.g. iLike) search for a substring, not an exact+ // value, so a dropdown of exact-value suggestions would be misleading — let the+ // user type freely instead.+ const activeOperator = property.operators?.find((op) =>+ isFilterOperatorObject(op)+ ? op.value === activeCondition?.operator+ : op === activeCondition?.operator+ )+ if (isFilterOperatorObject(activeOperator) && activeOperator.group === 'pattern') {+ return items+ }+if (!Array.isArray(property.options) && isCustomOptionObject(property.options)) {items.push({value: 'custom',