From 6d8b11aeb8f3f15ce627e8a5d8c6bcc193c614f7 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 17 Feb 2023 14:47:31 +0100 Subject: [PATCH] finished up Table definition and migration --- .../entity/0061-event_refactoring/Event.ts | 24 +++-------- database/migrations/0061-event_refactoring.ts | 43 ++++++++++++++----- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/database/entity/0061-event_refactoring/Event.ts b/database/entity/0061-event_refactoring/Event.ts index ca2ab58da..80b4238bb 100644 --- a/database/entity/0061-event_refactoring/Event.ts +++ b/database/entity/0061-event_refactoring/Event.ts @@ -1,12 +1,12 @@ import { Contribution } from '../Contribution' import { ContributionMessage } from '../ContributionMessage' import { User } from '../User' +import { Transaction } from '../Transaction' import Decimal from 'decimal.js-light' -import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Transaction } from 'typeorm' +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, JoinColumn } from 'typeorm' import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' -@Entity('event') -// TODO tablename event_protocol +@Entity('events') export class Event extends BaseEntity { @PrimaryGeneratedColumn('increment', { unsigned: true }) id: number @@ -14,28 +14,24 @@ export class Event extends BaseEntity { @Column({ length: 100, nullable: false, collation: 'utf8mb4_unicode_ci' }) type: string - // TODO proper field type - @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) + @CreateDateColumn({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP()' , nullable: false }) createdAt: Date - // TODO field name user_id // @Column({ name: 'affected_user_id', unsigned: true, nullable: false }) // affectedUserId: number @ManyToOne(() => User) @JoinColumn({ name: 'affected_user_id', referencedColumnName: 'id' }) - affectedUser: User | null + affectedUser: User - // TODO new column - // TODO potentially save actingRole aswell + // TODO potentially save actingRole as well // @Column({ name: 'acting_user_id', unsigned: true, nullable: false }) // actingUserId: number @ManyToOne(() => User) @JoinColumn({ name: 'acting_user_id', referencedColumnName: 'id' }) - actingUser: User | null + actingUser: User - // TODO rename column x_user_id // @Column({ name: 'involved_user_id', type: 'int', unsigned: true, nullable: true }) // involvedUserId: number | null @@ -43,9 +39,6 @@ export class Event extends BaseEntity { @JoinColumn({ name: 'involved_user_id', referencedColumnName: 'id' }) involvedUser: User | null - // TODO drop column xCommunityId - - // TODO rename column transaction_id // @Column({ name: 'involved_transaction_id', type: 'int', unsigned: true, nullable: true }) // involvedTransactionId: number | null @@ -53,7 +46,6 @@ export class Event extends BaseEntity { @JoinColumn({ name: 'involved_transaction_id', referencedColumnName: 'id' }) involvedTransaction: Transaction | null - // TODO rename column contribution_id // @Column({ name: 'involved_contribution_id', type: 'int', unsigned: true, nullable: true }) // involvedContributionId: number | null @@ -61,8 +53,6 @@ export class Event extends BaseEntity { @JoinColumn({ name: 'involved_contribution_id', referencedColumnName: 'id' }) involvedContribution: Contribution | null - // TODO move column - // TODO rename column message_id // TEST do we need the Id field definition? // @Column({ name: 'involved_contribution_message_id', type: 'int', unsigned: true, nullable: true }) // involvedContributionMessageId: number | null diff --git a/database/migrations/0061-event_refactoring.ts b/database/migrations/0061-event_refactoring.ts index a5ce50595..88874b15e 100644 --- a/database/migrations/0061-event_refactoring.ts +++ b/database/migrations/0061-event_refactoring.ts @@ -8,26 +8,47 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn('RENAME TABLE `event_protocol` TO `events`;') + await queryFn('ALTER TABLE `events` RENAME COLUMN `user_id` TO `affected_user_id`;') + await queryFn( - 'ALTER TABLE `communities` MODIFY COLUMN `last_announced_at` datetime(3) AFTER `end_point`;', + 'ALTER TABLE `events` ADD COLUMN `acting_user_id` int(10) unsigned DEFAULT NULL AFTER `affected_user_id`;', + ) + await queryFn('UPDATE `events` SET `acting_user_id` = `affected_user_id`;') + await queryFn( + 'ALTER TABLE `events` MODIFY COLUMN `acting_user_id` int(10) unsigned NOT NULL AFTER `affected_user_id`;', + ) + + await queryFn('ALTER TABLE `events` RENAME COLUMN `x_user_id` TO `involved_user_id`;') + await queryFn('ALTER TABLE `events` DROP COLUMN `x_community_id`;') + await queryFn('ALTER TABLE `events` RENAME COLUMN `transaction_id` TO `involved_transaction_id`;') + await queryFn( + 'ALTER TABLE `events` RENAME COLUMN `contribution_id` TO `involved_contribution_id`;', ) await queryFn( - 'ALTER TABLE `communities` ADD COLUMN `foreign` tinyint(4) NOT NULL DEFAULT 1 AFTER `id`;', + 'ALTER TABLE `events` MODIFY COLUMN `message_id` int(10) unsigned DEFAULT NULL AFTER `involved_contribution_id`;', ) await queryFn( - 'ALTER TABLE `communities` ADD COLUMN `verified_at` datetime(3) AFTER `last_announced_at`;', - ) - await queryFn( - 'ALTER TABLE `communities` ADD COLUMN `last_error_at` datetime(3) AFTER `verified_at`;', + 'ALTER TABLE `events` RENAME COLUMN `message_id` TO `involved_contribution_message_id`;', ) } export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { - // write downgrade logic as parameter of queryFn await queryFn( - 'ALTER TABLE `communities` MODIFY COLUMN `last_announced_at` datetime(3) NOT NULL AFTER `end_point`;', + 'ALTER TABLE `events` RENAME COLUMN `involved_contribution_message_id` TO `message_id`;', ) - await queryFn('ALTER TABLE `communities` DROP COLUMN `foreign`;') - await queryFn('ALTER TABLE `communities` DROP COLUMN `verified_at`;') - await queryFn('ALTER TABLE `communities` DROP COLUMN `last_error_at`;') + await queryFn( + 'ALTER TABLE `events` MODIFY COLUMN `message_id` int(10) unsigned DEFAULT NULL AFTER `amount`;', + ) + await queryFn( + 'ALTER TABLE `events` RENAME COLUMN `involved_contribution_id` TO `contribution_id`;', + ) + await queryFn('ALTER TABLE `events` RENAME COLUMN `involved_transaction_id` TO `transaction_id`;') + await queryFn( + 'ALTER TABLE `events` ADD COLUMN `x_community_id` int(10) unsigned DEFAULT NULL AFTER `involved_user_id`;', + ) + await queryFn('ALTER TABLE `events` RENAME COLUMN `involved_user_id` TO `x_user_id`;') + await queryFn('ALTER TABLE `events` DROP COLUMN `acting_user_id`;') + await queryFn('ALTER TABLE `events` RENAME COLUMN `affected_user_id` TO `user_id`;') + await queryFn('RENAME TABLE `events` TO `event_protocol`;') }