Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: unescape literal util method returning wrongly for functions which are longer than one word, and json + added tests

Joshen Limbf0a2ccb5448
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index 10fd709c644..9db3ce26a5c 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -49,6 +49,7 @@ export const generateColumnFieldFromPostgresColumn = (
table: PostgresTable
): ColumnField => {
const { primary_keys } = table
+ // @ts-ignore
const primaryKeyColumns = primary_keys.map((key) => key.name)
const foreignKey = getColumnForeignKey(column, table)
const isArray = column?.data_type === 'ARRAY'
@@ -194,19 +195,20 @@ const formatArrayToPostgresArray = (arrayString: string) => {
return arrayString.replaceAll('[', '{').replaceAll(']', '}')
}
-const unescapeLiteral = (value: string) => {
+export const unescapeLiteral = (value: string) => {
if (!value) return value
const splits = value.split("'::")
if (splits.length <= 1) return value
- // Handle timezones differently
- if (value.includes('timezone')) {
- const timezoneSplits = splits[0].split('(')
- const timezone = timezoneSplits[1].slice(1)
- const expressionSplits = splits[1].split(',')
- const expression = expressionSplits[1].slice(0, expressionSplits[1].length - 1)
- return `(${expression} at time zone '${timezone}')`
+ // Handle timezones
+ if (value.toLowerCase().includes('time zone')) {
+ return `${splits[0].toLowerCase()}')`
+ }
+
+ // Handle json
+ if (value.toLowerCase().includes('json')) {
+ return splits[0].slice(1)
}
let temp = splits[0].slice(1).replace("'{", '{')
@@ -216,21 +218,21 @@ const unescapeLiteral = (value: string) => {
value.endsWith('bigint[]') ||
value.endsWith('smallint[]')
) {
- temp = temp.replaceAll('{', '[')
- temp = temp.replaceAll('}', ']')
+ temp = temp.replace(/{/g, '[')
+ temp = temp.replace(/}/g, ']')
return temp
} else {
const matches = temp.match(/\{([^{}]+)\}/g)
if (matches) {
const array = [...matches]
array.forEach((x) => {
- let _x = x.replaceAll('{', '{"')
- _x = _x.replaceAll('}', '"}')
- _x = _x.replaceAll(',', '","')
+ let _x = x.replace(/{/g, '{"')
+ _x = _x.replace(/}/g, '"}')
+ _x = _x.replace(/,/g, '","')
temp = temp.replace(x, _x)
})
- temp = temp.replaceAll('{', '[')
- temp = temp.replaceAll('}', ']')
+ temp = temp.replace(/{/g, '[')
+ temp = temp.replace(/}/g, ']')
}
return temp
}
diff --git a/studio/tests/components/Editor/ColumnEditor.utils.test.ts b/studio/tests/components/Editor/ColumnEditor.utils.test.ts
new file mode 100644
index 00000000000..9fc82d11801
--- /dev/null
+++ b/studio/tests/components/Editor/ColumnEditor.utils.test.ts
@@ -0,0 +1,44 @@
+import { unescapeLiteral } from 'components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils'
+
+describe('ColumnEditor.utils: unescapeLiteral', () => {
+ test('should return varchar properly', () => {
+ const mockInput = "'potato'::character varying"
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('potato')
+ })
+ test('should return text properly', () => {
+ const mockInput = "'tomato'::text"
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('tomato')
+ })
+ test('should return numerical arrays properly', () => {
+ const mockInput = "'{1,2,3,4}'::integer[]"
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('[1,2,3,4]')
+ })
+ test('should return string arrays properly', () => {
+ const mockInput = "'{apple,orange}'::text[]"
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('["apple","orange"]')
+ })
+ test('should return single word functions as they are', () => {
+ const mockInput = 'uuid_generate_v4()'
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual(mockInput)
+ })
+ test('should return functions beyond one word properly', () => {
+ const mockInput = "(now() AT TIME ZONE 'utc'::text)"
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual("(now() at time zone 'utc')")
+ })
+ test('should return json object properly', () => {
+ const mockInput = `'{\"version\": 10, \"dimensions\": {\"width\": 50, \"height\": 20}}'::jsonb`
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('{"version": 10, "dimensions": {"width": 50, "height": 20}}')
+ })
+ test('should return json array properly', () => {
+ const mockInput = `'[{\"version\": 10}, {\"version\": 11}]'::jsonb`
+ const value = unescapeLiteral(mockInput)
+ expect(value).toStrictEqual('[{"version": 10}, {"version": 11}]')
+ })
+})