Skip to content

Commit

Permalink
export/submissions: remove raw SQL modification hack (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Nov 7, 2024
1 parent 52d6ed0 commit 7005a40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 119 deletions.
19 changes: 11 additions & 8 deletions lib/model/query/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// except according to the terms contained in the LICENSE file.

const config = require('config');
const { map } = require('ramda');
const { always, equals, identity, ifElse, map, pick, without } = require('ramda');
const { sql } = require('slonik');
const { Frame, table } = require('../frame');
const { Actor, Form, Submission } = require('../frames');
Expand Down Expand Up @@ -338,13 +338,16 @@ const _exportUnjoiner = unjoiner(Submission, Submission.Def, Submission.Xml, Sub
// TODO: this is a terrible hack to add some logic to one of our select fields. this is
// the /only/ place we need to do this in the entire codebase right now. so for now
// we just use the terrible hack.
const { raw } = require('slonik-sql-tag-raw');
const _exportFields = raw(_exportUnjoiner.fields.sql
.replace(',"submissions"."userAgent" as "submissions!userAgent"', '')
.replace(
'"submission_defs"."xml" as "submission_defs!xml"',
'(case when submission_defs."localKey" is null then submission_defs.xml end) as "submission_defs!xml"'
));
const xmlUnlessEncrypted = {
name: 'xml',
selectSql: tableAlias => sql`(case when ${sql.identifier([tableAlias, 'localKey'])} is null then ${sql.identifier([tableAlias, 'xml'])} end)`,
};
const cloneFrame = (frame, fieldMutator) => ({ ...pick(['from', 'to'], frame), fields: fieldMutator(frame.fields) });
const ExportSubmission = cloneFrame(Submission, without(['userAgent']));
const ExportSubmissionDef = cloneFrame(Submission.Def, map(ifElse(equals('xml'), always(xmlUnlessEncrypted), identity)));
const _exportFields = unjoiner(ExportSubmission, ExportSubmissionDef, Submission.Xml, Submission.Encryption,
Actor.alias('actors', 'submitter'), Frame.define(table('attachments'), 'present', 'expected'),
Frame.define(table('edits'), 'count'), Submission.Exports).fields;

const _export = (formId, draft, keyIds = [], options) => {
const encrypted = keyIds.length !== 0;
Expand Down
12 changes: 9 additions & 3 deletions lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ const unjoiner = (...frames) => {
const isOption = MaybeInstance instanceof Option;
const Instance = isOption ? MaybeInstance.get() : MaybeInstance;
for (const field of Instance.fields) {
const fullname = (Instance.from == null) ? sql.identifier([field]) : sql.identifier([Instance.from, field]);
const sqlname = (Instance.from == null) ? field : `${Instance.from}!${field}`;
fields.push(sql`${fullname} as ${sql.identifier([sqlname])}`);
const fieldName = field.name ?? field;
const sqlname = (Instance.from == null) ? fieldName : `${Instance.from}!${fieldName}`;
const sqlAlias = sql.identifier([sqlname]);
if (field.selectSql) {
fields.push(sql`${field.selectSql(Instance.from)} as ${sqlAlias}`);
} else {
const fullname = (Instance.from == null) ? sql.identifier([field]) : sql.identifier([Instance.from, field]);
fields.push(sql`${fullname} as ${sqlAlias}`);
}
unmap[sqlname] = Instance.to;
unprefix[sqlname] = field;
constructors[Instance.to] = isOption ? maybeConstruct(Instance) : construct(Instance);
Expand Down
107 changes: 0 additions & 107 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"sanitize-filename": "~1",
"semver": "^7.6.2",
"slonik": "npm:@getodk/[email protected]",
"slonik-sql-tag-raw": "npm:@getodk/[email protected]",
"tmp-promise": "~3",
"uuid": "^9.0.0",
"yauzl": "~2.10"
Expand Down

0 comments on commit 7005a40

Please sign in to comment.