2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
|
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
|
|
/* eslint-disable no-case-declarations */
|
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
import { UserSettings } from 'n8n-core';
|
|
|
|
import { ConnectionOptions, createConnection, getRepository } from 'typeorm';
|
2020-06-25 02:39:04 -07:00
|
|
|
import { TlsOptions } from 'tls';
|
2021-08-29 11:58:11 -07:00
|
|
|
import * as path from 'path';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
import { DatabaseType, GenericHelpers, IDatabaseCollections } from '.';
|
2020-06-25 02:39:04 -07:00
|
|
|
|
2021-06-23 02:20:07 -07:00
|
|
|
import * as config from '../config';
|
2020-05-13 00:22:14 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line import/no-cycle
|
2021-05-29 11:31:21 -07:00
|
|
|
import { entities } from './databases/entities';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
import { postgresMigrations } from './databases/postgresdb/migrations';
|
|
|
|
import { mysqlMigrations } from './databases/mysqldb/migrations';
|
|
|
|
import { sqliteMigrations } from './databases/sqlite/migrations';
|
|
|
|
|
|
|
|
export const collections: IDatabaseCollections = {
|
2019-06-23 03:35:23 -07:00
|
|
|
Credentials: null,
|
|
|
|
Execution: null,
|
|
|
|
Workflow: null,
|
2020-05-27 16:32:49 -07:00
|
|
|
Webhook: null,
|
2021-05-29 11:31:21 -07:00
|
|
|
Tag: null,
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2020-04-29 02:34:12 -07:00
|
|
|
export async function init(): Promise<IDatabaseCollections> {
|
2021-08-29 11:58: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 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 'postgresdb':
|
2021-08-29 11:58:11 -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;
|
|
|
|
if (sslCa !== '' || sslCert !== '' || sslKey !== '' || !sslRejectUnauthorized) {
|
2020-06-25 02:39:04 -07:00
|
|
|
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,
|
2021-08-29 11:58:11 -07: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'),
|
2021-01-23 11:00:32 -08:00
|
|
|
migrations: postgresMigrations,
|
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':
|
|
|
|
connectionOptions = {
|
2020-04-14 10:54:11 -07:00
|
|
|
type: dbType === 'mysqldb' ? 'mysql' : 'mariadb',
|
2021-08-29 11:58:11 -07:00
|
|
|
database: (await GenericHelpers.getConfigValue('database.mysqldb.database')) as string,
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2021-08-29 11:58:11 -07: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,
|
|
|
|
username: (await GenericHelpers.getConfigValue('database.mysqldb.user')) as string,
|
2021-01-23 11:00:32 -08:00
|
|
|
migrations: mysqlMigrations,
|
2020-05-13 00:22:14 -07:00
|
|
|
migrationsRun: true,
|
|
|
|
migrationsTableName: `${entityPrefix}migrations`,
|
2021-07-23 12:40:53 -07:00
|
|
|
timezone: 'Z', // set UTC as default
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'sqlite':
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'sqlite',
|
2021-05-29 11:31:21 -07:00
|
|
|
database: path.join(n8nFolder, 'database.sqlite'),
|
2020-05-13 00:22:14 -07:00
|
|
|
entityPrefix,
|
2021-01-23 11:00:32 -08:00
|
|
|
migrations: sqliteMigrations,
|
2021-02-08 23:59:32 -08:00
|
|
|
migrationsRun: false, // migrations for sqlite will be ran manually for now; see below
|
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!`);
|
2021-08-29 11:58:11 -07:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
2021-02-08 23:59:32 -08:00
|
|
|
let connection = await createConnection(connectionOptions);
|
|
|
|
|
|
|
|
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.
|
|
|
|
let migrations = [];
|
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
migrations = await connection.query(
|
|
|
|
`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2021-02-08 23:59:32 -08:00
|
|
|
// Migration table does not exist yet - it will be created after migrations run for the first time.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you remove this call, remember to turn back on the
|
|
|
|
// setting to run migrations automatically above.
|
|
|
|
await connection.runMigrations({
|
|
|
|
transaction: 'none',
|
|
|
|
});
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
2021-02-08 23:59:32 -08:00
|
|
|
if (migrations.length === 0) {
|
|
|
|
await connection.close();
|
|
|
|
connection = await createConnection(connectionOptions);
|
|
|
|
}
|
|
|
|
}
|
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);
|
2021-05-29 11:31:21 -07:00
|
|
|
collections.Tag = getRepository(entities.TagEntity);
|
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
|
|
|
}
|