chore: Migrate user role to new column (#6721)

* Migration

* Double write

* Backfill script

* Simplify for self-hosted

* flip

* migration
This commit is contained in:
Tom Moor
2024-03-27 16:57:59 -06:00
committed by GitHub
parent bea36f87a6
commit 77716f3803
2 changed files with 60 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ import {
IsDate,
AllowNull,
AfterUpdate,
BeforeSave,
} from "sequelize-typescript";
import { UserPreferenceDefaults } from "@shared/constants";
import { languages } from "@shared/i18n";
@@ -144,6 +145,10 @@ class User extends ParanoidModel<
@Column
isViewer: boolean;
@Default(UserRole.Member)
@Column(DataType.ENUM(...Object.values(UserRole)))
role: UserRole;
@Column(DataType.BLOB)
@Encrypted
get jwtSecret() {
@@ -623,6 +628,20 @@ class User extends ParanoidModel<
});
};
/**
* Temporary hook to double write role while we transition to the new field.
*/
@BeforeSave
static doubleWriteRole = async (model: User) => {
if (model.isAdmin) {
model.role = UserRole.Admin;
} else if (model.isViewer) {
model.role = UserRole.Viewer;
} else {
model.role = UserRole.Member;
}
};
@BeforeCreate
static setRandomJwtSecret = (model: User) => {
model.jwtSecret = crypto.randomBytes(64).toString("hex");