diff --git a/database/entity/0020-rename_and_clean_state_users/User.ts b/database/entity/0020-rename_and_clean_state_users/User.ts new file mode 100644 index 000000000..3d3b4d7b1 --- /dev/null +++ b/database/entity/0020-rename_and_clean_state_users/User.ts @@ -0,0 +1,68 @@ +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany } 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 + + @Column({ type: 'bool', default: false }) + disabled: boolean + + @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[] +} diff --git a/database/entity/User.ts b/database/entity/User.ts index 6dcdfed68..867d63112 100644 --- a/database/entity/User.ts +++ b/database/entity/User.ts @@ -1 +1 @@ -export { User } from './0019-replace_login_user_id_with_state_user_id/User' +export { User } from './0020-rename_and_clean_state_users/User' diff --git a/database/migrations/0020-rename_and_clean_state_users.ts b/database/migrations/0020-rename_and_clean_state_users.ts new file mode 100644 index 000000000..5abda1dc9 --- /dev/null +++ b/database/migrations/0020-rename_and_clean_state_users.ts @@ -0,0 +1,39 @@ +/* MIGRATION TO CLEAN UP AND RENAME STATE_USERS + * + * This migration renames 'state_users` to `users` + * and removes columns with no meaningful value + */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // rename `state_users` table to `users` + await queryFn('RENAME TABLE `state_users` TO `users`;') + + // Remove the column `index_id` from `users`, it only contains 0 as value + await queryFn('ALTER TABLE `users` DROP COLUMN `index_id`;') + + // Remove the column `username` from `users`, it contains only '' or NULL + await queryFn('ALTER TABLE `users` DROP COLUMN `username`;') + + // Remove the column `description` from `users`, it contains only '' or NULL + await queryFn('ALTER TABLE `users` DROP COLUMN `description`;') + + // Remove the column `passphrase_shown` from `users`, it contains only 0 as value + await queryFn('ALTER TABLE `users` DROP COLUMN `passphrase_shown`;') +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `users` ADD COLUMN `passphrase_shown` tinyint(4) NOT NULL DEFAULT 0 AFTER `email_checked`;', + ) + await queryFn( + "ALTER TABLE `users` ADD COLUMN `description` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT '' AFTER `disabled`;", + ) + await queryFn( + 'ALTER TABLE `users` ADD COLUMN `username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL AFTER `last_name`;', + ) + await queryFn( + 'ALTER TABLE `users` ADD COLUMN `index_id` smallint(6) NOT NULL DEFAULT 0 AFTER `id`;', + ) + + await queryFn('RENAME TABLE `users` TO `state_users`;') +}