Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update backfill migration to also set label in dataReceived column #1048

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/model/migrations/20231002-01-add-conflict-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const up = async (db) => {
`);

// Sets the value for "dataReceived" and "baseVersion" for existing row
await db.raw('UPDATE entity_defs SET "dataReceived" = data, "baseVersion" = CASE WHEN version > 1 THEN version - 1 ELSE NULL END');
// eslint-disable-next-line quotes
await db.raw(`UPDATE entity_defs SET "dataReceived" = data || jsonb_build_object('label', "label"), "baseVersion" = CASE WHEN version > 1 THEN version - 1 ELSE NULL END`);

};

Expand Down
43 changes: 43 additions & 0 deletions test/integration/other/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,46 @@ describe('database migrations from 20230802: delete orphan submissions', functio

}));
});

describe.skip('database migration: 20231002-01-add-conflict-details.js', function test() {
this.timeout(20000);

it('should update dataReceived and baseVersion of existing entity defs', testServiceFullTrx(async (service, container) => {
await upToMigration('20231002-01-add-conflict-details.js', false);
await populateUsers(container);
await populateForms(container);

const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
.set('Content-Type', 'application/xml')
.send(testData.forms.simpleEntity)
.expect(200);

const dsId = await container.oneFirst(sql`select id from datasets limit 1`);

// Create the entity in the DB directly because application code has changed.
const newEntity = await container.one(sql`
INSERT INTO entities (uuid, "datasetId", "creatorId", "createdAt")
VALUES (${uuid()}, ${dsId}, 5, now())
RETURNING "id"`);

// Create two entity defs
await container.run(sql`
INSERT INTO entity_defs ("entityId", "createdAt", "creatorId", "current", "label", "data", "version")
VALUES (${newEntity.id}, now(), 5, false, 'some label', '{"foo": "bar"}', 1)`);

await container.run(sql`
INSERT INTO entity_defs ("entityId", "createdAt", "creatorId", "current", "label", "data", "version")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should version be specified for both defs? The baseVersion of the first def will be null after the migration, but I think its version should be 1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The column has a default value of 1, which I visually saw, and is how the baseVersion gets set to null.

VALUES (${newEntity.id}, now(), 5, true, 'other label', '{"x": "y", "a": "b"}', 2)`);

// run migration: fill in dataReceived and baseVersion based on existing version
await up();

const defs = await container.all(sql`select data, label, version, "baseVersion", "dataReceived" from entity_defs order by id`);
defs[0].dataReceived.should.eql({ foo: 'bar', label: 'some label' });
defs[0].should.have.property('baseVersion').which.is.eql(null);
defs[1].dataReceived.should.eql({ a: 'b', x: 'y', label: 'other label' });
defs[1].should.have.property('baseVersion').which.is.eql(1);
}));
});