2025-04-23 05:29:42 +00:00
/ *
* SPDX - FileCopyrightText : syuilo and misskey - project
* SPDX - License - Identifier : AGPL - 3.0 - only
* /
2025-05-03 12:57:50 +09:00
import { isConcurrentIndexMigrationEnabled } from "./js/migration-config.js" ;
2025-04-23 05:29:42 +00:00
export class CompositeNoteIndex1745378064470 {
name = 'CompositeNoteIndex1745378064470' ;
2025-05-03 12:57:50 +09:00
transaction = isConcurrentIndexMigrationEnabled ( ) ? false : undefined ;
2025-04-23 05:29:42 +00:00
async up ( queryRunner ) {
2025-05-03 12:57:50 +09:00
const concurrently = isConcurrentIndexMigrationEnabled ( ) ;
if ( concurrently ) {
const hasValidIndex = await queryRunner . query ( ` SELECT indisvalid FROM pg_index INNER JOIN pg_class ON pg_index.indexrelid = pg_class.oid WHERE pg_class.relname = 'IDX_724b311e6f883751f261ebe378' ` ) ;
2025-05-06 20:15:26 +09:00
if ( hasValidIndex . length === 0 || hasValidIndex [ 0 ] . indisvalid !== true ) {
2025-05-03 12:57:50 +09:00
await queryRunner . query ( ` DROP INDEX IF EXISTS "IDX_724b311e6f883751f261ebe378" ` ) ;
await queryRunner . query ( ` CREATE INDEX CONCURRENTLY "IDX_724b311e6f883751f261ebe378" ON "note" ("userId", "id" DESC) ` ) ;
}
} else {
await queryRunner . query ( ` CREATE INDEX IF NOT EXISTS "IDX_724b311e6f883751f261ebe378" ON "note" ("userId", "id" DESC) ` ) ;
}
2025-04-23 05:29:42 +00:00
await queryRunner . query ( ` DROP INDEX IF EXISTS "IDX_5b87d9d19127bd5d92026017a7" ` ) ;
// Flush all cached Linear Scan Plans and redo statistics for composite index
// this is important for Postgres to learn that even in highly complex queries, using this index first can reduce the result set significantly
await queryRunner . query ( ` ANALYZE "user", "note" ` ) ;
}
async down ( queryRunner ) {
2025-05-03 12:57:50 +09:00
const mayConcurrently = isConcurrentIndexMigrationEnabled ( ) ? 'CONCURRENTLY' : '' ;
2025-04-23 05:29:42 +00:00
await queryRunner . query ( ` DROP INDEX IF EXISTS "IDX_724b311e6f883751f261ebe378" ` ) ;
2025-05-03 12:57:50 +09:00
await queryRunner . query ( ` CREATE INDEX ${ mayConcurrently } "IDX_5b87d9d19127bd5d92026017a7" ON "note" ("userId") ` ) ;
2025-04-23 05:29:42 +00:00
}
}