Update 20241014084900_identity-multiple-auth-methods.ts

This commit is contained in:
Daniel Hougaard
2024-11-02 19:44:09 +04:00
parent 1cd17a451c
commit 860ebb73a9
@@ -2,7 +2,7 @@ import { Knex } from "knex";
import { TableName } from "../schemas"; import { TableName } from "../schemas";
const BATCH_SIZE = 10000; const BATCH_SIZE = 10_000;
export async function up(knex: Knex): Promise<void> { export async function up(knex: Knex): Promise<void> {
const hasAuthMethodColumnAccessToken = await knex.schema.hasColumn(TableName.IdentityAccessToken, "authMethod"); const hasAuthMethodColumnAccessToken = await knex.schema.hasColumn(TableName.IdentityAccessToken, "authMethod");
@@ -12,25 +12,11 @@ export async function up(knex: Knex): Promise<void> {
t.string("authMethod").nullable(); t.string("authMethod").nullable();
}); });
// Get total count of records to process let nullableAccessTokens = await knex(TableName.IdentityAccessToken).whereNull("authMethod").limit(BATCH_SIZE);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment let totalUpdated = 0;
// @ts-ignore because count is a string
const count = (await knex(TableName.IdentityAccessToken).count("id as count").first()) as { count: string };
if (!count) { do {
throw new Error("Failed to find count of identity access tokens"); const batchIds = nullableAccessTokens.map((token) => token.id);
}
const totalRecords = parseInt(count.count, 10);
// Process in batches
for (let offset = 0; offset < totalRecords; offset += BATCH_SIZE) {
// ! Get the current access tokens to process
// eslint-disable-next-line no-await-in-loop
const batchIds = await knex(TableName.IdentityAccessToken)
.select("id")
.limit(BATCH_SIZE)
.offset(offset)
.pluck("id");
// ! Update the auth method column in batches for the current batch // ! Update the auth method column in batches for the current batch
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@@ -46,10 +32,12 @@ export async function up(knex: Knex): Promise<void> {
.first() .first()
}); });
// Log progress // eslint-disable-next-line no-await-in-loop
// eslint-disable-next-line no-console nullableAccessTokens = await knex(TableName.IdentityAccessToken).whereNull("authMethod").limit(BATCH_SIZE);
console.log(`Processed ${Math.min(offset + BATCH_SIZE, totalRecords)} of ${totalRecords} records`);
} totalUpdated += batchIds.length;
console.log(`Updated ${batchIds.length} access tokens in batch <> Total updated: ${totalUpdated}`);
} while (nullableAccessTokens.length > 0);
// ! We delete all access tokens where the identity has no auth method set! // ! We delete all access tokens where the identity has no auth method set!
// ! Which means un-configured identities that for some reason have access tokens, will have their access tokens deleted. // ! Which means un-configured identities that for some reason have access tokens, will have their access tokens deleted.