Skip to content

Commit

Permalink
Migration that backfills form def names from xml titles
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Apr 20, 2021
1 parent 430e271 commit 679df70
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/model/frames/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Form extends Frame.define(
const version = versionText.orElse('');
const name = nameText.orNull();
const key = pubKey.map((k) => new Key({ public: k }));
return new Form.Partial({ xmlFormId, name }, { def: new Form.Def({ version, title: name }), key });
return new Form.Partial({ xmlFormId, name }, { def: new Form.Def({ version, name: name }), key });
});

return (typeof xml === 'string')
Expand Down Expand Up @@ -165,7 +165,7 @@ Form.Def = Frame.define(
'sha256', readable, 'draftToken', readable,
'enketoId', readable, 'createdAt',
'publishedAt', readable, 'xlsBlobId',
'title', readable
'name', readable
);
Form.Xml = Frame.define(into('xml'), 'xml');

Expand Down
28 changes: 25 additions & 3 deletions lib/model/migrations/20210415-01-add-title-to-form-def.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,31 @@
// including this file, may be copied, modified, propagated, or distributed
// except according to the terms contained in the LICENSE file.

const up = (db) => db.schema.table('form_defs', (fd) => {
fd.text('title');
});
const { Form } = require('../frames');

const up = async (db) => {
// All column "name" to form_defs to store the title of a form
// Most places in central, the name of a form is called the "name"
// and only in the XForm/XLSForm is it called "title", so we are going
// with calling it "name" everywhere in the code, even in the database.
await db.schema.table('form_defs', (fd) => {
fd.text('name')
});

await db.raw('ALTER TABLE form_defs DISABLE TRIGGER check_managed_key');

const work = [];
for await (const def of db.select('*').from('form_defs').stream()) {
const partial = await Form.fromXml(def.xml);
if (partial.def.title == null) continue;

const data = { name: partial.def.title };
work.push(db.update(data).into('form_defs').where({ id: def.id }));
}
await Promise.all(work);

await db.raw('ALTER TABLE form_defs ENABLE TRIGGER check_managed_key');
};

const down = (db) => db.schema.table('form_defs', (fd) => {
fd.dropColumn('title');
Expand Down
8 changes: 4 additions & 4 deletions lib/model/query/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const _createNew = (form, def, project, publish) => ({ oneFirst, Actees, Forms }
Actees.provision('form', project)
.then((actee) => oneFirst(sql`
with def as
(insert into form_defs ("formId", xml, "title", hash, sha, sha256, version, "keyId", "xlsBlobId", "draftToken", "createdAt", "publishedAt")
values (nextval(pg_get_serial_sequence('forms', 'id')), ${form.xml}, ${def.title}, ${def.hash}, ${def.sha}, ${def.sha256}, ${def.version}, ${def.keyId}, ${form.xls.xlsBlobId || null}, ${(publish !== true) ? generateToken() : null}, clock_timestamp(), ${(publish === true) ? sql`clock_timestamp()` : null})
(insert into form_defs ("formId", xml, name, hash, sha, sha256, version, "keyId", "xlsBlobId", "draftToken", "createdAt", "publishedAt")
values (nextval(pg_get_serial_sequence('forms', 'id')), ${form.xml}, ${def.name}, ${def.hash}, ${def.sha}, ${def.sha256}, ${def.version}, ${def.keyId}, ${form.xls.xlsBlobId || null}, ${(publish !== true) ? generateToken() : null}, clock_timestamp(), ${(publish === true) ? sql`clock_timestamp()` : null})
returning *),
form as
(insert into forms (id, name, "xmlFormId", state, "projectId", ${sql.identifier([ (publish === true) ? 'currentDefId' : 'draftDefId' ])}, "acteeId", "createdAt")
Expand Down Expand Up @@ -146,7 +146,7 @@ const publish = (form) => ({ Forms }) => {
if (form.draftDefId !== form.def.id) throw Problem.internal.unknown();

return Promise.all([
Forms._update(form, { currentDefId: form.draftDefId, draftDefId: null, name: form.def.title }),
Forms._update(form, { currentDefId: form.draftDefId, draftDefId: null, name: form.def.name }),
Forms._updateDef(form, { draftToken: null, enketoId: null, publishedAt: (new Date()).toISOString() })
])
.catch(Problem.translate(
Expand Down Expand Up @@ -292,7 +292,7 @@ ${extend|| sql`
left outer join (select id, "contentType" as "excelContentType" from blobs) as xls
on form_defs."xlsBlobId"=xls.id`}
where ${equals(options.condition)} and forms."deletedAt" is null
order by coalesce(name, "xmlFormId") asc`);
order by coalesce(forms.name, "xmlFormId") asc`);

const _getWithoutXml = extender(Form, Form.Def)(Form.Extended, Actor.into('createdBy'))(_getSql);
const _getWithXml = extender(Form, Form.Def, Form.Xml)(Form.Extended, Actor.into('createdBy'))(_getSql);
Expand Down

0 comments on commit 679df70

Please sign in to comment.