Skip to content

Commit ad2a077

Browse files
authored
[db] fix the query for non-fga migrated users (#19194)
1 parent 5db20a7 commit ad2a077

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

components/gitpod-db/src/typeorm/user-db-impl.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
MaybeUser,
3939
PartialUserUpdate,
4040
UserDB,
41+
isBuiltinUser,
4142
} from "../user-db";
4243
import { DBGitpodToken } from "./entity/db-gitpod-token";
4344
import { DBIdentity } from "./entity/db-identity";
@@ -662,16 +663,13 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
662663

663664
async findUserIdsNotYetMigratedToFgaVersion(fgaRelationshipsVersion: number, limit: number): Promise<string[]> {
664665
const userRepo = await this.getUserRepo();
665-
const ids = (await userRepo
666+
const users = await userRepo
666667
.createQueryBuilder("user")
667-
.select(["id"])
668-
.where({
669-
fgaRelationshipsVersion: Not(Equal(fgaRelationshipsVersion)),
670-
markedDeleted: Equal(false),
671-
})
668+
.where("fgaRelationshipsVersion != :fgaRelationshipsVersion", { fgaRelationshipsVersion })
669+
.andWhere("markedDeleted != true")
672670
.orderBy("_lastModified", "DESC")
673671
.limit(limit)
674-
.getMany()) as Pick<DBUser, "id">[];
675-
return ids.map(({ id }) => id);
672+
.getMany();
673+
return users.map((user) => user.id).filter((id) => !isBuiltinUser(id));
676674
}
677675
}

components/gitpod-db/src/user-db.spec.db.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,39 @@ class UserDBSpec {
297297
expect(result.some((t) => t.tokenHash === token.tokenHash)).to.be.true;
298298
expect(result.some((t) => t.tokenHash === token2.tokenHash)).to.be.true;
299299
}
300+
301+
@test(timeout(10000))
302+
public async findUserIdsNotYetMigratedToFgaVersion() {
303+
let user1 = await this.db.newUser();
304+
user1.name = "ABC";
305+
user1.fgaRelationshipsVersion = 0;
306+
user1 = await this.db.storeUser(user1);
307+
308+
let user2 = await this.db.newUser();
309+
user2.name = "ABC2";
310+
user2.fgaRelationshipsVersion = 1;
311+
user2 = await this.db.storeUser(user2);
312+
313+
let user3 = await this.db.newUser();
314+
user3.name = "ABC3";
315+
user3.fgaRelationshipsVersion = 0;
316+
user3 = await this.db.storeUser(user3);
317+
318+
const result = await this.db.findUserIdsNotYetMigratedToFgaVersion(1, 10);
319+
expect(result).to.not.be.undefined;
320+
expect(result.length).to.eq(2);
321+
expect(result.some((id) => id === user1.id)).to.be.true;
322+
expect(result.some((id) => id === user2.id)).to.be.false;
323+
expect(result.some((id) => id === user3.id)).to.be.true;
324+
325+
const result2 = await this.db.findUserIdsNotYetMigratedToFgaVersion(1, 1);
326+
expect(result2).to.not.be.undefined;
327+
expect(result2.length).to.eq(1);
328+
329+
const result3 = await this.db.findUserIdsNotYetMigratedToFgaVersion(2, 10);
330+
expect(result3).to.not.be.undefined;
331+
expect(result3.length).to.eq(3);
332+
}
300333
}
301334

302335
namespace TestData {

0 commit comments

Comments
 (0)