replace disabled (flag) with deletedAt (date) on users

This commit is contained in:
Ulf Gebhardt 2022-02-17 10:00:02 +01:00
parent 6140ce19ed
commit bc43c495ad
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,75 @@
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
Column,
OneToMany,
DeleteDateColumn,
} from 'typeorm'
import { UserSetting } from '../UserSetting'
@Entity('users', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' })
export class User extends BaseEntity {
@PrimaryGeneratedColumn('increment', { unsigned: true })
id: number
@Column({ name: 'public_key', type: 'binary', length: 32, default: null, nullable: true })
pubKey: Buffer
@Column({ name: 'privkey', type: 'binary', length: 80, default: null, nullable: true })
privKey: Buffer
@Column({ length: 255, unique: true, nullable: false, collation: 'utf8mb4_unicode_ci' })
email: string
@Column({
name: 'first_name',
length: 255,
nullable: true,
default: null,
collation: 'utf8mb4_unicode_ci',
})
firstName: string
@Column({
name: 'last_name',
length: 255,
nullable: true,
default: null,
collation: 'utf8mb4_unicode_ci',
})
lastName: string
@DeleteDateColumn()
deletedAt: Date
@Column({ type: 'bigint', default: 0, unsigned: true })
password: BigInt
@Column({ name: 'email_hash', type: 'binary', length: 32, default: null, nullable: true })
emailHash: Buffer
@Column({ name: 'created', default: () => 'CURRENT_TIMESTAMP', nullable: false })
createdAt: Date
@Column({ name: 'email_checked', type: 'bool', nullable: false, default: false })
emailChecked: boolean
@Column({ length: 4, default: 'de', collation: 'utf8mb4_unicode_ci', nullable: false })
language: string
@Column({ name: 'publisher_id', default: 0 })
publisherId: number
@Column({
type: 'text',
name: 'passphrase',
collation: 'utf8mb4_unicode_ci',
nullable: true,
default: null,
})
passphrase: string
@OneToMany(() => UserSetting, (userSetting) => userSetting.user)
settings: UserSetting[]
}

View File

@ -1 +1 @@
export { User } from './0020-rename_and_clean_state_users/User'
export { User } from './0023-users_disabled_soft_delete/User'

View File

@ -0,0 +1,26 @@
/* MIGRATION TO IMPLEMENT SOFT DELETE ON THE USERS TABLE
*
* Replace the `disabled` column with `deletedAt` containing
* a date as it is standard for soft delete fields
*/
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// Create new `deletedAt` column
await queryFn(
'ALTER TABLE `users` ADD COLUMN `deletedAt` datetime DEFAULT NULL AFTER `disabled`;',
)
// Insert a 1.1.2022 as date for those users with `disabled=1`
await queryFn('UPDATE `users` SET `deletedAt` = "2022-01-01 00:00:00" WHERE `disabled` = 1;')
// Delete `disabled` column
await queryFn('ALTER TABLE `users` DROP COLUMN `disabled`;')
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn(
'ALTER TABLE `users` ADD COLUMN `disabled` tinyint(4) NOT NULL DEFAULT 0 AFTER `deletedAt`;',
)
await queryFn('UPDATE `users` SET `disabled` = 1 WHERE `deletedAt` IS NOT NULL;')
await queryFn('ALTER TABLE `users` DROP COLUMN `deletedAt`;')
}