mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-20 18:49:27 -08:00
Fixed reloading database schema for sqlite by reconnecting and fixing postgres migration
This commit is contained in:
parent
e18ecbed6a
commit
81780cfe19
|
@ -164,7 +164,7 @@ export async function init(): Promise<IDatabaseCollections> {
|
||||||
CreateIndexStoppedAt1594825041918,
|
CreateIndexStoppedAt1594825041918,
|
||||||
MakeStoppedAtNullable1607431743769,
|
MakeStoppedAtNullable1607431743769,
|
||||||
],
|
],
|
||||||
migrationsRun: true,
|
migrationsRun: false, // migrations for sqlite will be ran manually for now; see below
|
||||||
migrationsTableName: `${entityPrefix}migrations`,
|
migrationsTableName: `${entityPrefix}migrations`,
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
@ -181,27 +181,26 @@ export async function init(): Promise<IDatabaseCollections> {
|
||||||
|
|
||||||
let connection = await createConnection(connectionOptions);
|
let connection = await createConnection(connectionOptions);
|
||||||
|
|
||||||
let mustReconnect = false;
|
|
||||||
|
|
||||||
if (dbType === 'sqlite') {
|
if (dbType === 'sqlite') {
|
||||||
// This specific migration changes database metadata.
|
// This specific migration changes database metadata.
|
||||||
// A field is now nullable. We need to reconnect so that
|
// A field is now nullable. We need to reconnect so that
|
||||||
// n8n knows it has changed. Happens only on sqlite.
|
// 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) {
|
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);
|
collections.Credentials = getRepository(entities.CredentialsEntity);
|
||||||
|
|
|
@ -3,10 +3,15 @@ import {MigrationInterface, QueryRunner} from "typeorm";
|
||||||
import * as config from '../../../../config';
|
import * as config from '../../../../config';
|
||||||
|
|
||||||
export class MakeStoppedAtNullable1607431743768 implements MigrationInterface {
|
export class MakeStoppedAtNullable1607431743768 implements MigrationInterface {
|
||||||
|
name = 'MakeStoppedAtNullable1607431743768';
|
||||||
|
|
||||||
async up(queryRunner: QueryRunner): Promise<void> {
|
async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
const tablePrefix = config.get('database.tablePrefix');
|
let tablePrefix = config.get('database.tablePrefix');
|
||||||
await queryRunner.query('ALTER TABLE `' + tablePrefix + 'execution_entity` ALTER COLUMN `stoppedAt` DROP NOT NULL', undefined);
|
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> {
|
async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
|
Loading…
Reference in a new issue