n8n/packages/cli/src/Db.ts

152 lines
4.5 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import {
GenericHelpers,
2019-06-23 03:35:23 -07:00
IDatabaseCollections,
DatabaseType,
} from './';
import {
UserSettings,
} from 'n8n-core';
2019-06-23 03:35:23 -07:00
import {
ConnectionOptions,
createConnection,
getRepository,
2020-04-27 03:46:09 -07:00
Connection,
} from 'typeorm';
2019-06-23 03:35:23 -07:00
import {
MongoDb,
PostgresDb,
2019-06-23 03:35:23 -07:00
SQLite,
MySQLDb,
} from './databases';
2019-06-23 03:35:23 -07:00
export let collections: IDatabaseCollections = {
Credentials: null,
Execution: null,
Workflow: null,
};
2020-04-27 15:52:30 -07:00
import InitialMigration1587669153312 from './databases/postgresdb/migrations/1587669153312-InitialMigration'
2019-06-23 03:35:23 -07:00
import * as path from 'path';
2020-04-27 15:52:30 -07:00
export async function init(synchronize?: boolean): Promise<IDatabaseCollections> {
const dbType = await GenericHelpers.getConfigValue('database.type') as DatabaseType;
2019-06-23 03:35:23 -07:00
const n8nFolder = UserSettings.getUserN8nFolderPath();
let entities;
let connectionOptions: ConnectionOptions;
2020-04-27 03:46:09 -07:00
let connection;
2019-06-23 03:35:23 -07:00
let dbNotExistError: string | undefined;
switch (dbType) {
case 'mongodb':
entities = MongoDb;
connectionOptions = {
type: 'mongodb',
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string,
useNewUrlParser: true,
2020-04-27 03:46:09 -07:00
migrations: ['./databases/mongodb/migrations/*.js'],
};
break;
case 'postgresdb':
dbNotExistError = 'does not exist';
entities = PostgresDb;
connectionOptions = {
type: 'postgres',
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
database: await GenericHelpers.getConfigValue('database.postgresdb.database') as string,
host: await GenericHelpers.getConfigValue('database.postgresdb.host') as string,
password: await GenericHelpers.getConfigValue('database.postgresdb.password') as string,
port: await GenericHelpers.getConfigValue('database.postgresdb.port') as number,
username: await GenericHelpers.getConfigValue('database.postgresdb.user') as string,
2020-03-17 22:04:53 -07:00
schema: await GenericHelpers.getConfigValue('database.postgresdb.schema') as string,
2020-04-27 15:52:30 -07:00
migrations: [InitialMigration1587669153312]
};
break;
2020-04-14 10:54:11 -07:00
case 'mariadb':
case 'mysqldb':
dbNotExistError = 'does not exist';
entities = MySQLDb;
connectionOptions = {
2020-04-14 10:54:11 -07:00
type: dbType === 'mysqldb' ? 'mysql' : 'mariadb',
database: await GenericHelpers.getConfigValue('database.mysqldb.database') as string,
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
host: await GenericHelpers.getConfigValue('database.mysqldb.host') as string,
password: await GenericHelpers.getConfigValue('database.mysqldb.password') as string,
port: await GenericHelpers.getConfigValue('database.mysqldb.port') as number,
username: await GenericHelpers.getConfigValue('database.mysqldb.user') as string,
2020-04-27 03:46:09 -07:00
migrations: ['./databases/mysqldb/migrations/*.js']
};
break;
case 'sqlite':
dbNotExistError = 'no such table:';
entities = SQLite;
connectionOptions = {
type: 'sqlite',
database: path.join(n8nFolder, 'database.sqlite'),
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
2020-04-27 03:46:09 -07:00
migrations: ['./databases/sqlite/migrations/*.js'],
};
break;
default:
throw new Error(`The database "${dbType}" is currently not supported!`);
2019-06-23 03:35:23 -07:00
}
Object.assign(connectionOptions, {
entities: Object.values(entities),
2020-04-22 07:03:50 -07:00
synchronize: false,//synchronize === true || process.env['NODE_ENV'] !== 'production',
2020-04-27 03:46:09 -07:00
logging: true,
migrationsRun: true
2019-06-23 03:35:23 -07:00
});
2020-04-27 03:46:09 -07:00
try{
connection = await createConnection(connectionOptions);
2020-04-27 15:52:30 -07:00
let migrations = await connection.runMigrations({
transaction: 'none'
2020-04-27 03:46:09 -07:00
});
2020-04-27 15:52:30 -07:00
console.log(migrations);
2020-04-27 03:46:09 -07:00
2020-04-27 15:52:30 -07:00
}catch(e){
console.log(`Error: ${e}`);
return e;
}
2020-04-27 03:46:09 -07:00
2020-04-27 15:52:30 -07:00
// TODO: Fix that properly
// @ts-ignore
collections.Credentials = getRepository(entities.CredentialsEntity);
// @ts-ignore
collections.Execution = getRepository(entities.ExecutionEntity);
// @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.Execution!.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);
}
}
return collections;
2020-04-27 03:46:09 -07:00
};