Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migration in loop due to cornercase #2743

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Knex } from "knex";

import { TableName } from "../schemas";

const BATCH_SIZE = 30_000;
const BATCH_SIZE = 10_000;

export async function up(knex: Knex): Promise<void> {
const hasAuthMethodColumnAccessToken = await knex.schema.hasColumn(TableName.IdentityAccessToken, "authMethod");
Expand All @@ -12,7 +12,25 @@ export async function up(knex: Knex): Promise<void> {
t.string("authMethod").nullable();
});

let nullableAccessTokens = await knex(TableName.IdentityAccessToken).whereNull("authMethod").limit(BATCH_SIZE);
// first we remove identities without auth method that is unused
let nullableAuthMethodIdentities = [];
do {
const findNullableAuthMethodIdentities = knex(TableName.Identity)
.whereNull("authMethod")
.limit(BATCH_SIZE)
.select("id");

// eslint-disable-next-line no-await-in-loop
nullableAuthMethodIdentities = await knex(TableName.Identity)
.whereIn("id", findNullableAuthMethodIdentities)
.del()
.returning("id");
} while (nullableAuthMethodIdentities.length > 0);

let nullableAccessTokens = await knex(TableName.IdentityAccessToken)
.whereNull("authMethod")
.limit(BATCH_SIZE)
.select("id");
let totalUpdated = 0;

do {
Expand All @@ -33,7 +51,10 @@ export async function up(knex: Knex): Promise<void> {
});

// eslint-disable-next-line no-await-in-loop
nullableAccessTokens = await knex(TableName.IdentityAccessToken).whereNull("authMethod").limit(BATCH_SIZE);
nullableAccessTokens = await knex(TableName.IdentityAccessToken)
.whereNull("authMethod")
.limit(BATCH_SIZE)
.select("id");

totalUpdated += batchIds.length;
console.log(`Updated ${batchIds.length} access tokens in batch <> Total updated: ${totalUpdated}`);
Expand Down
Loading