Skip to content

Commit

Permalink
[db] fix the query for non-fga migrated users (#19194)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenefftinge authored Dec 5, 2023
1 parent 5db20a7 commit ad2a077
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
14 changes: 6 additions & 8 deletions components/gitpod-db/src/typeorm/user-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
MaybeUser,
PartialUserUpdate,
UserDB,
isBuiltinUser,
} from "../user-db";
import { DBGitpodToken } from "./entity/db-gitpod-token";
import { DBIdentity } from "./entity/db-identity";
Expand Down Expand Up @@ -662,16 +663,13 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us

async findUserIdsNotYetMigratedToFgaVersion(fgaRelationshipsVersion: number, limit: number): Promise<string[]> {
const userRepo = await this.getUserRepo();
const ids = (await userRepo
const users = await userRepo
.createQueryBuilder("user")
.select(["id"])
.where({
fgaRelationshipsVersion: Not(Equal(fgaRelationshipsVersion)),
markedDeleted: Equal(false),
})
.where("fgaRelationshipsVersion != :fgaRelationshipsVersion", { fgaRelationshipsVersion })
.andWhere("markedDeleted != true")
.orderBy("_lastModified", "DESC")
.limit(limit)
.getMany()) as Pick<DBUser, "id">[];
return ids.map(({ id }) => id);
.getMany();
return users.map((user) => user.id).filter((id) => !isBuiltinUser(id));
}
}
33 changes: 33 additions & 0 deletions components/gitpod-db/src/user-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,39 @@ class UserDBSpec {
expect(result.some((t) => t.tokenHash === token.tokenHash)).to.be.true;
expect(result.some((t) => t.tokenHash === token2.tokenHash)).to.be.true;
}

@test(timeout(10000))
public async findUserIdsNotYetMigratedToFgaVersion() {
let user1 = await this.db.newUser();
user1.name = "ABC";
user1.fgaRelationshipsVersion = 0;
user1 = await this.db.storeUser(user1);

let user2 = await this.db.newUser();
user2.name = "ABC2";
user2.fgaRelationshipsVersion = 1;
user2 = await this.db.storeUser(user2);

let user3 = await this.db.newUser();
user3.name = "ABC3";
user3.fgaRelationshipsVersion = 0;
user3 = await this.db.storeUser(user3);

const result = await this.db.findUserIdsNotYetMigratedToFgaVersion(1, 10);
expect(result).to.not.be.undefined;
expect(result.length).to.eq(2);
expect(result.some((id) => id === user1.id)).to.be.true;
expect(result.some((id) => id === user2.id)).to.be.false;
expect(result.some((id) => id === user3.id)).to.be.true;

const result2 = await this.db.findUserIdsNotYetMigratedToFgaVersion(1, 1);
expect(result2).to.not.be.undefined;
expect(result2.length).to.eq(1);

const result3 = await this.db.findUserIdsNotYetMigratedToFgaVersion(2, 10);
expect(result3).to.not.be.undefined;
expect(result3.length).to.eq(3);
}
}

namespace TestData {
Expand Down

0 comments on commit ad2a077

Please sign in to comment.