Fix runtime error during SQL migration

This commit is contained in:
Fedor Indutny 2021-10-27 10:15:03 -07:00 committed by GitHub
parent 30078ce3aa
commit 246583d274
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View file

@ -5,7 +5,6 @@ import type { Database } from 'better-sqlite3';
import { omit } from 'lodash'; import { omit } from 'lodash';
import type { LoggerType } from '../../types/Logging'; import type { LoggerType } from '../../types/Logging';
import { UUID } from '../../types/UUID';
import type { UUIDStringType } from '../../types/UUID'; import type { UUIDStringType } from '../../types/UUID';
import { isNotNil } from '../../util/isNotNil'; import { isNotNil } from '../../util/isNotNil';
import { assert } from '../../util/assert'; import { assert } from '../../util/assert';
@ -265,11 +264,11 @@ export default function updateToSchemaVersion43(
} }
changedDetails = true; changedDetails = true;
let newValue: UUIDStringType | null = getConversationUuid.get({ const newValue: UUIDStringType | null = getConversationUuid.get({
conversationId: oldValue, conversationId: oldValue,
}); });
if (key === 'inviter') { if (key === 'inviter' && !newValue) {
newValue = newValue ?? UUID.cast(oldValue); continue;
} }
if (!newValue) { if (!newValue) {
logger.warn( logger.warn(
@ -302,12 +301,11 @@ export default function updateToSchemaVersion43(
} }
if (sourceUuid) { if (sourceUuid) {
const newValue: UUIDStringType = const newValue: UUIDStringType | null = getConversationUuid.get({
getConversationUuid.get({ conversationId: sourceUuid,
conversationId: sourceUuid, });
}) ?? UUID.cast(sourceUuid);
if (newValue !== sourceUuid) { if (newValue) {
result = { result = {
...result, ...result,
sourceUuid: newValue, sourceUuid: newValue,

View file

@ -769,5 +769,32 @@ describe('SQL migrations test', () => {
sourceUuid: UUID_A, sourceUuid: UUID_A,
}); });
}); });
it('should not fail on invalid UUIDs', () => {
updateToVersion(42);
db.exec(
`
INSERT INTO messages
(id, json)
VALUES
('m', '${JSON.stringify({
id: 'm',
sourceUuid: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
})}');
`
);
updateToVersion(43);
const { json: messageMJSON } = db
.prepare('SELECT json FROM messages WHERE id = "m"')
.get();
assert.deepStrictEqual(JSON.parse(messageMJSON), {
id: 'm',
sourceUuid: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
});
});
}); });
}); });