mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
🐛 Fix issue that in production mode database did not get initialized
This commit is contained in:
parent
a9d49f60b9
commit
c8f339ed8e
|
@ -31,13 +31,14 @@ export let collections: IDatabaseCollections = {
|
|||
|
||||
import * as path from 'path';
|
||||
|
||||
export async function init(): Promise<IDatabaseCollections> {
|
||||
export async function init(synchronize?: boolean): Promise<IDatabaseCollections> {
|
||||
const dbType = await GenericHelpers.getConfigValue('database.type') as DatabaseType;
|
||||
const n8nFolder = UserSettings.getUserN8nFolderPath();
|
||||
|
||||
let entities;
|
||||
let connectionOptions: ConnectionOptions;
|
||||
|
||||
let dbNotExistError: string | undefined;
|
||||
if (dbType === 'mongodb') {
|
||||
entities = MongoDb;
|
||||
connectionOptions = {
|
||||
|
@ -46,6 +47,7 @@ export async function init(): Promise<IDatabaseCollections> {
|
|||
useNewUrlParser: true,
|
||||
};
|
||||
} else if (dbType === 'postgresdb') {
|
||||
dbNotExistError = 'does not exist';
|
||||
entities = PostgresDb;
|
||||
connectionOptions = {
|
||||
type: 'postgres',
|
||||
|
@ -56,6 +58,7 @@ export async function init(): Promise<IDatabaseCollections> {
|
|||
username: await GenericHelpers.getConfigValue('database.postgresdb.user') as string,
|
||||
};
|
||||
} else if (dbType === 'sqlite') {
|
||||
dbNotExistError = 'no such table:';
|
||||
entities = SQLite;
|
||||
connectionOptions = {
|
||||
type: 'sqlite',
|
||||
|
@ -67,11 +70,11 @@ export async function init(): Promise<IDatabaseCollections> {
|
|||
|
||||
Object.assign(connectionOptions, {
|
||||
entities: Object.values(entities),
|
||||
synchronize: process.env['NODE_ENV'] !== 'production',
|
||||
synchronize: synchronize === true || process.env['NODE_ENV'] !== 'production',
|
||||
logging: false
|
||||
});
|
||||
|
||||
await createConnection(connectionOptions);
|
||||
const connection = await createConnection(connectionOptions);
|
||||
|
||||
// TODO: Fix that properly
|
||||
// @ts-ignore
|
||||
|
@ -81,5 +84,24 @@ export async function init(): Promise<IDatabaseCollections> {
|
|||
// @ts-ignore
|
||||
collections.Workflow = getRepository(entities.WorkflowEntity);
|
||||
|
||||
// Make sure that database did already get initialized
|
||||
try {
|
||||
// Try a simple query, if it fails it is normally a sign that
|
||||
// database did not get initialized
|
||||
await collections.Workflow!.findOne({ id: 1 });
|
||||
} catch (error) {
|
||||
// If query errors and the problem is that the database does not exist
|
||||
// run the init again with "synchronize: true"
|
||||
if (dbNotExistError !== undefined && error.message.includes(dbNotExistError)) {
|
||||
// Disconnect before we try to connect again
|
||||
if (connection.isConnected) {
|
||||
await connection.close();
|
||||
}
|
||||
|
||||
return init(true);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
return collections;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue