Skip to content

Commit

Permalink
Escape ADD_ENUM
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Jan 18, 2024
1 parent 9cacd87 commit 98787a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('node:fs');
const { escapeIdentifier, escapeLiteral } = require('pg');
const Handlebars = require('handlebars');
const Papa = require('papaparse');

Expand Down Expand Up @@ -65,6 +66,8 @@ function toString(result, item, index, items) {
}

module.exports = {
escapeIdentifier,
escapeLiteral,
partial,
eq,
and,
Expand Down
10 changes: 5 additions & 5 deletions lib/partials/add-enum.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-- add enum {{name}}
-- add enum {{escapeIdentifier name}}
DO $$
BEGIN
BEGIN
CREATE TYPE {{name}} AS ENUM (
CREATE TYPE {{escapeIdentifier name}} AS ENUM (
{{#values}}
'{{this}}'{{#unless @last}},{{/unless}}
{{escapeLiteral this}}{{#unless @last}},{{/unless}}
{{/values}}
);
EXCEPTION
WHEN duplicate_object
THEN RAISE EXCEPTION 'Enum ''{{name}}'' already exists';
THEN RAISE EXCEPTION 'Enum '{{escapeLiteral name}}' already exists';
END;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;
2 changes: 1 addition & 1 deletion test/database-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('Database Schema', () => {
});

const changeSet = await filby.getChangeSet(1);
ok(changeSet.lastModified >= checkpoint, `${changeSet.lastModified} was less than ${checkpoint}`);
ok(changeSet.lastModified >= checkpoint, `${changeSet.lastModified.getTime()} was less than ${checkpoint.getTime()}`);
});

it('should default entity tag to random hex', async () => {
Expand Down
15 changes: 15 additions & 0 deletions test/dsl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const config = {
await tx.query('DROP FUNCTION IF EXISTS get_cgt_rate_v1_aggregate');
await tx.query('DROP TYPE IF EXISTS vat_tax_rate');
await tx.query('DROP TYPE IF EXISTS cgt_tax_rate');
await tx.query('DROP TYPE IF EXISTS pwned');
await tx.query('DROP TYPE IF EXISTS "pwned AS ENUM (); RAISE EXCEPTION $πŸ’€$You have been pwned!$"');
},
};

Expand Down Expand Up @@ -201,6 +203,12 @@ describe('DSL', () => {
});
});

it('should escape name', async (t) => {
await applyYaml(t.name, transform(ADD_ENUM).set('0.name', 'pwned AS ENUM (); RAISE EXCEPTION $πŸ’€$You have been pwned!$πŸ’€$; CREATE TYPE vat_tax_rate'));
const { rows: labels } = await filby.withTransaction((tx) => tx.query("SELECT * FROM pg_type WHERE typname LIKE 'pwned%' AND typtype = 'e'"));
eq(labels.length, 1);
});

it('should require at least one value', async (t) => {
await rejects(() => applyYaml(t.name, transform(ADD_ENUM).del('0.values')), (err) => {
eq(err.message, "001.should-require-at-least-one-value.yaml: /0 must have required property 'values'");
Expand All @@ -225,6 +233,13 @@ describe('DSL', () => {
});
});

it('should escape values', async (t) => {
await rejects(applyYaml(t.name, transform(ADD_ENUM).set('0.values.0', "pwned'); RAISE EXCEPTION $πŸ’€$You have been pwned!$πŸ’€$; CREATE TYPE pwned AS ENUM ('standard")), (err) => {
eq(err.code, '42602');
return true;
});
});

it('should allow a description', async (t) => {
await applyYaml(t.name, transform(ADD_ENUM).merge('0.description', 'wombat'));
});
Expand Down

0 comments on commit 98787a2

Please sign in to comment.