-
Notifications
You must be signed in to change notification settings - Fork 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 #1552 from akhilmhdh/feat/temp-roles
feat: multiple project role and temporary role support
- Loading branch information
Showing
46 changed files
with
2,145 additions
and
595 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName, TProjectUserMembershipRolesInsert } from "../schemas"; | ||
import { createOnUpdateTrigger, dropOnUpdateTrigger } from "../utils"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const doesTableExist = await knex.schema.hasTable(TableName.ProjectUserMembershipRole); | ||
if (!doesTableExist) { | ||
await knex.schema.createTable(TableName.ProjectUserMembershipRole, (t) => { | ||
t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid()); | ||
t.string("role").notNullable(); | ||
t.uuid("projectMembershipId").notNullable(); | ||
t.foreign("projectMembershipId").references("id").inTable(TableName.ProjectMembership).onDelete("CASCADE"); | ||
// until role is changed/removed the role should not deleted | ||
t.uuid("customRoleId"); | ||
t.foreign("customRoleId").references("id").inTable(TableName.ProjectRoles); | ||
t.boolean("isTemporary").notNullable().defaultTo(false); | ||
t.string("temporaryMode"); | ||
t.string("temporaryRange"); // could be cron or relative time like 1H or 1minute etc | ||
t.datetime("temporaryAccessStartTime"); | ||
t.datetime("temporaryAccessEndTime"); | ||
t.timestamps(true, true, true); | ||
}); | ||
} | ||
|
||
await createOnUpdateTrigger(knex, TableName.ProjectUserMembershipRole); | ||
|
||
const projectMembershipStream = knex.select("*").from(TableName.ProjectMembership).stream(); | ||
const chunkSize = 1000; | ||
let rows: TProjectUserMembershipRolesInsert[] = []; | ||
for await (const row of projectMembershipStream) { | ||
// disabling eslint just this part because the latest ts type doesn't have these values after migration as they are removed | ||
/* eslint-disable */ | ||
// @ts-ignore - created at is inserted from old data | ||
rows = rows.concat({ | ||
// @ts-ignore - missing in ts type post migration | ||
role: row.role, | ||
// @ts-ignore - missing in ts type post migration | ||
customRoleId: row.roleId, | ||
projectMembershipId: row.id, | ||
createdAt: row.createdAt, | ||
updatedAt: row.updatedAt | ||
}); | ||
/* eslint-disable */ | ||
if (rows.length >= chunkSize) { | ||
await knex(TableName.ProjectUserMembershipRole).insert(rows); | ||
rows.splice(0, rows.length); | ||
} | ||
} | ||
if (rows.length) await knex(TableName.ProjectUserMembershipRole).insert(rows); | ||
// will be dropped later | ||
// await knex.schema.alterTable(TableName.ProjectMembership, (t) => { | ||
// t.dropColumn("roleId"); | ||
// t.dropColumn("role"); | ||
// }); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.schema.dropTableIfExists(TableName.ProjectUserMembershipRole); | ||
await dropOnUpdateTrigger(knex, TableName.ProjectUserMembershipRole); | ||
} |
63 changes: 63 additions & 0 deletions
63
backend/src/db/migrations/20240312162556_temp-role-identity.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,63 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName, TIdentityProjectMembershipRoleInsert } from "../schemas"; | ||
import { createOnUpdateTrigger, dropOnUpdateTrigger } from "../utils"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const doesTableExist = await knex.schema.hasTable(TableName.IdentityProjectMembershipRole); | ||
if (!doesTableExist) { | ||
await knex.schema.createTable(TableName.IdentityProjectMembershipRole, (t) => { | ||
t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid()); | ||
t.string("role").notNullable(); | ||
t.uuid("projectMembershipId").notNullable(); | ||
t.foreign("projectMembershipId") | ||
.references("id") | ||
.inTable(TableName.IdentityProjectMembership) | ||
.onDelete("CASCADE"); | ||
// until role is changed/removed the role should not deleted | ||
t.uuid("customRoleId"); | ||
t.foreign("customRoleId").references("id").inTable(TableName.ProjectRoles); | ||
t.boolean("isTemporary").notNullable().defaultTo(false); | ||
t.string("temporaryMode"); | ||
t.string("temporaryRange"); // could be cron or relative time like 1H or 1minute etc | ||
t.datetime("temporaryAccessStartTime"); | ||
t.datetime("temporaryAccessEndTime"); | ||
t.timestamps(true, true, true); | ||
}); | ||
} | ||
|
||
await createOnUpdateTrigger(knex, TableName.IdentityProjectMembershipRole); | ||
|
||
const projectMembershipStream = knex.select("*").from(TableName.IdentityProjectMembership).stream(); | ||
const chunkSize = 1000; | ||
let rows: TIdentityProjectMembershipRoleInsert[] = []; | ||
for await (const row of projectMembershipStream) { | ||
// disabling eslint just this part because the latest ts type doesn't have these values after migration as they are removed | ||
/* eslint-disable */ | ||
// @ts-ignore - created at is inserted from old data | ||
rows = rows.concat({ | ||
// @ts-ignore - missing in ts type post migration | ||
role: row.role, | ||
// @ts-ignore - missing in ts type post migration | ||
customRoleId: row.roleId, | ||
projectMembershipId: row.id, | ||
createdAt: row.createdAt, | ||
updatedAt: row.updatedAt | ||
}); | ||
/* eslint-disable */ | ||
if (rows.length >= chunkSize) { | ||
await knex(TableName.IdentityProjectMembershipRole).insert(rows); | ||
rows.splice(0, rows.length); | ||
} | ||
} | ||
if(rows.length) await knex(TableName.IdentityProjectMembershipRole).insert(rows); | ||
// await knex.schema.alterTable(TableName.IdentityProjectMembership, (t) => { | ||
// t.dropColumn("roleId"); | ||
// t.dropColumn("role"); | ||
// }); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.schema.dropTableIfExists(TableName.IdentityProjectMembershipRole); | ||
await dropOnUpdateTrigger(knex, TableName.IdentityProjectMembershipRole); | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/db/schemas/identity-project-membership-role.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,31 @@ | ||
// Code generated by automation script, DO NOT EDIT. | ||
// Automated by pulling database and generating zod schema | ||
// To update. Just run npm run generate:schema | ||
// Written by akhilmhdh. | ||
|
||
import { z } from "zod"; | ||
|
||
import { TImmutableDBKeys } from "./models"; | ||
|
||
export const IdentityProjectMembershipRoleSchema = z.object({ | ||
id: z.string().uuid(), | ||
role: z.string(), | ||
projectMembershipId: z.string().uuid(), | ||
customRoleId: z.string().uuid().nullable().optional(), | ||
isTemporary: z.boolean().default(false), | ||
temporaryMode: z.string().nullable().optional(), | ||
temporaryRange: z.string().nullable().optional(), | ||
temporaryAccessStartTime: z.date().nullable().optional(), | ||
temporaryAccessEndTime: z.date().nullable().optional(), | ||
createdAt: z.date(), | ||
updatedAt: z.date() | ||
}); | ||
|
||
export type TIdentityProjectMembershipRole = z.infer<typeof IdentityProjectMembershipRoleSchema>; | ||
export type TIdentityProjectMembershipRoleInsert = Omit< | ||
z.input<typeof IdentityProjectMembershipRoleSchema>, | ||
TImmutableDBKeys | ||
>; | ||
export type TIdentityProjectMembershipRoleUpdate = Partial< | ||
Omit<z.input<typeof IdentityProjectMembershipRoleSchema>, TImmutableDBKeys> | ||
>; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Code generated by automation script, DO NOT EDIT. | ||
// Automated by pulling database and generating zod schema | ||
// To update. Just run npm run generate:schema | ||
// Written by akhilmhdh. | ||
|
||
import { z } from "zod"; | ||
|
||
import { TImmutableDBKeys } from "./models"; | ||
|
||
export const ProjectUserMembershipRolesSchema = z.object({ | ||
id: z.string().uuid(), | ||
role: z.string(), | ||
projectMembershipId: z.string().uuid(), | ||
customRoleId: z.string().uuid().nullable().optional(), | ||
isTemporary: z.boolean().default(false), | ||
temporaryMode: z.string().nullable().optional(), | ||
temporaryRange: z.string().nullable().optional(), | ||
temporaryAccessStartTime: z.date().nullable().optional(), | ||
temporaryAccessEndTime: z.date().nullable().optional(), | ||
createdAt: z.date(), | ||
updatedAt: z.date() | ||
}); | ||
|
||
export type TProjectUserMembershipRoles = z.infer<typeof ProjectUserMembershipRolesSchema>; | ||
export type TProjectUserMembershipRolesInsert = Omit< | ||
z.input<typeof ProjectUserMembershipRolesSchema>, | ||
TImmutableDBKeys | ||
>; | ||
export type TProjectUserMembershipRolesUpdate = Partial< | ||
Omit<z.input<typeof ProjectUserMembershipRolesSchema>, TImmutableDBKeys> | ||
>; |
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
Oops, something went wrong.