From 2b2cdca28270dfbbdf91e70c8195687440dcbeca Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 20 Sep 2024 09:58:46 -0500 Subject: [PATCH 1/2] Use char(64) for stats primary keys --- src/db/migrations/036_stats64.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/db/migrations/036_stats64.ts diff --git a/src/db/migrations/036_stats64.ts b/src/db/migrations/036_stats64.ts new file mode 100644 index 00000000..bad63ba4 --- /dev/null +++ b/src/db/migrations/036_stats64.ts @@ -0,0 +1,11 @@ +import { Kysely } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('char(64)')).execute(); + await db.schema.alterTable('event_stats').alterColumn('event_id', (col) => col.setDataType('char(64)')).execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('text')).execute(); + await db.schema.alterTable('event_stats').alterColumn('event_id', (col) => col.setDataType('text')).execute(); +} From f0c8096498e7646f596896502877523a07248a96 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 20 Sep 2024 10:06:17 -0500 Subject: [PATCH 2/2] stats64: delete invalid rows first --- src/db/migrations/036_stats64.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/db/migrations/036_stats64.ts b/src/db/migrations/036_stats64.ts index bad63ba4..fa9d357e 100644 --- a/src/db/migrations/036_stats64.ts +++ b/src/db/migrations/036_stats64.ts @@ -1,11 +1,14 @@ -import { Kysely } from 'kysely'; +import { Kysely, sql } from 'kysely'; export async function up(db: Kysely): Promise { - await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('char(64)')).execute(); + await db.deleteFrom('event_stats').where(sql`length(event_id)`, '>', 64).execute(); + await db.deleteFrom('author_stats').where(sql`length(pubkey)`, '>', 64).execute(); + await db.schema.alterTable('event_stats').alterColumn('event_id', (col) => col.setDataType('char(64)')).execute(); + await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('char(64)')).execute(); } export async function down(db: Kysely): Promise { - await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('text')).execute(); await db.schema.alterTable('event_stats').alterColumn('event_id', (col) => col.setDataType('text')).execute(); + await db.schema.alterTable('author_stats').alterColumn('pubkey', (col) => col.setDataType('text')).execute(); }