From ad1225998fc712f33a5c50afdb0466337790f39d Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 30 May 2023 10:59:47 +0200 Subject: [PATCH 01/14] created migration to correct public key length --- .../0067-community_table_public_key_length.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 database/migrations/0067-community_table_public_key_length.ts diff --git a/database/migrations/0067-community_table_public_key_length.ts b/database/migrations/0067-community_table_public_key_length.ts new file mode 100644 index 000000000..b51a1e6e0 --- /dev/null +++ b/database/migrations/0067-community_table_public_key_length.ts @@ -0,0 +1,23 @@ +/* MIGRATION TO CORRECT THE PUBLIC KEY LENGTHS + * + * This migration corrects the length of the saved public keys to 32 as this is the length it is generated for. + */ + +/* 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 `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') + // TODO: it is unclear if this is actually nullable - the model defines "default: null, nullable: true", but the table seems to be created without nullability(?) + await queryFn( + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', + ) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;') + // TODO: see above + await queryFn( + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', + ) +} From fd5716f9375f6e2a3db03ec537eb84952cea024a Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 30 May 2023 11:00:16 +0200 Subject: [PATCH 02/14] define new models for correct key-length --- .../Community.ts | 60 +++++++++++++++++++ .../FederatedCommunity.ts | 51 ++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 database/entity/0067-community_table_public_key_length/Community.ts create mode 100644 database/entity/0067-community_table_public_key_length/FederatedCommunity.ts diff --git a/database/entity/0067-community_table_public_key_length/Community.ts b/database/entity/0067-community_table_public_key_length/Community.ts new file mode 100644 index 000000000..382025c83 --- /dev/null +++ b/database/entity/0067-community_table_public_key_length/Community.ts @@ -0,0 +1,60 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('communities') +export class Community extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'url', length: 255, nullable: false }) + url: string + + @Column({ name: 'public_key', type: 'binary', length: 32, nullable: false }) + publicKey: Buffer + + @Column({ + name: 'community_uuid', + type: 'char', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + communityUuid: string | null + + @Column({ name: 'authenticated_at', type: 'datetime', nullable: true }) + authenticatedAt: Date | null + + @Column({ name: 'name', type: 'varchar', length: 40, nullable: true }) + name: string | null + + @Column({ name: 'description', type: 'varchar', length: 255, nullable: true }) + description: string | null + + @CreateDateColumn({ name: 'creation_date', type: 'datetime', nullable: true }) + creationDate: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/0067-community_table_public_key_length/FederatedCommunity.ts b/database/entity/0067-community_table_public_key_length/FederatedCommunity.ts new file mode 100644 index 000000000..7dd49a1d6 --- /dev/null +++ b/database/entity/0067-community_table_public_key_length/FederatedCommunity.ts @@ -0,0 +1,51 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('federated_communities') +export class FederatedCommunity extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'public_key', type: 'binary', length: 32, default: null, nullable: true }) + publicKey: Buffer + + @Column({ name: 'api_version', length: 10, nullable: false }) + apiVersion: string + + @Column({ name: 'end_point', length: 255, nullable: false }) + endPoint: string + + @Column({ name: 'last_announced_at', type: 'datetime', nullable: true }) + lastAnnouncedAt: Date | null + + @Column({ name: 'verified_at', type: 'datetime', nullable: true }) + verifiedAt: Date | null + + @Column({ name: 'last_error_at', type: 'datetime', nullable: true }) + lastErrorAt: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} From 1d9e5e1bf19d221960eec789491d0ecf5f389e44 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 30 May 2023 11:01:04 +0200 Subject: [PATCH 03/14] corrected entity links --- database/entity/Community.ts | 2 +- database/entity/FederatedCommunity.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/database/entity/Community.ts b/database/entity/Community.ts index ee08323b6..05361cfbf 100644 --- a/database/entity/Community.ts +++ b/database/entity/Community.ts @@ -1 +1 @@ -export { Community } from './0065-refactor_communities_table/Community' +export { Community } from './0067-community_table_public_key_length/Community' diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts index cacaaff9c..b2b6e23fa 100644 --- a/database/entity/FederatedCommunity.ts +++ b/database/entity/FederatedCommunity.ts @@ -1 +1 @@ -export { FederatedCommunity } from './0065-refactor_communities_table/FederatedCommunity' +export { FederatedCommunity } from './0067-community_table_public_key_length/FederatedCommunity' From 0431e40e488de5bee410a3d09ec039fde6cea4e7 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 30 May 2023 11:01:55 +0200 Subject: [PATCH 04/14] updated required database version --- backend/src/config/index.ts | 2 +- dht-node/src/config/index.ts | 2 +- federation/src/config/index.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 8d1ed8ae6..dd092a532 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0066-x-community-sendcoins-transactions_table', + DB_VERSION: '0067-community_table_public_key_length', 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/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 43949201b..6d9c1fd46 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: '0066-x-community-sendcoins-transactions_table', + DB_VERSION: '0067-community_table_public_key_length', 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 66d8a056c..429a6cf9b 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0066-x-community-sendcoins-transactions_table', + DB_VERSION: '0067-community_table_public_key_length', // 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 From 2a48c34fd8e4efdb9c2643413bd75401126d24d8 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 30 May 2023 11:08:04 +0200 Subject: [PATCH 05/14] corrected migration name since two tables are affected --- backend/src/config/index.ts | 2 +- .../Community.ts | 0 .../FederatedCommunity.ts | 0 database/entity/Community.ts | 2 +- database/entity/FederatedCommunity.ts | 2 +- ...key_length.ts => 0067-community_tables_public_key_length.ts} | 0 dht-node/src/config/index.ts | 2 +- federation/src/config/index.ts | 2 +- 8 files changed, 5 insertions(+), 5 deletions(-) rename database/entity/{0067-community_table_public_key_length => 0067-community_tables_public_key_length}/Community.ts (100%) rename database/entity/{0067-community_table_public_key_length => 0067-community_tables_public_key_length}/FederatedCommunity.ts (100%) rename database/migrations/{0067-community_table_public_key_length.ts => 0067-community_tables_public_key_length.ts} (100%) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index dd092a532..d12e5adad 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0067-community_table_public_key_length', + DB_VERSION: '0067-community_tables_public_key_length', 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/0067-community_table_public_key_length/Community.ts b/database/entity/0067-community_tables_public_key_length/Community.ts similarity index 100% rename from database/entity/0067-community_table_public_key_length/Community.ts rename to database/entity/0067-community_tables_public_key_length/Community.ts diff --git a/database/entity/0067-community_table_public_key_length/FederatedCommunity.ts b/database/entity/0067-community_tables_public_key_length/FederatedCommunity.ts similarity index 100% rename from database/entity/0067-community_table_public_key_length/FederatedCommunity.ts rename to database/entity/0067-community_tables_public_key_length/FederatedCommunity.ts diff --git a/database/entity/Community.ts b/database/entity/Community.ts index 05361cfbf..ab877faa8 100644 --- a/database/entity/Community.ts +++ b/database/entity/Community.ts @@ -1 +1 @@ -export { Community } from './0067-community_table_public_key_length/Community' +export { Community } from './0067-community_tables_public_key_length/Community' diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts index b2b6e23fa..3b636af09 100644 --- a/database/entity/FederatedCommunity.ts +++ b/database/entity/FederatedCommunity.ts @@ -1 +1 @@ -export { FederatedCommunity } from './0067-community_table_public_key_length/FederatedCommunity' +export { FederatedCommunity } from './0067-community_tables_public_key_length/FederatedCommunity' diff --git a/database/migrations/0067-community_table_public_key_length.ts b/database/migrations/0067-community_tables_public_key_length.ts similarity index 100% rename from database/migrations/0067-community_table_public_key_length.ts rename to database/migrations/0067-community_tables_public_key_length.ts diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 6d9c1fd46..ce939e96c 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: '0067-community_table_public_key_length', + DB_VERSION: '0067-community_tables_public_key_length', 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 429a6cf9b..0ce9945dd 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0067-community_table_public_key_length', + DB_VERSION: '0067-community_tables_public_key_length', // 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 From 63c407fc29c19140c6e8eea0eac6b67c78c6176f Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 7 Jun 2023 10:28:53 +0200 Subject: [PATCH 06/14] fix key length in tests --- .../federation/validateCommunities.test.ts | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/backend/src/federation/validateCommunities.test.ts b/backend/src/federation/validateCommunities.test.ts index 834f37e16..ef2c30d6b 100644 --- a/backend/src/federation/validateCommunities.test.ts +++ b/backend/src/federation/validateCommunities.test.ts @@ -73,9 +73,7 @@ describe('validate Communities', () => { } as Response }) const variables1 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -108,7 +106,7 @@ describe('validate Communities', () => { expect(logger.warn).toBeCalledWith( 'Federation: received not matching publicKey:', 'somePubKey', - expect.stringMatching('1111111111111111111111111111111111111111111111111111111111111111'), + expect.stringMatching('11111111111111111111111111111111'), ) }) }) @@ -120,15 +118,13 @@ describe('validate Communities', () => { return { data: { getPublicKey: { - publicKey: '1111111111111111111111111111111111111111111111111111111111111111', + publicKey: '11111111111111111111111111111111', }, }, } as Response }) const variables1 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -174,15 +170,13 @@ describe('validate Communities', () => { return { data: { getPublicKey: { - publicKey: '1111111111111111111111111111111111111111111111111111111111111111', + publicKey: '11111111111111111111111111111111', }, }, } as Response }) const variables2 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_1', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -222,9 +216,7 @@ describe('validate Communities', () => { let dbCom: DbFederatedCommunity beforeEach(async () => { const variables3 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '2_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), From 760c0ea9d9451dec42f8d24e36db23aa0c8532d6 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 7 Jun 2023 11:37:26 +0200 Subject: [PATCH 07/14] fix dht-node to handle pubkey as 32 bit buffer --- dht-node/src/dht_node/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index 36291904a..7e326c33f 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -94,7 +94,7 @@ export const startDHT = async (topic: string): Promise => { const variables = { apiVersion: recApiVersion.api, endPoint: recApiVersion.url, - publicKey: socket.remotePublicKey.toString('hex'), + publicKey: socket.remotePublicKey, lastAnnouncedAt: new Date(), } logger.debug(`upsert with variables=${JSON.stringify(variables)}`) @@ -195,14 +195,14 @@ async function writeFederatedHomeCommunityEntries(pubKey: string): Promise { // check for existing homeCommunity entry let homeCom = await DbCommunity.findOne({ foreign: false, - publicKey: Buffer.from(pubKey), + publicKey: Buffer.from(pubKey, 'hex'), }) if (!homeCom) { // check if a homecommunity with a different publicKey still exists @@ -225,7 +225,7 @@ async function writeHomeCommunityEntry(pubKey: string): Promise { } if (homeCom) { // simply update the existing entry, but it MUST keep the ID and UUID because of possible relations - homeCom.publicKey = Buffer.from(pubKey) + homeCom.publicKey = Buffer.from(pubKey, 'hex') homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL + '/api/' homeCom.name = CONFIG.COMMUNITY_NAME homeCom.description = CONFIG.COMMUNITY_DESCRIPTION @@ -235,7 +235,7 @@ async function writeHomeCommunityEntry(pubKey: string): Promise { // insert a new homecommunity entry including a new ID and a new but ensured unique UUID homeCom = new DbCommunity() homeCom.foreign = false - homeCom.publicKey = Buffer.from(pubKey) + homeCom.publicKey = Buffer.from(pubKey, 'hex') homeCom.communityUuid = await newCommunityUuid() homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL + '/api/' homeCom.name = CONFIG.COMMUNITY_NAME From a8407618bd2218e08110f0bb5f47c113f35b3971 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 20 Jun 2023 09:18:41 +0200 Subject: [PATCH 08/14] merge conflicts solved --- .../Community.ts | 3 +++ .../FederatedCommunity.ts | 0 database/entity/FederatedCommunity.ts | 2 +- ...ey_length.ts => 0068-community_tables_public_key_length.ts} | 0 4 files changed, 4 insertions(+), 1 deletion(-) rename database/entity/{0067-community_tables_public_key_length => 0068-community_tables_public_key_length}/Community.ts (93%) rename database/entity/{0067-community_tables_public_key_length => 0068-community_tables_public_key_length}/FederatedCommunity.ts (100%) rename database/migrations/{0067-community_tables_public_key_length.ts => 0068-community_tables_public_key_length.ts} (100%) diff --git a/database/entity/0067-community_tables_public_key_length/Community.ts b/database/entity/0068-community_tables_public_key_length/Community.ts similarity index 93% rename from database/entity/0067-community_tables_public_key_length/Community.ts rename to database/entity/0068-community_tables_public_key_length/Community.ts index 382025c83..e6f8d255c 100644 --- a/database/entity/0067-community_tables_public_key_length/Community.ts +++ b/database/entity/0068-community_tables_public_key_length/Community.ts @@ -21,6 +21,9 @@ export class Community extends BaseEntity { @Column({ name: 'public_key', type: 'binary', length: 32, nullable: false }) publicKey: Buffer + @Column({ name: 'private_key', type: 'binary', length: 64, nullable: true }) + privateKey: Buffer | null + @Column({ name: 'community_uuid', type: 'char', diff --git a/database/entity/0067-community_tables_public_key_length/FederatedCommunity.ts b/database/entity/0068-community_tables_public_key_length/FederatedCommunity.ts similarity index 100% rename from database/entity/0067-community_tables_public_key_length/FederatedCommunity.ts rename to database/entity/0068-community_tables_public_key_length/FederatedCommunity.ts diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts index 3b636af09..37a33e4d9 100644 --- a/database/entity/FederatedCommunity.ts +++ b/database/entity/FederatedCommunity.ts @@ -1 +1 @@ -export { FederatedCommunity } from './0067-community_tables_public_key_length/FederatedCommunity' +export { FederatedCommunity } from './0068-community_tables_public_key_length/FederatedCommunity' diff --git a/database/migrations/0067-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts similarity index 100% rename from database/migrations/0067-community_tables_public_key_length.ts rename to database/migrations/0068-community_tables_public_key_length.ts From 580c5940d27f2278a3a0bfb1d3d1b3b6225b8a5a Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 20 Jun 2023 09:39:54 +0200 Subject: [PATCH 09/14] covert public_key according to new standarts unhex/hex --- database/migrations/0068-community_tables_public_key_length.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index b51a1e6e0..df70b1182 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -7,6 +7,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn('UPDATE `communities` SET `public_key` = UNHEX(publicKey);') await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') // TODO: it is unclear if this is actually nullable - the model defines "default: null, nullable: true", but the table seems to be created without nullability(?) await queryFn( @@ -16,6 +17,7 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;') + await queryFn('UPDATE `communities` SET `public_key` = HEX(publicKey);') // TODO: see above await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', From d23e1a5aad9d409696ec42631c7a2df660134d8a Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 20 Jun 2023 10:42:39 +0200 Subject: [PATCH 10/14] removed obsolete todos --- database/migrations/0068-community_tables_public_key_length.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index df70b1182..413af935f 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -9,7 +9,6 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn('UPDATE `communities` SET `public_key` = UNHEX(publicKey);') await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') - // TODO: it is unclear if this is actually nullable - the model defines "default: null, nullable: true", but the table seems to be created without nullability(?) await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', ) @@ -18,7 +17,6 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;') await queryFn('UPDATE `communities` SET `public_key` = HEX(publicKey);') - // TODO: see above await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', ) From 6c3b784d45ad7533b553ca16ddfec6f38f662743 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 20 Jun 2023 10:50:48 +0200 Subject: [PATCH 11/14] corrected database migration --- .../migrations/0068-community_tables_public_key_length.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index 413af935f..b87ed1a70 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -7,7 +7,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { - await queryFn('UPDATE `communities` SET `public_key` = UNHEX(publicKey);') + await queryFn('UPDATE `communities` SET `public_key` = UNHEX(public_key);') await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', @@ -16,7 +16,7 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;') - await queryFn('UPDATE `communities` SET `public_key` = HEX(publicKey);') + await queryFn('UPDATE `communities` SET `public_key` = HEX(public_key);') await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', ) From 52925d4ba75c80f58634fa00d57f39fe3f880712 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 30 Jun 2023 13:10:51 +0200 Subject: [PATCH 12/14] convert the right table --- .../0068-community_tables_public_key_length.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index b87ed1a70..236fc4430 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -7,17 +7,21 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { - await queryFn('UPDATE `communities` SET `public_key` = UNHEX(public_key);') - await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') + await queryFn('UPDATE `federated_communities` SET `public_key` = UNHEX(public_key);') await queryFn( - 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;', + ) + await queryFn( + 'ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', ) } export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { - await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;') - await queryFn('UPDATE `communities` SET `public_key` = HEX(public_key);') await queryFn( - 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;', + ) + await queryFn('UPDATE `federated_communities` SET `public_key` = HEX(public_key);') + await queryFn( + 'ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', ) } From bd384a75bfbcc5bc7c7ac2720d6eed5cc77e004b Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 30 Jun 2023 13:29:51 +0200 Subject: [PATCH 13/14] fix migration --- .../0068-community_tables_public_key_length.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index 236fc4430..2d82307fa 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -8,12 +8,14 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn('UPDATE `federated_communities` SET `public_key` = UNHEX(public_key);') - await queryFn( - 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;', - ) - await queryFn( - 'ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NULL DEFAULT NULL;', - ) + try { + await queryFn( + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;', + ) + await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') + } catch(e){ + console.log(e) + } } export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { From 8688385a6f2e158697765fee2fb6e65558c1d9a1 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 30 Jun 2023 14:25:03 +0200 Subject: [PATCH 14/14] fixed migration --- ...0068-community_tables_public_key_length.ts | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts index 2d82307fa..22c04e850 100644 --- a/database/migrations/0068-community_tables_public_key_length.ts +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -7,23 +7,36 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // federated communities + await queryFn('DROP INDEX `public_api_key` ON `federated_communities`;') await queryFn('UPDATE `federated_communities` SET `public_key` = UNHEX(public_key);') - try { - await queryFn( - 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;', - ) - await queryFn('ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(32) NOT NULL;') - } catch(e){ - console.log(e) - } + await queryFn( + 'ALTER TABLE `federated_communities` ADD COLUMN `public_key_new` binary(32) NOT NULL AFTER `public_key`;', + ) + await queryFn('UPDATE `federated_communities` SET public_key_new = substring(public_key,1,32);') + await queryFn('ALTER TABLE `federated_communities` DROP COLUMN public_key;') + await queryFn('ALTER TABLE `federated_communities` RENAME COLUMN public_key_new TO public_key;') + await queryFn( + 'ALTER TABLE `federated_communities` ADD CONSTRAINT `public_api_key` UNIQUE (public_key, api_version);', + ) + + // communities + await queryFn( + 'ALTER TABLE `communities` ADD COLUMN `public_key_new` binary(32) NOT NULL AFTER `public_key`;', + ) + await queryFn('UPDATE `communities` SET public_key_new = substring(public_key,1,32);') + await queryFn('ALTER TABLE `communities` DROP COLUMN public_key;') + await queryFn('ALTER TABLE `communities` RENAME COLUMN public_key_new TO public_key;') } export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { await queryFn( 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;', ) - await queryFn('UPDATE `federated_communities` SET `public_key` = HEX(public_key);') + await queryFn( + 'UPDATE `federated_communities` SET `public_key` = substring(HEX(public_key),1,64);', + ) await queryFn( 'ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', ) -} +} \ No newline at end of file