From 604340fbec7762325bf5dca060ce1a44058c9106 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Sat, 29 Jan 2022 05:31:16 +0100 Subject: [PATCH] migrate transaction_signatures table - move data into transactions table remove two unnecessary fields from transactions table --- .../Transaction.ts | 34 +++++++++++ database/entity/Transaction.ts | 2 +- database/entity/TransactionSignature.ts | 1 - database/entity/index.ts | 2 - .../migrations/0016-transaction_signatures.ts | 60 +++++++++++++++++++ 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 database/entity/0016-transaction_signatures/Transaction.ts delete mode 100644 database/entity/TransactionSignature.ts create mode 100644 database/migrations/0016-transaction_signatures.ts diff --git a/database/entity/0016-transaction_signatures/Transaction.ts b/database/entity/0016-transaction_signatures/Transaction.ts new file mode 100644 index 000000000..8c8251baf --- /dev/null +++ b/database/entity/0016-transaction_signatures/Transaction.ts @@ -0,0 +1,34 @@ +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToOne } from 'typeorm' +import { TransactionCreation } from '../TransactionCreation' +import { TransactionSendCoin } from '../TransactionSendCoin' + +@Entity('transactions') +export class Transaction extends BaseEntity { + // TODO the id is defined as bigint(20) - there might be problems with that: https://github.com/typeorm/typeorm/issues/2400 + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'transaction_type_id', unsigned: true, nullable: false }) + transactionTypeId: number + + @Column({ name: 'tx_hash', type: 'binary', length: 48, default: null }) + txHash: Buffer + + @Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' }) + memo: string + + @Column({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP' }) + received: Date + + @Column({ type: 'binary', length: 64, nullable: true, default: null }) + signature: Buffer + + @Column({ type: 'binary', length: 32, nullable: true, default: null }) + pubkey: Buffer + + @OneToOne(() => TransactionSendCoin, (transactionSendCoin) => transactionSendCoin.transaction) + transactionSendCoin: TransactionSendCoin + + @OneToOne(() => TransactionCreation, (transactionCreation) => transactionCreation.transaction) + transactionCreation: TransactionCreation +} diff --git a/database/entity/Transaction.ts b/database/entity/Transaction.ts index d3915c05d..f17479f9f 100644 --- a/database/entity/Transaction.ts +++ b/database/entity/Transaction.ts @@ -1 +1 @@ -export { Transaction } from './0001-init_db/Transaction' +export { Transaction } from './0016-transaction_signatures/Transaction' diff --git a/database/entity/TransactionSignature.ts b/database/entity/TransactionSignature.ts deleted file mode 100644 index e3c9cbe1c..000000000 --- a/database/entity/TransactionSignature.ts +++ /dev/null @@ -1 +0,0 @@ -export { TransactionSignature } from './0001-init_db/TransactionSignature' diff --git a/database/entity/index.ts b/database/entity/index.ts index 331e797df..c5ad5aac6 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -8,7 +8,6 @@ import { Migration } from './Migration' import { ServerUser } from './ServerUser' import { Transaction } from './Transaction' import { TransactionCreation } from './TransactionCreation' -import { TransactionSignature } from './TransactionSignature' import { TransactionSendCoin } from './TransactionSendCoin' import { User } from './User' import { UserSetting } from './UserSetting' @@ -27,7 +26,6 @@ export const entities = [ ServerUser, Transaction, TransactionCreation, - TransactionSignature, TransactionSendCoin, User, UserSetting, diff --git a/database/migrations/0016-transaction_signatures.ts b/database/migrations/0016-transaction_signatures.ts new file mode 100644 index 000000000..971512a77 --- /dev/null +++ b/database/migrations/0016-transaction_signatures.ts @@ -0,0 +1,60 @@ +/* MIGRATION TO CLEANUP TRANSACTIONS TABLE + * + * This migration cleans up the transactions table and + * combines its data with transaction_signatures. + */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // Drop column `state_group_id` since it only contains "0" as value, no variation. + // Furthermore it was not present in our model itself (meaning that newly created ) + await queryFn('ALTER TABLE `transactions` DROP COLUMN `state_group_id`;') + + // Drop column `blockchain_type_id` since it only contains "1" as value, no variation. + await queryFn('ALTER TABLE `transactions` DROP COLUMN `blockchain_type_id`;') + + // Create `signature` column - for data from `transaction_signatures` table. + await queryFn( + 'ALTER TABLE `transactions` ADD COLUMN `signature` binary(64) DEFAULT NULL AFTER `received`;', + ) + + // Create `pubkey` column - for data from `transaction_signatures` table. + await queryFn( + 'ALTER TABLE `transactions` ADD COLUMN `pubkey` binary(32) DEFAULT NULL AFTER `signature`;', + ) + + // Transfer data from `transaction_signatures` table to `transactions` table + await queryFn(` + UPDATE transactions + INNER JOIN transaction_signatures ON transactions.id = transaction_signatures.transaction_id + SET transactions.signature = transaction_signatures.signature, transactions.pubkey = transaction_signatures.pubkey; + `) + + // Drop `transaction_signatures` table + await queryFn('DROP TABLE `transaction_signatures`;') +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn(` + CREATE TABLE \`transaction_signatures\` ( + \`id\` int(10) unsigned NOT NULL AUTO_INCREMENT, + \`transaction_id\` int(10) unsigned NOT NULL, + \`signature\` binary(64) NOT NULL, + \`pubkey\` binary(32) NOT NULL, + PRIMARY KEY (\`id\`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci + `) + await queryFn(` + INSERT INTO transaction_signatures (transaction_id, signature, pubkey) + VALUES (SELECT id as transaction_id, signature, pubkey FROM transactions WHERE signature IS NOT NULL and pubkey IS NOT NULL); + `) + await queryFn('ALTER TABLE `transactions` DROP COLUMN `pubkey`;') + await queryFn('ALTER TABLE `transactions` DROP COLUMN `signature`;') + await queryFn( + 'ALTER TABLE `transactions` ADD COLUMN `blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1 AFTER `received` ;', + ) + await queryFn( + 'ALTER TABLE `transactions` ADD COLUMN `state_group_id` int(10) unsigned DEFAULT NULL AFTER `id`;', + ) + // We have to set the correct values previously in the table , since its not the same as the column's default + await queryFn('UPDATE `transactions` SET `state_group_id` = 0;') +}