From 017c17c8a273842a2a92b908f5ece2bd702cee34 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 19 Sep 2024 19:35:05 -0300 Subject: [PATCH] refactor: remove author_search table, put search in author_stats --- .../034_move_author_search_to_author_stats.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/db/migrations/034_move_author_search_to_author_stats.ts diff --git a/src/db/migrations/034_move_author_search_to_author_stats.ts b/src/db/migrations/034_move_author_search_to_author_stats.ts new file mode 100644 index 00000000..6d21ca39 --- /dev/null +++ b/src/db/migrations/034_move_author_search_to_author_stats.ts @@ -0,0 +1,32 @@ +import { Kysely, sql } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema + .alterTable('author_stats') + .addColumn('search', 'text', (col) => col.notNull().defaultTo('')) + .execute(); + + await sql`CREATE INDEX author_stats_search_idx ON author_stats USING GIN (search gin_trgm_ops)`.execute(db); + + await db.insertInto('author_stats') + .columns(['pubkey', 'search']) + .expression( + db.selectFrom('author_search') + .select(['pubkey', 'search']), + ) + .onConflict((oc) => + oc.column('pubkey') + .doUpdateSet((eb) => ({ + search: eb.ref('excluded.search'), + })) + ) + .execute(); + + await db.schema.dropIndex('author_search_search_idx').ifExists().execute(); + await db.schema.dropTable('author_search').execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropIndex('author_stats_search_idx').ifExists().execute(); + await db.schema.alterTable('author_stats').dropColumn('search').execute(); +}