Skip to content

Commit

Permalink
fix(core): Add down method to AddApiKeysTable1724951148974 migrat…
Browse files Browse the repository at this point in the history
…ion (no-changelog) (#11118)
  • Loading branch information
RicardoE105 authored Oct 8, 2024
1 parent 2161ff1 commit 1ca1494
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ApiKey } from '@/databases/entities/api-key';
import type { MigrationContext } from '@/databases/types';
import type { MigrationContext, ReversibleMigration } from '@/databases/types';
import { generateNanoId } from '@/databases/utils/generators';

export class AddApiKeysTable1724951148974 {
export class AddApiKeysTable1724951148974 implements ReversibleMigration {
async up({
queryRunner,
escape,
Expand Down Expand Up @@ -55,4 +55,55 @@ export class AddApiKeysTable1724951148974 {
// Drop apiKey column on user's table
await queryRunner.query(`ALTER TABLE ${userTable} DROP COLUMN ${apiKeyColumn};`);
}

async down({
queryRunner,
runQuery,
schemaBuilder: { dropTable, addColumns, createIndex, column },
escape,
isMysql,
}: MigrationContext) {
const userTable = escape.tableName('user');
const userApiKeysTable = escape.tableName('user_api_keys');
const apiKeyColumn = escape.columnName('apiKey');
const userIdColumn = escape.columnName('userId');
const idColumn = escape.columnName('id');
const createdAtColumn = escape.columnName('createdAt');

await addColumns('user', [column('apiKey').varchar()]);

await createIndex('user', ['apiKey'], true);

const queryToGetUsersApiKeys = isMysql
? `
SELECT ${userIdColumn},
${apiKeyColumn},
${createdAtColumn}
FROM ${userApiKeysTable} u
WHERE ${createdAtColumn} = (SELECT Min(${createdAtColumn})
FROM ${userApiKeysTable}
WHERE ${userIdColumn} = u.${userIdColumn});`
: `
SELECT DISTINCT ON
(${userIdColumn}) ${userIdColumn},
${apiKeyColumn}, ${createdAtColumn}
FROM ${userApiKeysTable}
ORDER BY ${userIdColumn}, ${createdAtColumn} ASC;`;

const oldestApiKeysPerUser = (await queryRunner.query(queryToGetUsersApiKeys)) as Array<
Partial<ApiKey>
>;

await Promise.all(
oldestApiKeysPerUser.map(
async (user: { userId: string; apiKey: string }) =>
await runQuery(
`UPDATE ${userTable} SET ${apiKeyColumn} = :apiKey WHERE ${idColumn} = :userId`,
user,
),
),
);

await dropTable('user_api_keys');
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { ApiKey } from '@/databases/entities/api-key';
import type { MigrationContext } from '@/databases/types';
import type { MigrationContext, ReversibleMigration } from '@/databases/types';
import { generateNanoId } from '@/databases/utils/generators';

export class AddApiKeysTable1724951148974 {
export class AddApiKeysTable1724951148974 implements ReversibleMigration {
transaction = false as const;

async up({ queryRunner, tablePrefix, runQuery }: MigrationContext) {
const tableName = `${tablePrefix}user_api_keys`;

Expand Down Expand Up @@ -74,4 +76,52 @@ export class AddApiKeysTable1724951148974 {
// Rename the temporary table to users
await queryRunner.query('ALTER TABLE users_new RENAME TO user;');
}

async down({
queryRunner,
runQuery,
tablePrefix,
schemaBuilder: { dropTable, createIndex },
escape,
}: MigrationContext) {
const userApiKeysTable = escape.tableName('user_api_keys');
const apiKeyColumn = escape.columnName('apiKey');
const userIdColumn = escape.columnName('userId');
const idColumn = escape.columnName('id');
const createdAtColumn = escape.columnName('createdAt');

const queryToGetUsersApiKeys = `
SELECT
${userIdColumn},
${apiKeyColumn},
${createdAtColumn}
FROM
${userApiKeysTable}
WHERE
${createdAtColumn} IN(
SELECT
MIN(${createdAtColumn})
FROM ${userApiKeysTable}
GROUP BY ${userIdColumn});`;

const oldestApiKeysPerUser = (await queryRunner.query(queryToGetUsersApiKeys)) as Array<
Partial<ApiKey>
>;

await queryRunner.query(`ALTER TABLE ${tablePrefix}user ADD COLUMN "apiKey" varchar;`);

await createIndex('user', ['apiKey'], true);

await Promise.all(
oldestApiKeysPerUser.map(
async (user: { userId: string; apiKey: string }) =>
await runQuery(
`UPDATE ${tablePrefix}user SET ${apiKeyColumn} = :apiKey WHERE ${idColumn} = :userId`,
user,
),
),
);

await dropTable('user_api_keys');
}
}

0 comments on commit 1ca1494

Please sign in to comment.