feat: add MISSKEY_MIGRATION_CREATE_INDEX_CONCURRENTLY option
This commit is contained in:
parent
6b21c249d4
commit
c37c4fb51b
|
@ -3,12 +3,15 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { isConcurrentIndexMigrationEnabled } from "./js/migration-config.js";
|
||||||
|
|
||||||
export class CompositeNoteIndex1745378064470 {
|
export class CompositeNoteIndex1745378064470 {
|
||||||
name = 'CompositeNoteIndex1745378064470';
|
name = 'CompositeNoteIndex1745378064470';
|
||||||
transaction = false;
|
transaction = isConcurrentIndexMigrationEnabled() ? false : undefined;
|
||||||
|
|
||||||
async up(queryRunner) {
|
async up(queryRunner) {
|
||||||
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_724b311e6f883751f261ebe378" ON "note" ("userId", "id" DESC)`);
|
const mayConcurrently = isConcurrentIndexMigrationEnabled() ? 'CONCURRENTLY' : '';
|
||||||
|
await queryRunner.query(`CREATE INDEX ${mayConcurrently} "IDX_724b311e6f883751f261ebe378" ON "note" ("userId", "id" DESC)`);
|
||||||
await queryRunner.query(`DROP INDEX IF EXISTS "IDX_5b87d9d19127bd5d92026017a7"`);
|
await queryRunner.query(`DROP INDEX IF EXISTS "IDX_5b87d9d19127bd5d92026017a7"`);
|
||||||
// Flush all cached Linear Scan Plans and redo statistics for composite index
|
// 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
|
// this is important for Postgres to learn that even in highly complex queries, using this index first can reduce the result set significantly
|
||||||
|
@ -16,7 +19,8 @@ export class CompositeNoteIndex1745378064470 {
|
||||||
}
|
}
|
||||||
|
|
||||||
async down(queryRunner) {
|
async down(queryRunner) {
|
||||||
|
const mayConcurrently = isConcurrentIndexMigrationEnabled() ? 'CONCURRENTLY' : '';
|
||||||
await queryRunner.query(`DROP INDEX IF EXISTS "IDX_724b311e6f883751f261ebe378"`);
|
await queryRunner.query(`DROP INDEX IF EXISTS "IDX_724b311e6f883751f261ebe378"`);
|
||||||
await queryRunner.query(`CREATE INDEX CONCURRENTLY "IDX_5b87d9d19127bd5d92026017a7" ON "note" ("userId")`);
|
await queryRunner.query(`CREATE INDEX ${mayConcurrently} "IDX_5b87d9d19127bd5d92026017a7" ON "note" ("userId")`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
export function isConcurrentIndexMigrationEnabled() {
|
||||||
|
return process.env.MISSKEY_MIGRATION_CREATE_INDEX_CONCURRENTLY === '1';
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { loadConfig } from './built/config.js';
|
import { loadConfig } from './built/config.js';
|
||||||
import { entities } from './built/postgres.js';
|
import { entities } from './built/postgres.js';
|
||||||
|
import { isConcurrentIndexMigrationEnabled } from "./migration/js/migration-config.js";
|
||||||
|
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
|
||||||
|
@ -14,5 +15,5 @@ export default new DataSource({
|
||||||
extra: config.db.extra,
|
extra: config.db.extra,
|
||||||
entities: entities,
|
entities: entities,
|
||||||
migrations: ['migration/*.js'],
|
migrations: ['migration/*.js'],
|
||||||
migrationsTransactionMode: 'each',
|
migrationsTransactionMode: isConcurrentIndexMigrationEnabled() ? 'each' : 'all',
|
||||||
});
|
});
|
||||||
|
|
|
@ -24,8 +24,13 @@ const $config: Provider = {
|
||||||
const $db: Provider = {
|
const $db: Provider = {
|
||||||
provide: DI.db,
|
provide: DI.db,
|
||||||
useFactory: async (config) => {
|
useFactory: async (config) => {
|
||||||
|
try {
|
||||||
const db = createPostgresDataSource(config);
|
const db = createPostgresDataSource(config);
|
||||||
return await db.initialize();
|
return await db.initialize();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
inject: [DI.config],
|
inject: [DI.config],
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue