2022-10-03 14:45:44 +00:00
|
|
|
import {makeId} from 'app/server/lib/idUtils';
|
2024-07-01 13:13:39 +00:00
|
|
|
import {chunk} from 'lodash';
|
2022-10-03 14:45:44 +00:00
|
|
|
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm";
|
|
|
|
|
|
|
|
export class UserUUID1663851423064 implements MigrationInterface {
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<any> {
|
|
|
|
// Add ref column, for now make it nullable and not unique, we
|
|
|
|
// first need to put a value in it for all existing users.
|
|
|
|
await queryRunner.addColumn("users", new TableColumn({
|
|
|
|
name: "ref",
|
|
|
|
type: 'varchar',
|
|
|
|
isNullable: true,
|
|
|
|
isUnique: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Updating so many rows in a multiple queries is not ideal. We will send updates in chunks.
|
|
|
|
// 300 seems to be a good number, for 24k rows we have 80 queries.
|
|
|
|
const userList = await queryRunner.manager.createQueryBuilder()
|
2024-07-01 13:13:39 +00:00
|
|
|
.select(["users.id", "users.ref"])
|
|
|
|
.from("users", "users")
|
2022-10-03 14:45:44 +00:00
|
|
|
.getMany();
|
|
|
|
userList.forEach(u => u.ref = makeId());
|
2024-07-01 13:13:39 +00:00
|
|
|
|
|
|
|
const userChunks = chunk(userList, 300);
|
|
|
|
for (const users of userChunks) {
|
|
|
|
await queryRunner.connection.transaction(async manager => {
|
|
|
|
const queries = users.map((user: any, _index: number, _array: any[]) => {
|
|
|
|
return queryRunner.manager.update("users", user.id, user);
|
|
|
|
});
|
|
|
|
await Promise.all(queries);
|
|
|
|
});
|
|
|
|
}
|
2022-10-03 14:45:44 +00:00
|
|
|
|
|
|
|
// We are not making this column unique yet, because it can fail
|
|
|
|
// if there are some old workers still running, and any new user
|
|
|
|
// is created. We will make it unique in a later migration.
|
|
|
|
}
|
|
|
|
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<any> {
|
|
|
|
await queryRunner.dropColumn("users", "ref");
|
|
|
|
}
|
|
|
|
}
|