2019-06-23 03:35:23 -07:00
|
|
|
import {
|
2020-05-04 08:29:39 -07:00
|
|
|
DatabaseType,
|
2019-08-03 05:06:11 -07:00
|
|
|
GenericHelpers,
|
2019-06-23 03:35:23 -07:00
|
|
|
IDatabaseCollections,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
import {
|
|
|
|
UserSettings,
|
2019-09-19 05:14:37 -07:00
|
|
|
} from 'n8n-core';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ConnectionOptions,
|
|
|
|
createConnection,
|
|
|
|
getRepository,
|
2019-09-19 05:14:37 -07:00
|
|
|
} from 'typeorm';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2020-06-25 02:39:04 -07:00
|
|
|
import { TlsOptions } from 'tls';
|
|
|
|
|
2020-05-13 00:22:14 -07:00
|
|
|
import * as config from '../config';
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
MongoDb,
|
2020-05-04 08:29:39 -07:00
|
|
|
MySQLDb,
|
2019-07-22 11:29:06 -07:00
|
|
|
PostgresDb,
|
2019-06-23 03:35:23 -07:00
|
|
|
SQLite,
|
2019-06-24 01:30:46 -07:00
|
|
|
} from './databases';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
export let collections: IDatabaseCollections = {
|
|
|
|
Credentials: null,
|
|
|
|
Execution: null,
|
|
|
|
Workflow: null,
|
2020-05-27 16:32:49 -07:00
|
|
|
Webhook: null,
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2020-04-29 02:34:12 -07:00
|
|
|
import {
|
2020-05-27 16:32:49 -07:00
|
|
|
InitialMigration1587669153312,
|
|
|
|
WebhookModel1589476000887,
|
2020-07-17 08:08:40 -07:00
|
|
|
CreateIndexStoppedAt1594828256133,
|
2020-04-29 02:34:12 -07:00
|
|
|
} from './databases/postgresdb/migrations';
|
|
|
|
|
2020-05-13 00:22:14 -07:00
|
|
|
import {
|
2020-06-20 18:59:06 -07:00
|
|
|
InitialMigration1587563438936,
|
|
|
|
WebhookModel1592679094242,
|
2020-07-17 08:08:40 -07:00
|
|
|
CreateIndexStoppedAt1594910478695,
|
2020-05-13 00:22:14 -07:00
|
|
|
} from './databases/mongodb/migrations';
|
|
|
|
|
2020-04-29 02:34:12 -07:00
|
|
|
import {
|
2020-06-17 20:42:04 -07:00
|
|
|
InitialMigration1588157391238,
|
|
|
|
WebhookModel1592447867632,
|
2020-07-17 08:08:40 -07:00
|
|
|
CreateIndexStoppedAt1594902918301,
|
2020-04-29 02:34:12 -07:00
|
|
|
} from './databases/mysqldb/migrations';
|
|
|
|
|
|
|
|
import {
|
2020-06-17 20:42:04 -07:00
|
|
|
InitialMigration1588102412422,
|
|
|
|
WebhookModel1592445003908,
|
2020-07-17 08:08:40 -07:00
|
|
|
CreateIndexStoppedAt1594825041918,
|
2020-04-29 02:34:12 -07:00
|
|
|
} from './databases/sqlite/migrations';
|
2020-04-27 15:52:30 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2020-04-29 02:34:12 -07:00
|
|
|
export async function init(): Promise<IDatabaseCollections> {
|
2019-08-03 05:06:11 -07:00
|
|
|
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-05-13 00:22:14 -07:00
|
|
|
const entityPrefix = config.get('database.tablePrefix');
|
|
|
|
|
2020-02-10 08:09:06 -08:00
|
|
|
switch (dbType) {
|
|
|
|
case 'mongodb':
|
|
|
|
entities = MongoDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'mongodb',
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2020-02-10 08:09:06 -08:00
|
|
|
url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string,
|
|
|
|
useNewUrlParser: true,
|
2020-07-17 08:08:40 -07:00
|
|
|
migrations: [
|
|
|
|
InitialMigration1587563438936,
|
|
|
|
WebhookModel1592679094242,
|
|
|
|
CreateIndexStoppedAt1594910478695,
|
|
|
|
],
|
2020-05-13 00:22:14 -07:00
|
|
|
migrationsRun: true,
|
|
|
|
migrationsTableName: `${entityPrefix}migrations`,
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'postgresdb':
|
|
|
|
entities = PostgresDb;
|
2020-06-25 02:39:04 -07:00
|
|
|
|
|
|
|
const sslCa = await GenericHelpers.getConfigValue('database.postgresdb.ssl.ca') as string;
|
|
|
|
const sslCert = await GenericHelpers.getConfigValue('database.postgresdb.ssl.cert') as string;
|
|
|
|
const sslKey = await GenericHelpers.getConfigValue('database.postgresdb.ssl.key') as string;
|
|
|
|
const sslRejectUnauthorized = await GenericHelpers.getConfigValue('database.postgresdb.ssl.rejectUnauthorized') as boolean;
|
|
|
|
|
|
|
|
let ssl: TlsOptions | undefined = undefined;
|
|
|
|
if (sslCa !== '' || sslCert !== '' || sslKey !== '' || sslRejectUnauthorized !== true) {
|
|
|
|
ssl = {
|
|
|
|
ca: sslCa || undefined,
|
|
|
|
cert: sslCert || undefined,
|
|
|
|
key: sslKey || undefined,
|
|
|
|
rejectUnauthorized: sslRejectUnauthorized,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:09:06 -08:00
|
|
|
connectionOptions = {
|
|
|
|
type: 'postgres',
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2020-02-10 08:09:06 -08:00
|
|
|
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-05-13 00:31:31 -07:00
|
|
|
schema: config.get('database.postgresdb.schema'),
|
2020-07-17 08:08:40 -07:00
|
|
|
migrations: [
|
|
|
|
InitialMigration1587669153312,
|
|
|
|
WebhookModel1589476000887,
|
|
|
|
CreateIndexStoppedAt1594828256133,
|
|
|
|
],
|
2020-05-13 00:22:14 -07:00
|
|
|
migrationsRun: true,
|
|
|
|
migrationsTableName: `${entityPrefix}migrations`,
|
2020-06-25 02:39:04 -07:00
|
|
|
ssl,
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
2020-06-25 02:39:04 -07:00
|
|
|
|
2020-02-10 08:09:06 -08:00
|
|
|
break;
|
|
|
|
|
2020-04-14 10:54:11 -07:00
|
|
|
case 'mariadb':
|
2020-02-10 08:09:06 -08:00
|
|
|
case 'mysqldb':
|
|
|
|
entities = MySQLDb;
|
|
|
|
connectionOptions = {
|
2020-04-14 10:54:11 -07:00
|
|
|
type: dbType === 'mysqldb' ? 'mysql' : 'mariadb',
|
2020-02-10 08:09:06 -08:00
|
|
|
database: await GenericHelpers.getConfigValue('database.mysqldb.database') as string,
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2020-02-10 08:09:06 -08:00
|
|
|
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,
|
2020-03-21 10:32:26 -07:00
|
|
|
username: await GenericHelpers.getConfigValue('database.mysqldb.user') as string,
|
2020-07-17 08:08:40 -07:00
|
|
|
migrations: [
|
|
|
|
InitialMigration1588157391238,
|
|
|
|
WebhookModel1592447867632,
|
|
|
|
CreateIndexStoppedAt1594902918301,
|
|
|
|
],
|
2020-05-13 00:22:14 -07:00
|
|
|
migrationsRun: true,
|
|
|
|
migrationsTableName: `${entityPrefix}migrations`,
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'sqlite':
|
|
|
|
entities = SQLite;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'sqlite',
|
2020-04-29 04:57:21 -07:00
|
|
|
database: path.join(n8nFolder, 'database.sqlite'),
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2020-07-17 08:08:40 -07:00
|
|
|
migrations: [
|
|
|
|
InitialMigration1588102412422,
|
|
|
|
WebhookModel1592445003908,
|
|
|
|
CreateIndexStoppedAt1594825041918
|
|
|
|
],
|
2020-05-04 08:29:39 -07:00
|
|
|
migrationsRun: true,
|
2020-05-13 00:22:14 -07:00
|
|
|
migrationsTableName: `${entityPrefix}migrations`,
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
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-29 02:34:12 -07:00
|
|
|
synchronize: false,
|
2020-05-04 08:29:39 -07:00
|
|
|
logging: false,
|
2019-06-23 03:35:23 -07:00
|
|
|
});
|
|
|
|
|
2020-05-04 08:29:39 -07:00
|
|
|
const connection = await createConnection(connectionOptions);
|
2020-04-27 03:46:09 -07:00
|
|
|
|
2020-05-04 08:29:39 -07:00
|
|
|
await connection.runMigrations({
|
|
|
|
transaction: 'none',
|
|
|
|
});
|
2020-04-27 03:46:09 -07:00
|
|
|
|
2020-05-04 08:29:39 -07:00
|
|
|
collections.Credentials = getRepository(entities.CredentialsEntity);
|
|
|
|
collections.Execution = getRepository(entities.ExecutionEntity);
|
|
|
|
collections.Workflow = getRepository(entities.WorkflowEntity);
|
2020-05-27 16:32:49 -07:00
|
|
|
collections.Webhook = getRepository(entities.WebhookEntity);
|
2020-04-27 03:46:09 -07:00
|
|
|
|
2020-04-27 15:52:30 -07:00
|
|
|
return collections;
|
2020-05-04 08:29:39 -07:00
|
|
|
}
|