migrate transaction_signatures table - move data into transactions table

remove two unnecessary fields from transactions table
This commit is contained in:
Ulf Gebhardt 2022-01-29 05:31:16 +01:00
parent 4396080ef5
commit 604340fbec
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
5 changed files with 95 additions and 4 deletions

View File

@ -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
}

View File

@ -1 +1 @@
export { Transaction } from './0001-init_db/Transaction'
export { Transaction } from './0016-transaction_signatures/Transaction'

View File

@ -1 +0,0 @@
export { TransactionSignature } from './0001-init_db/TransactionSignature'

View File

@ -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,

View File

@ -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<Array<any>>) {
// 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<Array<any>>) {
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;')
}