mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-25 22:36:38 +00:00
Fix migration
This commit is contained in:
@@ -10,21 +10,33 @@ export default async function migration() {
|
|||||||
try {
|
try {
|
||||||
await db.execute(sql`BEGIN`);
|
await db.execute(sql`BEGIN`);
|
||||||
|
|
||||||
const webauthnCredentialsQuery = await db.execute(sql`SELECT "credentialId", "publicKey" FROM "webauthnCredentials"`);
|
const webauthnCredentialsQuery = await db.execute(sql`SELECT "credentialId", "publicKey", "userId", "signCount", "transports", "name", "lastUsed", "dateCreated" FROM "webauthnCredentials"`);
|
||||||
|
|
||||||
const webauthnCredentials = webauthnCredentialsQuery.rows as { credentialId: string; publicKey: string }[];
|
const webauthnCredentials = webauthnCredentialsQuery.rows as {
|
||||||
|
credentialId: string;
|
||||||
|
publicKey: string;
|
||||||
|
userId: string;
|
||||||
|
signCount: number;
|
||||||
|
transports: string | null;
|
||||||
|
name: string | null;
|
||||||
|
lastUsed: string;
|
||||||
|
dateCreated: string;
|
||||||
|
}[];
|
||||||
|
|
||||||
for (const webauthnCredential of webauthnCredentials) {
|
for (const webauthnCredential of webauthnCredentials) {
|
||||||
const credentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64')));
|
const newCredentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64')));
|
||||||
|
const newPublicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64')));
|
||||||
|
|
||||||
|
// Delete the old record
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
UPDATE "webauthnCredentials" SET "credentialId" = ${credentialId}
|
DELETE FROM "webauthnCredentials"
|
||||||
WHERE "credentialId" = ${webauthnCredential.credentialId}
|
WHERE "credentialId" = ${webauthnCredential.credentialId}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
const publicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64')));
|
// Insert the updated record with converted values
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
UPDATE "webauthnCredentials" SET "publicKey" = ${publicKey}
|
INSERT INTO "webauthnCredentials" ("credentialId", "publicKey", "userId", "signCount", "transports", "name", "lastUsed", "dateCreated")
|
||||||
WHERE "credentialId" = ${webauthnCredential.credentialId}
|
VALUES (${newCredentialId}, ${newPublicKey}, ${webauthnCredential.userId}, ${webauthnCredential.signCount}, ${webauthnCredential.transports}, ${webauthnCredential.name}, ${webauthnCredential.lastUsed}, ${webauthnCredential.dateCreated})
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,21 +13,31 @@ export default async function migration() {
|
|||||||
|
|
||||||
db.transaction(() => {
|
db.transaction(() => {
|
||||||
|
|
||||||
const webauthnCredentials = db.prepare(`SELECT credentialId, publicKey FROM 'webauthnCredentials'`).all() as {
|
const webauthnCredentials = db.prepare(`SELECT credentialId, publicKey, userId, signCount, transports, name, lastUsed, dateCreated FROM 'webauthnCredentials'`).all() as {
|
||||||
credentialId: string; publicKey: string
|
credentialId: string; publicKey: string; userId: string; signCount: number; transports: string | null; name: string | null; lastUsed: string; dateCreated: string;
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
for (const webauthnCredential of webauthnCredentials) {
|
for (const webauthnCredential of webauthnCredentials) {
|
||||||
const credentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64')));
|
const newCredentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64')));
|
||||||
db.prepare(
|
const newPublicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64')));
|
||||||
`UPDATE 'webauthnCredentials' SET 'credentialId' = ? WHERE 'credentialId' = ?`
|
|
||||||
).run(credentialId, webauthnCredential.credentialId);
|
// Delete the old record
|
||||||
|
db.prepare(`DELETE FROM 'webauthnCredentials' WHERE 'credentialId' = ?`).run(webauthnCredential.credentialId);
|
||||||
const publicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64')));
|
|
||||||
db.prepare(
|
// Insert the updated record with converted values
|
||||||
`UPDATE 'webauthnCredentials' SET 'publicKey' = ? WHERE 'credentialId' = ?`
|
db.prepare(
|
||||||
).run(publicKey, webauthnCredential.credentialId);
|
`INSERT INTO 'webauthnCredentials' (credentialId, publicKey, userId, signCount, transports, name, lastUsed, dateCreated) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
}
|
).run(
|
||||||
|
newCredentialId,
|
||||||
|
newPublicKey,
|
||||||
|
webauthnCredential.userId,
|
||||||
|
webauthnCredential.signCount,
|
||||||
|
webauthnCredential.transports,
|
||||||
|
webauthnCredential.name,
|
||||||
|
webauthnCredential.lastUsed,
|
||||||
|
webauthnCredential.dateCreated
|
||||||
|
);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
console.log(`${version} migration complete`);
|
console.log(`${version} migration complete`);
|
||||||
|
|||||||
Reference in New Issue
Block a user