import { BaseSchema } from '@adonisjs/lucid/schema' /** * Spot listings (CONCEPT.md D29): old spots become a release without files * (source `spot`), whose NZB is fetched when first downloaded. Search shows * releases with files or spot listings, so the partial release indexes * cover both. Spots are verified from their overview when fetched. */ const VISIBLE = `(file_count > 0 OR source = 'spot')` const INDEXES: [name: string, definition: string][] = [ ['releases_name_trgm', `USING gin (name gin_trgm_ops)`], ['releases_contents_trgm', `USING gin ((contents::text) gin_trgm_ops)`], ['releases_posted_at_idx', `(posted_at DESC NULLS LAST)`], ['releases_size_index', `(size)`], ['releases_file_count_index', `(file_count)`], ['releases_complete_idx', `(complete)`], ['releases_category_idx', `(category)`], ] export default class extends BaseSchema { static disableTransactions = true async up() { await this.db.rawQuery(`ALTER TABLE spots ADD COLUMN IF NOT EXISTS verified boolean`) // worker:spots claims recent spots first, then the history in posting order. await this.db.rawQuery(`CREATE INDEX CONCURRENTLY IF NOT EXISTS spots_status_article_idx ON spots (status, article_number)`) await this.db.rawQuery(`CREATE INDEX CONCURRENTLY IF NOT EXISTS spots_status_posted_idx ON spots (status, posted_at)`) await this.swap(VISIBLE) } async down() { await this.swap('file_count > 0') await this.db.rawQuery(`DROP INDEX CONCURRENTLY IF EXISTS spots_status_article_idx`) await this.db.rawQuery(`DROP INDEX CONCURRENTLY IF EXISTS spots_status_posted_idx`) await this.db.rawQuery(`ALTER TABLE spots DROP COLUMN IF EXISTS verified`) } private async swap(predicate: string) { for (const [name, definition] of INDEXES) { await this.db.rawQuery(`DROP INDEX CONCURRENTLY IF EXISTS ${name}_next`) await this.db.rawQuery(`CREATE INDEX CONCURRENTLY ${name}_next ON releases ${definition} WHERE ${predicate}`) await this.db.rawQuery(`DROP INDEX CONCURRENTLY IF EXISTS ${name}`) await this.db.rawQuery(`ALTER INDEX ${name}_next RENAME TO ${name}`) } } }