Change sessions.id to a TEXT field to prevent type coercion

This commit is contained in:
Scott Nonnenberg 2018-11-19 17:38:55 -08:00
parent 20632b9581
commit bde1462159
2 changed files with 40 additions and 3 deletions

View File

@ -422,13 +422,50 @@ async function updateToSchemaVersion6(currentVersion, instance) {
console.log('updateToSchemaVersion6: success!');
}
async function updateToSchemaVersion7(currentVersion, instance) {
if (currentVersion >= 7) {
return;
}
console.log('updateToSchemaVersion7: starting...');
await instance.run('BEGIN TRANSACTION;');
// SQLite has been coercing our STRINGs into numbers, so we force it with TEXT
// We create a new table then copy the data into it, since we can't modify columns
await instance.run('DROP INDEX sessions_number;');
await instance.run('ALTER TABLE sessions RENAME TO sessions_old;');
await instance.run(
`CREATE TABLE sessions(
id TEXT PRIMARY KEY,
number TEXT,
json TEXT
);`
);
await instance.run(`CREATE INDEX sessions_number ON sessions (
number
) WHERE number IS NOT NULL;`);
await instance.run(`INSERT INTO sessions(id, number, json)
SELECT "+" || id, number, json FROM sessions_old;
`);
await instance.run('DROP TABLE sessions_old;');
await instance.run('PRAGMA schema_version = 7;');
await instance.run('COMMIT TRANSACTION;');
console.log('updateToSchemaVersion7: success!');
}
const SCHEMA_VERSIONS = [
updateToSchemaVersion1,
updateToSchemaVersion2,
updateToSchemaVersion3,
updateToSchemaVersion4,
// version 5 was dropped
() => null, // version 5 was dropped
updateToSchemaVersion6,
updateToSchemaVersion7,
];
async function updateSchema(instance) {

View File

@ -940,7 +940,7 @@ describe('SignalProtocolStore', () => {
describe('getDeviceIds', () => {
it('returns deviceIds for a number', async () => {
const testRecord = 'an opaque string';
const devices = [1, 2, 3].map(deviceId => {
const devices = [1, 2, 3, 10].map(deviceId => {
return [number, deviceId].join('.');
});
@ -951,7 +951,7 @@ describe('SignalProtocolStore', () => {
);
const deviceIds = await store.getDeviceIds(number);
assert.sameMembers(deviceIds, [1, 2, 3]);
assert.sameMembers(deviceIds, [1, 2, 3, 10]);
});
it('returns empty array for a number with no device ids', async () => {
const deviceIds = await store.getDeviceIds('foo');