Skip to content

Commit

Permalink
Fix runtime error during SQL migration
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Oct 27, 2021
1 parent 30078ce commit 246583d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
16 changes: 7 additions & 9 deletions ts/sql/migrations/43-gv2-uuid.ts
Expand Up @@ -5,7 +5,6 @@ import type { Database } from 'better-sqlite3';
import { omit } from 'lodash';

import type { LoggerType } from '../../types/Logging';
import { UUID } from '../../types/UUID';
import type { UUIDStringType } from '../../types/UUID';
import { isNotNil } from '../../util/isNotNil';
import { assert } from '../../util/assert';
Expand Down Expand Up @@ -265,11 +264,11 @@ export default function updateToSchemaVersion43(
}
changedDetails = true;

let newValue: UUIDStringType | null = getConversationUuid.get({
const newValue: UUIDStringType | null = getConversationUuid.get({
conversationId: oldValue,
});
if (key === 'inviter') {
newValue = newValue ?? UUID.cast(oldValue);
if (key === 'inviter' && !newValue) {
continue;
}
if (!newValue) {
logger.warn(
Expand Down Expand Up @@ -302,12 +301,11 @@ export default function updateToSchemaVersion43(
}

if (sourceUuid) {
const newValue: UUIDStringType =
getConversationUuid.get({
conversationId: sourceUuid,
}) ?? UUID.cast(sourceUuid);
const newValue: UUIDStringType | null = getConversationUuid.get({
conversationId: sourceUuid,
});

if (newValue !== sourceUuid) {
if (newValue) {
result = {
...result,
sourceUuid: newValue,
Expand Down
27 changes: 27 additions & 0 deletions ts/test-node/sql_migrations_test.ts
Expand Up @@ -769,5 +769,32 @@ describe('SQL migrations test', () => {
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',
});
});
});
});

0 comments on commit 246583d

Please sign in to comment.