mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
refactor db
This commit is contained in:
parent
c113d95a3c
commit
35670ded96
@ -9,7 +9,7 @@ import {
|
||||
} from 'typeorm'
|
||||
import { User } from '../User'
|
||||
import { TransactionRecipe } from './TransactionRecipe'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
import { ConfirmedTransaction } from './ConfirmedTransaction'
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@ -11,7 +11,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
import { Account } from './Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { TransactionRecipe } from './TransactionRecipe'
|
||||
|
||||
@Entity('confirmed_transactions')
|
||||
export class ConfirmedTransaction extends BaseEntity {
|
||||
|
||||
@ -12,7 +12,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
import { Account } from './Account'
|
||||
import { Community } from './Community'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
import { ConfirmedTransaction } from './ConfirmedTransaction'
|
||||
|
||||
@Entity('transaction_recipes')
|
||||
export class TransactionRecipe extends BaseEntity {
|
||||
|
||||
@ -8,8 +8,8 @@ import {
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { User } from '../User'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { ConfirmedTransaction } from '../ConfirmedTransaction'
|
||||
import { TransactionRecipe } from '../0001-init_db/TransactionRecipe'
|
||||
import { ConfirmedTransaction } from './ConfirmedTransaction'
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@ -8,7 +8,7 @@ import {
|
||||
BaseEntity,
|
||||
} from 'typeorm'
|
||||
import { Account } from '../Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { TransactionRecipe } from '../0001-init_db/TransactionRecipe'
|
||||
import { AccountCommunity } from '../AccountCommunity'
|
||||
|
||||
@Entity('communities')
|
||||
|
||||
@ -11,7 +11,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
|
||||
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
|
||||
import { Account } from './Account'
|
||||
import { TransactionRecipe } from '../TransactionRecipe'
|
||||
import { TransactionRecipe } from '../0001-init_db/TransactionRecipe'
|
||||
|
||||
@Entity('confirmed_transactions')
|
||||
export class ConfirmedTransaction extends BaseEntity {
|
||||
|
||||
@ -35,14 +35,10 @@ export class Account extends BaseEntity {
|
||||
@Column({ type: 'tinyint', unsigned: true })
|
||||
type: number
|
||||
|
||||
@Column({
|
||||
name: 'created_at',
|
||||
type: 'datetime',
|
||||
precision: 3,
|
||||
default: () => 'CURRENT_TIMESTAMP(3)',
|
||||
})
|
||||
@Column({ name: 'created_at', type: 'datetime', precision: 3 })
|
||||
createdAt: Date
|
||||
|
||||
// use timestamp from iota milestone which is only in seconds precision, so no need to use 3 Bytes extra here
|
||||
@Column({ name: 'confirmed_at', type: 'datetime', nullable: true })
|
||||
confirmedAt?: Date
|
||||
|
||||
@ -56,10 +52,11 @@ export class Account extends BaseEntity {
|
||||
})
|
||||
balanceConfirmedAt: Decimal
|
||||
|
||||
// use timestamp from iota milestone which is only in seconds precision, so no need to use 3 Bytes extra here
|
||||
@Column({
|
||||
name: 'balance_confirmed_at_date',
|
||||
type: 'datetime',
|
||||
default: () => 'CURRENT_TIMESTAMP()',
|
||||
nullable: true,
|
||||
})
|
||||
balanceConfirmedAtDate: Date
|
||||
|
||||
@ -77,7 +74,6 @@ export class Account extends BaseEntity {
|
||||
name: 'balance_created_at_date',
|
||||
type: 'datetime',
|
||||
precision: 3,
|
||||
default: () => 'CURRENT_TIMESTAMP(3)',
|
||||
})
|
||||
balanceCreatedAtDate: Date
|
||||
|
||||
|
||||
@ -45,14 +45,10 @@ export class Community extends BaseEntity {
|
||||
@JoinColumn({ name: 'auf_account_id' })
|
||||
aufAccount?: Account
|
||||
|
||||
@Column({
|
||||
name: 'created_at',
|
||||
type: 'datetime',
|
||||
precision: 3,
|
||||
default: () => 'CURRENT_TIMESTAMP(3)',
|
||||
})
|
||||
@Column({ name: 'created_at', type: 'datetime', precision: 3 })
|
||||
createdAt: Date
|
||||
|
||||
// use timestamp from iota milestone which is only in seconds precision, so no need to use 3 Bytes extra here
|
||||
@Column({ name: 'confirmed_at', type: 'datetime', nullable: true })
|
||||
confirmedAt?: Date
|
||||
|
||||
|
||||
@ -117,6 +117,7 @@ export class Transaction extends BaseEntity {
|
||||
@Column({ name: 'iota_milestone', type: 'bigint', nullable: true })
|
||||
iotaMilestone?: number
|
||||
|
||||
// use timestamp from iota milestone which is only in seconds precision, so no need to use 3 Bytes extra here
|
||||
@Column({ name: 'confirmed_at', type: 'datetime', nullable: true })
|
||||
confirmedAt?: Date
|
||||
}
|
||||
|
||||
35
dlt-database/entity/0003-refactor_transaction_recipe/User.ts
Normal file
35
dlt-database/entity/0003-refactor_transaction_recipe/User.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany, JoinColumn } from 'typeorm'
|
||||
|
||||
import { Account } from '../Account'
|
||||
|
||||
@Entity('users', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' })
|
||||
export class User extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('increment', { unsigned: true })
|
||||
id: number
|
||||
|
||||
@Column({
|
||||
name: 'gradido_id',
|
||||
length: 36,
|
||||
nullable: true,
|
||||
collation: 'utf8mb4_unicode_ci',
|
||||
})
|
||||
gradidoID?: string
|
||||
|
||||
@Column({ name: 'derive1_pubkey', type: 'binary', length: 32, unique: true })
|
||||
derive1Pubkey: Buffer
|
||||
|
||||
@Column({ name: 'created_at', type: 'datetime', precision: 3 })
|
||||
createdAt: Date
|
||||
|
||||
// use timestamp from iota milestone which is only in seconds precision, so no need to use 3 Bytes extra here
|
||||
@Column({
|
||||
name: 'confirmed_at',
|
||||
type: 'datetime',
|
||||
nullable: true,
|
||||
})
|
||||
confirmedAt?: Date
|
||||
|
||||
@OneToMany(() => Account, (account) => account.user)
|
||||
@JoinColumn({ name: 'user_id' })
|
||||
accounts?: Account[]
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export { ConfirmedTransaction } from './0002-refactor_add_community/ConfirmedTransaction'
|
||||
@ -1 +0,0 @@
|
||||
export { TransactionRecipe } from './0001-init_db/TransactionRecipe'
|
||||
@ -1 +1 @@
|
||||
export { User } from './0002-refactor_add_community/User'
|
||||
export { User } from './0003-refactor_transaction_recipe/User'
|
||||
|
||||
@ -23,7 +23,7 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
\`confirmed_at\` datetime(3) DEFAULT NULL,
|
||||
PRIMARY KEY (\`id\`),
|
||||
INDEX \`gradido_id\` (\`gradido_id\`),
|
||||
UNIQUE KEY \`pubkey\` (\`pubkey\`)
|
||||
UNIQUE KEY \`derive1_pubkey\` (\`derive1_pubkey\`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`)
|
||||
|
||||
await queryFn(`
|
||||
@ -38,7 +38,7 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
\`balance\` decimal(40,20) NOT NULL DEFAULT 0,
|
||||
\`balance_date\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
PRIMARY KEY (\`id\`),
|
||||
UNIQUE KEY \`pubkey\` (\`pubkey\`),
|
||||
UNIQUE KEY \`derive2_pubkey\` (\`derive2_pubkey\`),
|
||||
FOREIGN KEY (\`user_id\`) REFERENCES users(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
`)
|
||||
@ -56,7 +56,7 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
\`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
\`confirmed_at\` datetime(3) DEFAULT NULL,
|
||||
PRIMARY KEY (\`id\`),
|
||||
UNIQUE KEY \`pubkey\` (\`pubkey\`),
|
||||
UNIQUE KEY \`root_pubkey\` (\`root_pubkey\`),
|
||||
FOREIGN KEY (\`gmw_account_id\`) REFERENCES accounts(id),
|
||||
FOREIGN KEY (\`auf_account_id\`) REFERENCES accounts(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`)
|
||||
|
||||
@ -9,17 +9,21 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
await queryFn(`
|
||||
ALTER TABLE \`accounts\`
|
||||
RENAME COLUMN \`balance\` TO \`balance_confirmed_at\`,
|
||||
RENAME COLUMN \`balance_date\` TO \`balance_confirmed_at_date\`,
|
||||
RENAME COLUMN \`balance_date\` TO \`balance_confirmed_at_date\`
|
||||
;
|
||||
`)
|
||||
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` MODIFY COLUMN \`derivation_index\` int(10) unsigned NULL DEFAULT NULL;`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` ADD COLUMN \`balance_created_at\` decimal(40,20) NOT NULL DEFAULT 0 AFTER \`balance_date\`;`,
|
||||
`ALTER TABLE \`accounts\` ADD COLUMN \`balance_created_at\` decimal(40,20) NOT NULL DEFAULT 0 AFTER \`balance_confirmed_at_date\`;`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` ADD COLUMN \`balance_created_at_date\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) AFTER \`balance_created_at\`;`,
|
||||
`ALTER TABLE \`accounts\` ADD COLUMN \`balance_created_at_date\` datetime(3) NOT NULL AFTER \`balance_created_at\`;`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` MODIFY COLUMN \`balance_confirmed_at_date\` datetime NULL DEFAULT NULL;`,
|
||||
)
|
||||
|
||||
await queryFn(
|
||||
@ -55,6 +59,14 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
)
|
||||
|
||||
await queryFn(`ALTER TABLE \`communities\` ADD UNIQUE(\`iota_topic\`);`)
|
||||
|
||||
await queryFn(`ALTER TABLE \`users\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL;`)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`communities\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL;`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL;`,
|
||||
)
|
||||
}
|
||||
|
||||
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||
@ -94,10 +106,14 @@ export async function downgrade(queryFn: (query: string, values?: any[]) => Prom
|
||||
FOREIGN KEY (\`account_id\`) REFERENCES accounts(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`)
|
||||
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` MODIFY COLUMN \`balance_confirmed_at_date\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);`,
|
||||
)
|
||||
await queryFn(`
|
||||
ALTER TABLE \`accounts\`
|
||||
RENAME COLUMN \`balance_confirmed_at\` TO \`balance\`,
|
||||
RENAME COLUMN \`balance_confirmed_at_date\` TO \`balance_date\`,
|
||||
RENAME COLUMN \`balance_confirmed_at_date\` TO \`balance_date\`
|
||||
;
|
||||
`)
|
||||
|
||||
await queryFn(
|
||||
@ -106,4 +122,14 @@ export async function downgrade(queryFn: (query: string, values?: any[]) => Prom
|
||||
await queryFn(`ALTER TABLE \`accounts\` DROP COLUMN \`balance_created_at\`;`)
|
||||
await queryFn(`ALTER TABLE \`accounts\` DROP COLUMN \`balance_created_at_date\`;`)
|
||||
await queryFn(`DROP TABLE \`transactions\`;`)
|
||||
|
||||
await queryFn(
|
||||
`ALTER TABLE \`users\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`communities\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);`,
|
||||
)
|
||||
await queryFn(
|
||||
`ALTER TABLE \`accounts\` CHANGE \`created_at\` \`created_at\` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);`,
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user