mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-05 17:36:09 +00:00
add_user_roles migration and entity
This commit is contained in:
parent
5226245b86
commit
d1d9f6c050
@ -89,7 +89,7 @@ export class User extends BaseEntity {
|
||||
|
||||
@OneToOne(() => UserRole, (role: UserRole) => role.userId)
|
||||
@JoinColumn({ name: 'user_id' })
|
||||
userRole: UserRole
|
||||
userRole?: UserRole
|
||||
|
||||
@Column({ name: 'referrer_id', type: 'int', unsigned: true, nullable: true, default: null })
|
||||
referrerId?: number | null
|
||||
|
||||
@ -12,15 +12,32 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`)
|
||||
|
||||
// merge values from login_email_opt_in table with users.email in new user_contacts table
|
||||
// insert values from users table with users.is_admin in new user_roles table
|
||||
await queryFn(`
|
||||
INSERT INTO user_roles
|
||||
(user_id, role, created_at, updated_at)
|
||||
SELECT u.id, 'admin', u.is_admin, null
|
||||
FROM users u
|
||||
WHERE u.is_admin IS NOT NULL;`)
|
||||
|
||||
// remove column is_admin from users table
|
||||
await queryFn('ALTER TABLE users DROP COLUMN is_admin;')
|
||||
}
|
||||
|
||||
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||
// first add column is_admin in users table
|
||||
await queryFn(
|
||||
'ALTER TABLE users ADD COLUMN is_admin datetime(3) NULL DEFAULT NULL AFTER language;',
|
||||
)
|
||||
// reconstruct the previous is_admin back from user_roles to users table
|
||||
const roles = await queryFn(
|
||||
`SELECT r.user_id, r.role, r.created_at FROM user_roles as r WHERE r.role = "admin"`,
|
||||
)
|
||||
for (const id in roles) {
|
||||
const role = roles[id]
|
||||
const isAdminDate = new Date(role.created_at).toISOString().slice(0, 19).replace('T', ' ')
|
||||
await queryFn(`UPDATE users SET is_admin = "${isAdminDate}" WHERE id = "${role.user_id}"`)
|
||||
}
|
||||
|
||||
await queryFn(`DROP TABLE user_roles;`)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user