Skip to content

[db] fix the query for non-fga migrated users #19194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should be able to keep the select to reduce the amount of bytes shuffled around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svenefftinge I tried, and it's indeed this line that fails the method, .select(["user.id"]) works. 🙄 So this is different to other querybuilder methods, which does not auto-namespace it's argument. And also, MySQL happily executes and retunrs and empty result... 🙈

.where({
fgaRelationshipsVersion: Not(Equal(fgaRelationshipsVersion)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svenefftinge Can you share some insight into what's not working here, e.g. how the resulting query looks like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, I wrote a test and verified it failed and then used the pattern we use in other queries as well to make the test pass.

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