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} from "typeorm";
|
|
|
|
|
|
|
|
export class UserRefUnique1664528376930 implements MigrationInterface {
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
|
|
// This is second part of migration 1663851423064-UserUUID, that makes
|
|
|
|
// the ref column unique.
|
|
|
|
|
|
|
|
// Update users that don't have unique ref set.
|
|
|
|
const userList = await queryRunner.manager.createQueryBuilder()
|
2024-07-01 13:13:39 +00:00
|
|
|
.select(["users.id", "users.ref"])
|
|
|
|
.from("users", "users")
|
|
|
|
.where("users.ref is null")
|
|
|
|
.getMany();
|
2022-10-03 14:45:44 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Mark column as unique and non-nullable.
|
|
|
|
const users = (await queryRunner.getTable('users'))!;
|
|
|
|
const oldRef = users.findColumnByName('ref')!;
|
|
|
|
const newRef = oldRef.clone();
|
|
|
|
newRef.isUnique = true;
|
|
|
|
newRef.isNullable = false;
|
|
|
|
await queryRunner.changeColumn('users', oldRef, newRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
|
|
// Mark column as non unique and nullable.
|
|
|
|
const users = (await queryRunner.getTable('users'))!;
|
|
|
|
const oldRef = users.findColumnByName('ref')!;
|
|
|
|
const newRef = oldRef.clone();
|
|
|
|
newRef.isUnique = false;
|
|
|
|
newRef.isNullable = true;
|
|
|
|
await queryRunner.changeColumn('users', oldRef, newRef);
|
|
|
|
}
|
|
|
|
}
|