-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2997 from Infisical/daniel/unique-group-names
feat(groups): unique names
- Loading branch information
Showing
10 changed files
with
139 additions
and
36 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
backend/src/db/migrations/20250115222458_groups-unique-name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
// find any duplicate group names within organizations | ||
const duplicates = await knex(TableName.Groups) | ||
.select("orgId", "name") | ||
.count("* as count") | ||
.groupBy("orgId", "name") | ||
.having(knex.raw("count(*) > 1")); | ||
|
||
// for each set of duplicates, update all but one with a numbered suffix | ||
for await (const duplicate of duplicates) { | ||
const groups = await knex(TableName.Groups) | ||
.select("id", "name") | ||
.where({ | ||
orgId: duplicate.orgId, | ||
name: duplicate.name | ||
}) | ||
.orderBy("createdAt", "asc"); // keep original name for oldest group | ||
|
||
// skip the first (oldest) group, rename others with numbered suffix | ||
for (let i = 1; i < groups.length; i += 1) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await knex(TableName.Groups) | ||
.where("id", groups[i].id) | ||
.update({ | ||
name: `${groups[i].name} (${i})`, | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore TS doesn't know about Knex's timestamp types | ||
updatedAt: new Date() | ||
}); | ||
} | ||
} | ||
|
||
// add the unique constraint | ||
await knex.schema.alterTable(TableName.Groups, (t) => { | ||
t.unique(["orgId", "name"]); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
// Remove the unique constraint | ||
await knex.schema.alterTable(TableName.Groups, (t) => { | ||
t.dropUnique(["orgId", "name"]); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { isDisposableEmail } from "./validate-email"; | ||
export { isValidFolderName, isValidSecretPath } from "./validate-folder-name"; | ||
export { blockLocalAndPrivateIpAddresses } from "./validate-url"; | ||
export { isUuidV4 } from "./validate-uuid"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { z } from "zod"; | ||
|
||
export const isUuidV4 = (uuid: string) => z.string().uuid().safeParse(uuid).success; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters