Fix user schema

This commit is contained in:
Alice Gaudon 2020-07-24 16:13:15 +02:00
parent c0dd48d064
commit 40181a973b
4 changed files with 34 additions and 22 deletions

View File

@ -0,0 +1,29 @@
import Migration from "../../db/Migration";
import {Connection} from "mysql";
export default class FixUserMainEmailRelation extends Migration {
public async install(connection: Connection): Promise<void> {
await this.query(`ALTER TABLE users
ADD COLUMN main_user_email_id INT,
ADD FOREIGN KEY main_user_email_fk (main_user_email_id) REFERENCES user_emails (id)`, connection);
await this.query(`UPDATE users u LEFT JOIN user_emails ue ON u.id = ue.user_id
SET u.main_user_email_id=ue.id
WHERE ue.main = true`, connection);
await this.query(`ALTER TABLE user_emails
DROP COLUMN main`, connection);
}
public async rollback(connection: Connection): Promise<void> {
await this.query(`ALTER TABLE user_emails
ADD COLUMN main BOOLEAN DEFAULT false`, connection);
await this.query(`UPDATE user_emails ue LEFT JOIN users u ON ue.id = u.main_user_email_id
SET ue.main = true`, connection)
await this.query(`ALTER TABLE users
DROP FOREIGN KEY main_user_email_fk,
DROP COLUMN main_user_email_id`, connection);
}
public registerModels(): void {
}
}

View File

@ -14,6 +14,7 @@ export default class User extends Model {
public readonly id?: number = undefined;
public name?: string = undefined;
public main_user_email_id?: number = undefined;
public is_admin: boolean = false;
public created_at?: Date = undefined;
public updated_at?: Date = undefined;
@ -23,7 +24,7 @@ export default class User extends Model {
foreignKey: 'user_id'
});
public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('main', true));
public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('id', this.main_user_email_id));
public constructor(data: any) {
super(data);
@ -31,6 +32,7 @@ export default class User extends Model {
protected init(): void {
this.setValidation('name').acceptUndefined().between(3, 64);
this.setValidation('main_user_email_id').acceptUndefined().exists(UserEmail, 'id');
if (User.isApprovalMode()) {
this.setValidation('approved').defined();
}

View File

@ -9,7 +9,6 @@ export default class UserEmail extends Model {
public readonly id?: number = undefined;
public user_id?: number = undefined;
public readonly email?: string = undefined;
private main?: boolean = undefined;
public created_at?: Date = undefined;
public readonly user = new OneModelRelation<UserEmail, User>(this, ModelFactory.get(User), {
@ -17,8 +16,6 @@ export default class UserEmail extends Model {
foreignKey: 'id'
});
private _wasSetToMain: boolean = false;
constructor(data: any) {
super(data);
}
@ -28,22 +25,4 @@ export default class UserEmail extends Model {
this.setValidation('email').defined().regexp(EMAIL_REGEX).unique(this);
this.setValidation('main').defined();
}
protected async beforeSave(exists: boolean, connection: Connection) {
if (this._wasSetToMain) {
await query(`UPDATE ${this.table} SET main=false WHERE user_id=${this.user_id}`, null, connection);
this._wasSetToMain = false;
}
}
public isMain(): boolean {
return Boolean(this.main);
}
public setMain() {
if (!this.isMain()) {
this.main = true;
this._wasSetToMain = true;
}
}
}

View File

@ -1,9 +1,11 @@
import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable";
import CreateLogsTable from "../src/migrations/CreateLogsTable";
import CreateUsersAndUserEmailsTable from "../src/auth/migrations/CreateUsersAndUserEmailsTable";
import FixUserMainEmailRelation from "../src/auth/migrations/FixUserMainEmailRelation";
export const MIGRATIONS = [
CreateMigrationsTable,
CreateLogsTable,
CreateUsersAndUserEmailsTable,
FixUserMainEmailRelation,
];