Skip to content

Commit a8334f8

Browse files
committed
change suffix and exclude deleted forms/projects
1 parent d1264ca commit a8334f8

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

lib/model/migrations/20241010-01-schedule-entity-form-upgrade.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
// except according to the terms contained in the LICENSE file.
99

1010
const up = (db) => db.raw(`
11-
INSERT INTO audits ("action", "acteeId")
12-
SELECT 'upgrade.process.form.entities_version', f."acteeId"
13-
FROM forms f
14-
JOIN form_defs fd ON f."id" = fd."formId"
11+
INSERT INTO audits ("action", "acteeId", "loggedAt", "details")
12+
SELECT 'upgrade.process.form.entities_version', forms."acteeId", clock_timestamp(),
13+
'{"upgrade": "As part of upgrading Central to v2024.3, this form is being updated to the latest entities-version spec."}'
14+
FROM forms
15+
JOIN form_defs fd ON forms."id" = fd."formId"
1516
JOIN dataset_form_defs dfd ON fd."id" = dfd."formDefId"
17+
JOIN projects ON projects."id" = forms."projectId"
1618
WHERE dfd."actions" @> '["update"]'
17-
GROUP BY f."acteeId";
19+
AND forms."deletedAt" IS NULL
20+
AND projects."deletedAt" IS NULL
21+
GROUP BY forms."acteeId";
1822
`);
1923

2024
const down = () => {};

lib/model/query/forms.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ const _createNewDef = (partial, form, publish, data) => ({ one }) =>
201201
// ===========
202202
// partial: Partial form definition of the new version
203203
// form: Form frame of existing form
204-
// publish: set true if you want new version to be published (only used by setManagedKey, everywhere else calls publish() explicitly)
204+
// publish: set true if you want new version to be published (used infrequently)
205+
// One example where publish=true is in setManagedKey, which updates the form XML.
206+
// Most other situations call publish() explicitly.
205207
// duplicating: set true if copying form definition from previously uploaded definition, in that cases we don't check for structural change
206208
// as user has already been warned otherwise set false
207209
const createVersion = (partial, form, publish, duplicating = false) => async ({ Datasets, FormAttachments, Forms, Keys }) => {

lib/worker/form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ const updateEntitiesVersion = async ({ Forms }, event) => {
4646
const { projectId, xmlFormId } = await Forms.getByActeeIdForUpdate(event.acteeId).then(o => o.get());
4747
const publishedVersion = await Forms.getByProjectAndXmlFormId(projectId, xmlFormId, true, Form.PublishedVersion).then(o => o.orNull());
4848
if (publishedVersion && publishedVersion.currentDefId != null) {
49-
const partial = await publishedVersion.withUpgradeEntityVersion('2023.1.0', '2024.1.0', '_upgrade');
49+
const partial = await publishedVersion.withUpgradeEntityVersion('2023.1.0', '2024.1.0', '[upgrade]');
5050
if (partial != null)
5151
await Forms.createVersion(partial, publishedVersion, true, true);
5252
}
5353

5454
const draftVersion = await Forms.getByProjectAndXmlFormId(projectId, xmlFormId, true, Form.DraftVersion).then(o => o.orNull());
5555
if (draftVersion && draftVersion.draftDefId != null) {
56-
const partial = await draftVersion.withUpgradeEntityVersion('2023.1.0', '2024.1.0', '_upgrade');
56+
const partial = await draftVersion.withUpgradeEntityVersion('2023.1.0', '2024.1.0', '[upgrade]');
5757
// update xml and version in place
5858
if (partial != null)
5959
await Forms._updateDef(draftVersion.def, { xml: partial.xml, version: partial.def.version });

test/integration/other/form-entities-version.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const upgradedUpdateEntity = `<?xml version="1.0"?>
1010
<h:head>
1111
<model entities:entities-version="2024.1.0">
1212
<instance>
13-
<data id="updateEntity" orx:version="1.0_upgrade">
13+
<data id="updateEntity" orx:version="1.0[upgrade]">
1414
<name/>
1515
<age/>
1616
<hometown/>
@@ -48,7 +48,7 @@ describe('Update / migrate entities-version within form', () => {
4848
await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
4949
.then(({ body }) => {
5050
body.length.should.equal(2);
51-
body[0].version.should.equal('1.0_upgrade');
51+
body[0].version.should.equal('1.0[upgrade]');
5252
body[1].version.should.equal('1.0');
5353
});
5454

@@ -76,7 +76,7 @@ describe('Update / migrate entities-version within form', () => {
7676
// The version on the draft does change even though it is updated in place
7777
await asAlice.get('/v1/projects/1/forms/updateEntity/draft')
7878
.then(({ body }) => {
79-
body.version.should.equal('1.0_upgrade');
79+
body.version.should.equal('1.0[upgrade]');
8080
});
8181

8282
await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
@@ -111,13 +111,13 @@ describe('Update / migrate entities-version within form', () => {
111111
// The version on the draft does change even though it is updated in place
112112
await asAlice.get('/v1/projects/1/forms/updateEntity/draft')
113113
.then(({ body }) => {
114-
body.version.should.equal('1.0_upgrade');
114+
body.version.should.equal('1.0[upgrade]');
115115
});
116116

117117
await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
118118
.then(({ body }) => {
119119
body.length.should.equal(2);
120-
body[0].version.should.equal('1.0_upgrade');
120+
body[0].version.should.equal('1.0[upgrade]');
121121
body[1].version.should.equal('1.0');
122122
});
123123

@@ -162,7 +162,7 @@ describe('Update / migrate entities-version within form', () => {
162162

163163
await asAlice.get('/v1/projects/1/forms/updateEntity/versions')
164164
.then(({ body }) => {
165-
body.map(f => f.version).should.eql([ '3.0_upgrade', '3.0', '2.0', '1.0' ]);
165+
body.map(f => f.version).should.eql([ '3.0[upgrade]', '3.0', '2.0', '1.0' ]);
166166
});
167167
}));
168168

@@ -274,15 +274,15 @@ describe('Update / migrate entities-version within form', () => {
274274
await asAlice.get('/v1/projects/1/forms/withAttachments.xml')
275275
.then(({ text }) => {
276276
text.includes('entities:entities-version="2024.1.0"').should.equal(true);
277-
text.includes('version="_upgrade"').should.equal(true);
277+
text.includes('version="[upgrade]"').should.equal(true);
278278
text.includes('trunkVersion="" branchId=""').should.equal(true);
279279
});
280280

281281
// Check form xml (draft)
282282
await asAlice.get('/v1/projects/1/forms/withAttachments/draft.xml')
283283
.then(({ text }) => {
284284
text.includes('entities:entities-version="2024.1.0"').should.equal(true);
285-
text.includes('version="_upgrade"').should.equal(true);
285+
text.includes('version="[upgrade]"').should.equal(true);
286286
text.includes('trunkVersion="" branchId=""').should.equal(true);
287287
});
288288

test/integration/other/migrations.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,29 @@ testMigration('20240914-02-remove-orphaned-client-audits.js', () => {
10941094
.send(testData.forms.simpleEntity)
10951095
.expect(200);
10961096

1097+
// Deleted forms and projects
1098+
// Upload another form that needs updating but will be deleted
1099+
await asAlice.post('/v1/projects/1/forms')
1100+
.send(testData.forms.updateEntity.replace('id="updateEntity"', 'id="updateEntityDeleted"'))
1101+
.expect(200);
1102+
1103+
await asAlice.delete('/v1/projects/1/forms/updateEntityDeleted')
1104+
.expect(200);
1105+
1106+
// Create a new project
1107+
const newProjectId = await asAlice.post('/v1/projects')
1108+
.send({ name: 'NewDeletedProject' })
1109+
.expect(200)
1110+
.then(({ body }) => body.id);
1111+
1112+
// Upload a form that needs updating to the new project
1113+
await asAlice.post(`/v1/projects/${newProjectId}/forms?publish=true`)
1114+
.send(testData.forms.updateEntity)
1115+
.expect(200);
1116+
1117+
// Delete the new project
1118+
await asAlice.delete(`/v1/projects/${newProjectId}`);
1119+
10971120
// Mark forms for upgrade
10981121
await up();
10991122

@@ -1111,7 +1134,7 @@ testMigration('20240914-02-remove-orphaned-client-audits.js', () => {
11111134
<h:head>
11121135
<model entities:entities-version="2024.1.0">
11131136
<instance>
1114-
<data id="updateEntity" orx:version="2.0_upgrade">
1137+
<data id="updateEntity" orx:version="2.0[upgrade]">
11151138
<name/>
11161139
<age/>
11171140
<hometown/>

test/unit/data/schema.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ describe('form schema', () => {
20892089

20902090
describe('updateEntityForm', () => {
20912091
it('should change version 2023->2024, add trunkVersion, and add branchId', (async () => {
2092-
const result = await updateEntityForm(testData.forms.updateEntity, '2023.1.0', '2024.1.0', '_upgrade');
2092+
const result = await updateEntityForm(testData.forms.updateEntity, '2023.1.0', '2024.1.0', '[upgrade]');
20932093
// entities-version has been updated
20942094
// version has suffix
20952095
// trunkVersion and branchId are present
@@ -2098,7 +2098,7 @@ describe('form schema', () => {
20982098
<h:head>
20992099
<model entities:entities-version="2024.1.0">
21002100
<instance>
2101-
<data id="updateEntity" orx:version="1.0_upgrade">
2101+
<data id="updateEntity" orx:version="1.0[upgrade]">
21022102
<name/>
21032103
<age/>
21042104
<hometown/>
@@ -2117,11 +2117,11 @@ describe('form schema', () => {
21172117
}));
21182118

21192119
it('should not alter a version 2022.1.0 form when the old version to replace is 2023.1.0', (async () => {
2120-
const result = await updateEntityForm(testData.forms.simpleEntity, '2023.1.0', '2024.1.0', '_upgrade');
2120+
const result = await updateEntityForm(testData.forms.simpleEntity, '2023.1.0', '2024.1.0', '[upgrade]');
21212121
result.should.equal(testData.forms.simpleEntity);
21222122
}));
21232123
it('should not alter a version 2024.1.0 form when the old version to replace is 2023.1.0', (async () => {
2124-
const result = await updateEntityForm(testData.forms.offlineEntity, '2023.1.0', '2024.1.0', '_upgrade');
2124+
const result = await updateEntityForm(testData.forms.offlineEntity, '2023.1.0', '2024.1.0', '[upgrade]');
21252125
result.should.equal(testData.forms.offlineEntity);
21262126
}));
21272127
});

0 commit comments

Comments
 (0)