feat(cli): add fix-username command to make all usernames lowercase (#2366)

* feat(cli): add fix-username command to make all usernames lowercase

* fix: add missing command
This commit is contained in:
Meier Lukas
2025-02-18 22:02:42 +01:00
committed by GitHub
parent 7705bc44ae
commit 8b7caf1d7d
2 changed files with 38 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
import { command } from "@drizzle-team/brocli";
import { db, eq } from "@homarr/db";
import { users } from "@homarr/db/schema";
export const fixUsernames = command({
name: "fix-usernames",
desc: "Changes all credentials usernames to lowercase",
// eslint-disable-next-line no-restricted-syntax
handler: async () => {
if (!process.env.AUTH_PROVIDERS?.toLowerCase().includes("credentials")) {
console.error("Credentials provider is not enabled");
return;
}
const credentialUsers = await db.query.users.findMany({
where: eq(users.provider, "credentials"),
});
for (const user of credentialUsers) {
if (!user.name) continue;
if (user.name !== user.name.toLowerCase()) continue;
await db
.update(users)
.set({
name: user.name.toLowerCase(),
})
.where(eq(users.id, user.id));
console.log(`Changed username from ${user.name} to ${user.name.toLowerCase()}`);
}
console.log("All usernames have been fixed");
},
});

View File

@@ -1,8 +1,9 @@
import { run } from "@drizzle-team/brocli";
import { fixUsernames } from "./commands/fix-usernames";
import { resetPassword } from "./commands/reset-password";
const commands = [resetPassword];
const commands = [resetPassword, fixUsernames];
void run(commands, {
name: "homarr-cli",