mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Migration that makes user.ref unique and non-nullable
Summary: Making user.ref column unique and non-nullable Test Plan: manual and existing Reviewers: georgegevoian, paulfitz Reviewed By: georgegevoian, paulfitz Subscribers: paulfitz Differential Revision: https://phab.getgrist.com/D3654
This commit is contained in:
37
app/gen-server/migration/1664528376930-UserRefUnique.ts
Normal file
37
app/gen-server/migration/1664528376930-UserRefUnique.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {User} from 'app/gen-server/entity/User';
|
||||
import {makeId} from 'app/server/lib/idUtils';
|
||||
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()
|
||||
.select("users")
|
||||
.from(User, "users")
|
||||
.where("ref is null")
|
||||
.getMany();
|
||||
userList.forEach(u => u.ref = makeId());
|
||||
await queryRunner.manager.save(userList, {chunk: 300});
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user