Summary: There is a new column in users table called ref (user reference). It holds user's unique reference number that can be used for features that require some kind of ownership logic (like comments). Test Plan: Updated tests Reviewers: georgegevoian, paulfitz Reviewed By: georgegevoian, paulfitz Differential Revision: https://phab.getgrist.com/D3641messytables-requirements
parent
356090abae
commit
9628253fd8
@ -0,0 +1,33 @@
|
||||
import {User} from 'app/gen-server/entity/User';
|
||||
import {makeId} from 'app/server/lib/idUtils';
|
||||
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()
|
||||
.select("users")
|
||||
.from(User, "users")
|
||||
.getMany();
|
||||
userList.forEach(u => u.ref = makeId());
|
||||
await queryRunner.manager.save(userList, { chunk: 300 });
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue