diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 9aabaf4ad..ef537d804 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/database/entity/0063-event_link_fields/Event.ts b/database/entity/0063-event_link_fields/Event.ts new file mode 100644 index 000000000..bd3616cf8 --- /dev/null +++ b/database/entity/0063-event_link_fields/Event.ts @@ -0,0 +1,99 @@ +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, + CreateDateColumn, + ManyToOne, + JoinColumn, +} from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { TransactionLink } from '../TransactionLink' +import { ContributionLink } from '../ContributionLink' + +@Entity('events') +export class Event extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ length: 100, nullable: false, collation: 'utf8mb4_unicode_ci' }) + type: string + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP()', + nullable: false, + }) + createdAt: Date + + @Column({ name: 'affected_user_id', unsigned: true, nullable: false }) + affectedUserId: number + + @ManyToOne(() => User) + @JoinColumn({ name: 'affected_user_id', referencedColumnName: 'id' }) + affectedUser: User + + @Column({ name: 'acting_user_id', unsigned: true, nullable: false }) + actingUserId: number + + @ManyToOne(() => User) + @JoinColumn({ name: 'acting_user_id', referencedColumnName: 'id' }) + actingUser: User + + @Column({ name: 'involved_user_id', type: 'int', unsigned: true, nullable: true }) + involvedUserId: number | null + + @ManyToOne(() => User) + @JoinColumn({ name: 'involved_user_id', referencedColumnName: 'id' }) + involvedUser: User | null + + @Column({ name: 'involved_transaction_id', type: 'int', unsigned: true, nullable: true }) + involvedTransactionId: number | null + + @ManyToOne(() => Transaction) + @JoinColumn({ name: 'involved_transaction_id', referencedColumnName: 'id' }) + involvedTransaction: Transaction | null + + @Column({ name: 'involved_contribution_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionId: number | null + + @ManyToOne(() => Contribution) + @JoinColumn({ name: 'involved_contribution_id', referencedColumnName: 'id' }) + involvedContribution: Contribution | null + + @Column({ name: 'involved_contribution_message_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionMessageId: number | null + + @ManyToOne(() => ContributionMessage) + @JoinColumn({ name: 'involved_contribution_message_id', referencedColumnName: 'id' }) + involvedContributionMessage: ContributionMessage | null + + @Column({ name: 'involved_transaction_link_id', type: 'int', unsigned: true, nullable: true }) + involvedTransactionLinkId: number | null + + @ManyToOne(() => TransactionLink) + @JoinColumn({ name: 'involved_transaction_link_id', referencedColumnName: 'id' }) + involvedTransactionLink: TransactionLink | null + + @Column({ name: 'involved_contribution_link_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionLinkId: number | null + + @ManyToOne(() => ContributionLink) + @JoinColumn({ name: 'involved_contribution_link_id', referencedColumnName: 'id' }) + involvedContributionLink: ContributionLink | null + + @Column({ + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + transformer: DecimalTransformer, + }) + amount: Decimal | null +} diff --git a/database/entity/Event.ts b/database/entity/Event.ts index e53085f2f..04cbaf458 100644 --- a/database/entity/Event.ts +++ b/database/entity/Event.ts @@ -1 +1 @@ -export { Event } from './0061-event_refactoring/Event' +export { Event } from './0063-event_link_fields/Event' diff --git a/database/migrations/0063-event_link_fields.ts b/database/migrations/0063-event_link_fields.ts new file mode 100644 index 000000000..3b383ad07 --- /dev/null +++ b/database/migrations/0063-event_link_fields.ts @@ -0,0 +1,29 @@ +/* MIGRATION TO ADD LINK ID FIELDS TO EVENT TABLE + * + * This migration add two fields to store a TransactionLinkId and a ContributionLinkId + * in the event table. Furthermore the event `REDEEM_REGISTER` is rewritten to use the + * new fields. + */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `events` ADD COLUMN `involved_transaction_link_id` int(10) unsigned DEFAULT NULL AFTER `involved_contribution_message_id`;', + ) + await queryFn( + 'ALTER TABLE `events` ADD COLUMN `involved_contribution_link_id` int(10) unsigned DEFAULT NULL AFTER `involved_transaction_link_id`;', + ) + await queryFn( + 'UPDATE `events` SET `involved_transaction_link_id` = `involved_transaction_id`, `involved_transaction_id` = NULL, `involved_contribution_link_id` = `involved_contribution_id`, `involved_contribution_id` = NULL WHERE `type` = "REDEEM_REGISTER";', + ) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'UPDATE `events` SET `involved_transaction_id` = `involved_transaction_link_id`, `involved_contribution_id` = `involved_contribution_link_id` WHERE `type` = "REDEEM_REGISTER";', + ) + await queryFn('ALTER TABLE `events` DROP COLUMN `involved_contribution_link_id`;') + await queryFn('ALTER TABLE `events` DROP COLUMN `involved_transaction_link_id`;') +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 7ca44e52c..78f2b162c 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -3,7 +3,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/federation/src/config/index.ts b/federation/src/config/index.ts index b3b529e20..61204ca37 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', // DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info