Fixed reloading database schema for sqlite by reconnecting and fixing postgres migration

This commit is contained in:
Omar Ajoue 2021-01-04 17:51:39 +01:00
parent e18ecbed6a
commit 81780cfe19
2 changed files with 18 additions and 14 deletions

View file

@ -164,7 +164,7 @@ export async function init(): Promise<IDatabaseCollections> {
CreateIndexStoppedAt1594825041918,
MakeStoppedAtNullable1607431743769,
],
migrationsRun: true,
migrationsRun: false, // migrations for sqlite will be ran manually for now; see below
migrationsTableName: `${entityPrefix}migrations`,
};
break;
@ -181,27 +181,26 @@ export async function init(): Promise<IDatabaseCollections> {
let connection = await createConnection(connectionOptions);
let mustReconnect = false;
if (dbType === 'sqlite') {
// This specific migration changes database metadata.
// A field is now nullable. We need to reconnect so that
// n8n knows it has changed. Happens only on sqlite.
const migrations = await connection.query('SELECT id FROM migrations where name = "MakeStoppedAtNullable1607431743769"');
const migrations = await connection.query(`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`);
// If you remove this call, remember to turn back on the
// setting to run migrations automatically above.
await connection.runMigrations({
transaction: 'none',
});
if (migrations.length === 0) {
mustReconnect = true;
await connection.close();
connection = await createConnection(connectionOptions);
}
}
await connection.runMigrations({
transaction: 'none',
});
if (mustReconnect) {
console.log('is reconnecting');
await connection.close();
connection = await createConnection(connectionOptions);
}
collections.Credentials = getRepository(entities.CredentialsEntity);

View file

@ -3,10 +3,15 @@ import {MigrationInterface, QueryRunner} from "typeorm";
import * as config from '../../../../config';
export class MakeStoppedAtNullable1607431743768 implements MigrationInterface {
name = 'MakeStoppedAtNullable1607431743768';
async up(queryRunner: QueryRunner): Promise<void> {
const tablePrefix = config.get('database.tablePrefix');
await queryRunner.query('ALTER TABLE `' + tablePrefix + 'execution_entity` ALTER COLUMN `stoppedAt` DROP NOT NULL', undefined);
let tablePrefix = config.get('database.tablePrefix');
const schema = config.get('database.postgresdb.schema');
if (schema) {
tablePrefix = schema + '.' + tablePrefix;
}
await queryRunner.query('ALTER TABLE ' + tablePrefix + 'execution_entity ALTER COLUMN "stoppedAt" DROP NOT NULL', undefined);
}
async down(queryRunner: QueryRunner): Promise<void> {