feat: updated UserConfigDump functions to support any variant config dump for migration 34

pull/2971/head
William Grant 2 years ago
parent 22e9e6bb44
commit 88a87903cb

@ -11,9 +11,58 @@ import { CONVERSATION_PRIORITIES } from '../../../models/conversationAttributes'
import { MESSAGES_TABLE, toSqliteBoolean } from '../../database_utility';
import { fromHexToArray } from '../../../session/utils/String';
import { checkTargetMigration, hasDebugEnvVariable } from '../utils';
import { ConfigDumpRow, CONFIG_DUMP_TABLE } from '../../../types/sqlSharedTypes';
const targetVersion = 34;
function fetchConfigDumps(
db: BetterSqlite3.Database,
version: number,
userPubkeyhex: string,
variant: 'UserConfig' | 'ContactsConfig' | 'UserGroupsConfig' | 'ConvoInfoVolatileConfig'
): ConfigDumpRow | null {
checkTargetMigration(version, targetVersion);
const configWrapperDumps = db
.prepare(
`SELECT * FROM ${CONFIG_DUMP_TABLE} WHERE variant = $variant AND publicKey = $publicKey;`
)
.all({ variant, publicKey: userPubkeyhex }) as Array<ConfigDumpRow>;
if (!configWrapperDumps || !configWrapperDumps.length) {
return null;
}
// we can only have one dump with the current variants and our pubkey
return configWrapperDumps[0];
}
function writeConfigDumps(
db: BetterSqlite3.Database,
version: number,
userPubkeyhex: string,
variant: 'UserConfig' | 'ContactsConfig' | 'UserGroupsConfig' | 'ConvoInfoVolatileConfig',
dump: Uint8Array
) {
checkTargetMigration(version, targetVersion);
db.prepare(
`INSERT OR REPLACE INTO ${CONFIG_DUMP_TABLE} (
publicKey,
variant,
data
) values (
$publicKey,
$variant,
$data
);`
).run({
publicKey: userPubkeyhex,
variant,
data: dump,
});
}
/**
* This function returns a contactInfo for the wrapper to understand from the DB values.
* Created in this file so we can reuse it during the migration (node side), and from the renderer side
@ -169,5 +218,7 @@ function insertContactIntoContactWrapper(
}
export const V34 = {
fetchConfigDumps,
writeConfigDumps,
insertContactIntoContactWrapper,
};

@ -12,7 +12,7 @@ import {
ConversationAttributes,
} from '../../models/conversationAttributes';
import { fromHexToArray } from '../../session/utils/String';
import { CONFIG_DUMP_TABLE, ConfigDumpRow } from '../../types/sqlSharedTypes';
import { CONFIG_DUMP_TABLE } from '../../types/sqlSharedTypes';
import {
CLOSED_GROUP_V2_KEY_PAIRS_TABLE,
CONVERSATIONS_TABLE,
@ -1678,7 +1678,6 @@ function updateToSessionSchemaVersion34(currentVersion: number, db: BetterSqlite
const { privateEd25519, publicKeyHex } = loggedInUser.ourKeys;
// Conversation changes
// TODO can this be moved into libsession completely
db.prepare(
`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN expirationType TEXT DEFAULT "off";`
).run();
@ -1705,11 +1704,13 @@ function updateToSessionSchemaVersion34(currentVersion: number, db: BetterSqlite
const expirySeconds = ourConversation.expireTimer || 0;
// TODO update with Audric's snippet
// Get existing config wrapper dump and update it
const userConfigWrapperDump = db
.prepare(`SELECT * FROM ${CONFIG_DUMP_TABLE} WHERE variant = 'UserConfig';`)
.get() as ConfigDumpRow | undefined;
const userConfigWrapperDump = MIGRATION_HELPERS.V34.fetchConfigDumps(
db,
targetVersion,
publicKeyHex,
'UserConfig'
);
if (userConfigWrapperDump) {
const userConfigData = userConfigWrapperDump.data;
@ -1718,35 +1719,20 @@ function updateToSessionSchemaVersion34(currentVersion: number, db: BetterSqlite
userProfileWrapper.setNoteToSelfExpiry(expirySeconds);
// dump the user wrapper content and save it to the DB
const userDump = userProfileWrapper.dump();
const configDumpInfo = db
.prepare(
`INSERT OR REPLACE INTO ${CONFIG_DUMP_TABLE} (
publicKey,
variant,
data
) values (
$publicKey,
$variant,
$data
);`
)
.run({
publicKey: publicKeyHex,
variant: 'UserConfig',
data: userDump,
});
MIGRATION_HELPERS.V34.writeConfigDumps(
db,
targetVersion,
publicKeyHex,
'UserConfig',
userProfileWrapper.dump()
);
// TODO Cleanup logging
console.log(
'===================== userConfigWrapperDump configDumpInfo',
configDumpInfo,
'======================='
'===================== user config wrapper dump updated ======================='
);
} else {
console.log(
'===================== userConfigWrapperDump not found ======================='
'===================== user config wrapper dump not found ======================='
);
}
}
@ -1773,14 +1759,20 @@ function updateToSessionSchemaVersion34(currentVersion: number, db: BetterSqlite
if (isArray(contactsToUpdateInWrapper) && contactsToUpdateInWrapper.length) {
const blockedNumbers = getBlockedNumbersDuringMigration(db);
const contactsWrapperDump = db
.prepare(`SELECT * FROM ${CONFIG_DUMP_TABLE} WHERE variant = 'ContactConfig';`)
.get() as ConfigDumpRow | undefined;
const volatileInfoConfigWrapperDump = db
.prepare(
`SELECT * FROM ${CONFIG_DUMP_TABLE} WHERE variant = 'ConvoInfoVolatileConfig';`
)
.get() as ConfigDumpRow | undefined;
// Get existing config wrapper dumps and update them
const contactsWrapperDump = MIGRATION_HELPERS.V34.fetchConfigDumps(
db,
targetVersion,
publicKeyHex,
'ContactsConfig'
);
const volatileInfoConfigWrapperDump = MIGRATION_HELPERS.V34.fetchConfigDumps(
db,
targetVersion,
publicKeyHex,
'ConvoInfoVolatileConfig'
);
if (contactsWrapperDump && volatileInfoConfigWrapperDump) {
const contactsData = contactsWrapperDump.data;
@ -1813,61 +1805,34 @@ function updateToSessionSchemaVersion34(currentVersion: number, db: BetterSqlite
'===================== Done with contact updating ======================='
);
// dump the user wrapper content and save it to the DB
const contactsDump = contactsConfigWrapper.dump();
const contactsDumpInfo = db
.prepare(
`INSERT OR REPLACE INTO ${CONFIG_DUMP_TABLE} (
publicKey,
variant,
data
) values (
$publicKey,
$variant,
$data
);`
)
.run({
publicKey: publicKeyHex,
variant: 'ContactConfig',
data: contactsDump,
});
// TODO Cleanup logging
// dump the wrapper content and save it to the DB
MIGRATION_HELPERS.V34.writeConfigDumps(
db,
targetVersion,
publicKeyHex,
'ContactsConfig',
contactsConfigWrapper.dump()
);
console.log(
'===================== contactsConfigWrapper contactsDumpInfo',
contactsDumpInfo,
'======================='
'===================== contacts config wrapper dump updated ======================='
);
// dump the wrapper content and save it to the DB
MIGRATION_HELPERS.V34.writeConfigDumps(
db,
targetVersion,
publicKeyHex,
'ConvoInfoVolatileConfig',
volatileInfoConfigWrapper.dump()
);
const volatileInfoConfigDump = volatileInfoConfigWrapper.dump();
const volatileInfoConfigDumpInfo = db
.prepare(
`INSERT OR REPLACE INTO ${CONFIG_DUMP_TABLE} (
publicKey,
variant,
data
) values (
$publicKey,
$variant,
$data
);`
)
.run({
publicKey: publicKeyHex,
variant: 'ConvoInfoVolatileConfig',
data: volatileInfoConfigDump,
});
// TODO Cleanup logging
console.log(
'===================== volatileInfoConfigWrapper volatileInfoConfigDumpInfo',
volatileInfoConfigDumpInfo,
'======================='
'===================== convo info volatile config wrapper dump updated ======================='
);
} else {
console.log(
'===================== contactsWrapperDump or volatileInfoConfigWrapperDump was not found ======================='
'===================== contacts config or convo info volatile config wrapper dump found ======================='
);
}
}

Loading…
Cancel
Save