Skip to content

Commit bd6c3ca

Browse files
author
mvtthew
committed
fixup code review & more test cases
1 parent 321508a commit bd6c3ca

5 files changed

Lines changed: 348 additions & 137 deletions

File tree

packages/dbml-core/__tests__/examples/exporter/postgres_exporter/output/function.out.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
CREATE OR REPLACE FUNCTION "public"."increment"(len_from integer, len_to integer)
1+
CREATE OR REPLACE FUNCTION "public"."increment"("len_from" integer, "len_to" integer)
22
RETURNS integer
33
LANGUAGE plpgsql
44
VOLATILE
5+
SECURITY INVOKER
56
AS $$
67
DECLARE
78
film_count INTEGER;
@@ -14,7 +15,7 @@ DECLARE
1415
END;
1516
$$;
1617

17-
CREATE OR REPLACE FUNCTION "public"."simple_add"(a integer, b integer)
18+
CREATE OR REPLACE FUNCTION "public"."simple_add"("a" integer, "b" integer)
1819
RETURNS integer
1920
LANGUAGE plpgsql
2021
IMMUTABLE

packages/dbml-core/src/export/PostgresExporter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,17 @@ class PostgresExporter {
556556
}
557557

558558
static exportFunctions (functionIds, model) {
559+
const toSqlType = (type) => (type || '').replace(/_/g, ' ');
559560
return functionIds.map((functionId) => {
560561
const fn = model.functions[functionId];
561562
const schema = fn.schemaName || 'public';
562-
const argList = (fn.args || []).map((a) => `${a.name} ${a.type}`).join(', ');
563-
const returnType = (fn.returns || 'void').replace(/_/g, ' ');
563+
const argList = (fn.args || []).map((a) => `"${a.name}" ${toSqlType(a.type)}`).join(', ');
564+
const returnType = toSqlType(fn.returns || 'void');
564565
let line = `CREATE OR REPLACE FUNCTION "${schema}"."${fn.name}"(${argList})`;
565566
line += `\nRETURNS ${returnType}`;
566567
line += `\nLANGUAGE ${fn.language || 'plpgsql'}`;
567568
line += `\n${(fn.behavior || 'volatile').toUpperCase()}`;
568-
if (fn.security === 'definer') line += '\nSECURITY DEFINER';
569+
line += `\nSECURITY ${(fn.security || 'invoker').toUpperCase()}`;
569570
line += '\nAS $$';
570571
line += `\n${(fn.body || '').trim()}`;
571572
line += '\n$$;\n';

packages/dbml-parse/__tests__/examples/interpreter/record/function.test.ts

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)