refactor: remove author_search table, put search in author_stats

This commit is contained in:
P. Reis 2024-09-19 19:35:05 -03:00
parent d1f452d87b
commit 017c17c8a2

View file

@ -0,0 +1,32 @@
import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
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<any>): Promise<void> {
await db.schema.dropIndex('author_stats_search_idx').ifExists().execute();
await db.schema.alterTable('author_stats').dropColumn('search').execute();
}