Skip to content

Commit b17c553

Browse files
committed
remove unnecessary custom postgres function
1 parent f29ca02 commit b17c553

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

packages/host/app/lib/sqlite-adapter.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,14 @@ export default class SQLiteAdapter implements DBAdapter {
167167
})
168168
.replace(/ANY_VALUE\(([^)]*)\)/g, '$1')
169169
.replace(/CROSS JOIN LATERAL/g, 'CROSS JOIN')
170-
.replace(/jsonb_array_each\(/g, 'json_each(')
170+
.replace(/jsonb_array_elements_text\(/g, 'json_each(')
171171
.replace(/jsonb_tree\(/g, 'json_tree(')
172+
.replace(/([^\s]+\s[^\s]+)_array_element/g, (match, group) => {
173+
if (group.startsWith('as ')) {
174+
return match;
175+
}
176+
return `${match}.value`;
177+
})
172178
.replace(/\.text_value/g, '.value')
173179
.replace(/\.jsonb_value/g, '.value')
174180
.replace(/= 'null'::jsonb/g, 'IS NULL')

packages/realm-server/migrations/1712771547705_initial.js

-16
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ exports.up = (pgm) => {
6060
});
6161
pgm.sql('ALTER TABLE jobs SET UNLOGGED');
6262

63-
pgm.sql(`
64-
CREATE OR REPLACE FUNCTION jsonb_array_each(data JSONB)
65-
RETURNS TABLE (index_text INTEGER, text_value TEXT) AS
66-
$$
67-
BEGIN
68-
RETURN QUERY
69-
SELECT
70-
index::INTEGER,
71-
value::TEXT
72-
FROM
73-
jsonb_array_elements_text(data) WITH ORDINALITY AS arr(value, index);
74-
END;
75-
$$
76-
LANGUAGE PLPGSQL;
77-
`);
78-
7963
pgm.sql(`
8064
CREATE OR REPLACE FUNCTION jsonb_tree(data JSONB, root_path TEXT DEFAULT NULL)
8165
RETURNS TABLE (fullkey TEXT, jsonb_value JSONB, text_value TEXT, level INT) AS

packages/runtime-common/indexer/client.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ export class IndexerDBClient {
171171
`SELECT card_url
172172
FROM
173173
indexed_cards as i
174-
CROSS JOIN LATERAL jsonb_array_each(i.deps) as deps_each
174+
CROSS JOIN LATERAL jsonb_array_elements_text(i.deps) as deps_array_element
175175
INNER JOIN realm_versions r ON i.realm_url = r.realm_url
176-
WHERE
177-
deps_each.text_value =`,
176+
WHERE deps_array_element =`,
178177
param(cardId),
179178
'AND',
180179
...realmVersionExpression({ useWorkInProgressIndex: true }),
@@ -453,13 +452,13 @@ export class IndexerDBClient {
453452
// SELECT card_url, pristine_doc
454453
// FROM
455454
// indexed_cards,
456-
// CROSS JOIN LATERAL jsonb_array_each(types) as types0_each,
455+
// CROSS JOIN LATERAL jsonb_array_elements_text(types) as types0_array_element
457456
// -- This json_tree was derived by this handler:
458457
// CROSS JOIN LATERAL jsonb_tree(search_doc, '$.friends') as friends1_tree
459458
// WHERE
460459
// ( ( is_deleted = FALSE OR is_deleted IS NULL ) )
461460
// AND (
462-
// ( types0_each.text_value = $1 )
461+
// ( types0_array_element = $1 )
463462
// AND (
464463
// ( friends1_tree.text_value = $2 )
465464
// AND
@@ -728,7 +727,7 @@ export class IndexerDBClient {
728727
let key = `tree_${column}_${path}`;
729728
let { name } = tableValuedFunctions.get(key) ?? {};
730729
if (!name) {
731-
name = `${field}${nonce++}_${element.kind.split('-').pop()!}`;
730+
name = `${field}${nonce++}_tree`;
732731
let absolutePath = path === '$' ? '$' : `$.${path}`;
733732

734733
tableValuedFunctions.set(key, {
@@ -742,14 +741,14 @@ export class IndexerDBClient {
742741
let key = `each_${column}`;
743742
let { name } = tableValuedFunctions.get(key) ?? {};
744743
if (!name) {
745-
name = `${column}${nonce++}_${element.kind.split('-').pop()!}`;
744+
name = `${column}${nonce++}_array_element`;
746745

747746
tableValuedFunctions.set(key, {
748747
name,
749-
fn: `jsonb_array_each(${column}) as ${name}`,
748+
fn: `jsonb_array_elements_text(${column}) as ${name}`,
750749
});
751750
}
752-
return `${name}.text_value`;
751+
return name;
753752
} else {
754753
throw assertNever(element);
755754
}

0 commit comments

Comments
 (0)