mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 12:44:07 -08:00
init migration test files, ormconfig
This commit is contained in:
parent
07c69f5ade
commit
daeafe68f3
|
@ -115,7 +115,11 @@ export class Start extends Command {
|
|||
await credentialTypes.init(loadNodesAndCredentials.credentialTypes);
|
||||
|
||||
// Wait till the database is ready
|
||||
await startDbInitPromise;
|
||||
let dbReady = await startDbInitPromise;
|
||||
|
||||
if(!dbReady){
|
||||
throw new Error("DB did not initialize.");
|
||||
}
|
||||
|
||||
if (flags.tunnel === true) {
|
||||
this.log('\nWaiting for tunnel ...');
|
||||
|
|
|
@ -2,6 +2,25 @@ import {MongoDb, SQLite, MySQLDb, PostgresDb} from './src/databases/index';
|
|||
|
||||
module.exports = [
|
||||
{
|
||||
"name": "sqlite",
|
||||
"type": "sqlite",
|
||||
"logging": true,
|
||||
"entities": Object.values(SQLite),
|
||||
"database": "C:\Users\Ronald\.n8n\database.sqlite",
|
||||
"migrations": [
|
||||
"./src/databases/sqlite/migrations/*.ts"
|
||||
],
|
||||
"subscribers": [
|
||||
"./src/databases/sqlite/subscribers/*.ts"
|
||||
],
|
||||
"cli": {
|
||||
"entitiesDir": "./src/databases/sqlite",
|
||||
"migrationsDir": "./src/databases/sqlite/migrations",
|
||||
"subscribersDir": "./src/databases/sqlite/subscribers"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "mongodb",
|
||||
"type": "mongodb",
|
||||
"logging": false,
|
||||
"entities": Object.values(MongoDb),
|
||||
|
@ -18,38 +37,30 @@ module.exports = [
|
|||
}
|
||||
},
|
||||
{
|
||||
"name": "postgres",
|
||||
"type": "postgres",
|
||||
"logging": false,
|
||||
"host": "localhost",
|
||||
"username": "postgres",
|
||||
"password": "docker",
|
||||
"port": 5432,
|
||||
"database": "postgres",
|
||||
"schema": "public",
|
||||
"entities": Object.values(PostgresDb),
|
||||
"migrations": [
|
||||
"./src/databases/postgresdb/Migrations/**/*.ts"
|
||||
"./src/databases/postgresdb/migrations/*.ts"
|
||||
],
|
||||
"subscribers": [
|
||||
"src/subscriber/**/*.ts"
|
||||
],
|
||||
"cli": {
|
||||
"entitiesDir": "./src/databases/postgresdb",
|
||||
"migrationsDir": "./src/databases/postgresdb/Migrations",
|
||||
"subscribersDir": "./src/databases/postgresdb/Subscribers"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "sqlite",
|
||||
"logging": false,
|
||||
"entities": Object.values(SQLite),
|
||||
"migrations": [
|
||||
"./src/databases/sqlite/Migrations/**/*.ts"
|
||||
],
|
||||
"subscribers": [
|
||||
"src/subscriber/**/*.ts"
|
||||
],
|
||||
"cli": {
|
||||
"entitiesDir": "./src/databases/sqlite",
|
||||
"migrationsDir": "./src/databases/sqlite/Migrations",
|
||||
"subscribersDir": "./src/databases/sqlite/Subscribers"
|
||||
"migrationsDir": "./src/databases/postgresdb/migrations",
|
||||
"subscribersDir": "./src/databases/postgresdb/subscribers"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "mysql",
|
||||
"type": "mysql",
|
||||
"logging": false,
|
||||
"entities": Object.values(MySQLDb),
|
||||
|
|
|
@ -105,6 +105,7 @@
|
|||
"request-promise-native": "^1.0.7",
|
||||
"sqlite3": "^4.0.6",
|
||||
"sse-channel": "^3.1.1",
|
||||
"ts-node": "^8.9.1",
|
||||
"typeorm": "^0.2.24"
|
||||
},
|
||||
"jest": {
|
||||
|
|
|
@ -12,6 +12,7 @@ import {
|
|||
ConnectionOptions,
|
||||
createConnection,
|
||||
getRepository,
|
||||
Connection,
|
||||
} from 'typeorm';
|
||||
|
||||
import {
|
||||
|
@ -29,12 +30,13 @@ export let collections: IDatabaseCollections = {
|
|||
|
||||
import * as path from 'path';
|
||||
|
||||
export async function init(synchronize?: boolean): Promise<IDatabaseCollections> {
|
||||
export async function init(synchronize?: boolean): Promise<boolean> {
|
||||
const dbType = await GenericHelpers.getConfigValue('database.type') as DatabaseType;
|
||||
const n8nFolder = UserSettings.getUserN8nFolderPath();
|
||||
|
||||
let entities;
|
||||
let connectionOptions: ConnectionOptions;
|
||||
let connection;
|
||||
|
||||
let dbNotExistError: string | undefined;
|
||||
switch (dbType) {
|
||||
|
@ -45,8 +47,7 @@ export async function init(synchronize?: boolean): Promise<IDatabaseCollections>
|
|||
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
||||
url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string,
|
||||
useNewUrlParser: true,
|
||||
migrations: ['./databases/mongodb/Migrations/*.ts'],
|
||||
migrationsRun: true
|
||||
migrations: ['./databases/mongodb/migrations/*.js'],
|
||||
};
|
||||
break;
|
||||
|
||||
|
@ -62,8 +63,7 @@ export async function init(synchronize?: boolean): Promise<IDatabaseCollections>
|
|||
port: await GenericHelpers.getConfigValue('database.postgresdb.port') as number,
|
||||
username: await GenericHelpers.getConfigValue('database.postgresdb.user') as string,
|
||||
schema: await GenericHelpers.getConfigValue('database.postgresdb.schema') as string,
|
||||
migrations: ['./databases/postgresdb/Migrations/*.ts'],
|
||||
migrationsRun: true
|
||||
migrations: ['./databases/postgresdb/migrations/*.js']
|
||||
};
|
||||
break;
|
||||
|
||||
|
@ -79,8 +79,7 @@ export async function init(synchronize?: boolean): Promise<IDatabaseCollections>
|
|||
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,
|
||||
migrations: ['./databases/mysqldb/Migrations/*.ts'],
|
||||
migrationsRun: true
|
||||
migrations: ['./databases/mysqldb/migrations/*.js']
|
||||
};
|
||||
break;
|
||||
|
||||
|
@ -91,8 +90,7 @@ export async function init(synchronize?: boolean): Promise<IDatabaseCollections>
|
|||
type: 'sqlite',
|
||||
database: path.join(n8nFolder, 'database.sqlite'),
|
||||
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
||||
migrations: ['./databases/sqlite/Migrations/*.ts'],
|
||||
migrationsRun: true
|
||||
migrations: ['./databases/sqlite/migrations/*.js'],
|
||||
};
|
||||
break;
|
||||
|
||||
|
@ -103,37 +101,25 @@ export async function init(synchronize?: boolean): Promise<IDatabaseCollections>
|
|||
Object.assign(connectionOptions, {
|
||||
entities: Object.values(entities),
|
||||
synchronize: false,//synchronize === true || process.env['NODE_ENV'] !== 'production',
|
||||
logging: false
|
||||
logging: true,
|
||||
migrationsRun: true
|
||||
});
|
||||
|
||||
const connection = await createConnection(connectionOptions);
|
||||
|
||||
// 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.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;
|
||||
for(let i = 0; i < 1000; i++){
|
||||
console.log(connectionOptions);
|
||||
}
|
||||
|
||||
return collections;
|
||||
}
|
||||
try{
|
||||
connection = await createConnection(connectionOptions);
|
||||
|
||||
await connection.runMigrations({
|
||||
transaction: "none"
|
||||
});
|
||||
}catch(e){
|
||||
throw new Error("Couldn't connect to db / migrate stuff.")
|
||||
}
|
||||
|
||||
|
||||
|
||||
return connection.isConnected;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
import {MigrationInterface, QueryRunner} from "typeorm";
|
||||
|
||||
export class InitialMigration1587669153312 implements MigrationInterface {
|
||||
name = 'InitialMigration1587669153312'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE credentials_entity (id SERIAL NOT NULL, name character varying(128) NOT NULL, data text NOT NULL, type character varying(32) NOT NULL, nodesAccess json NOT NULL, createdAt TIMESTAMP NOT NULL, updatedAt TIMESTAMP NOT NULL, CONSTRAINT PK_814c3d3c36e8a27fa8edb761b0e PRIMARY KEY (id))`, undefined);
|
||||
await queryRunner.query(`CREATE INDEX IDX_07fde106c0b471d8cc80a64fc8 ON credentials_entity (type) `, undefined);
|
||||
await queryRunner.query(`CREATE TABLE execution_entity (id SERIAL NOT NULL, data text NOT NULL, finished boolean NOT NULL, mode character varying NOT NULL, retryOf character varying, retrySuccessId character varying, startedAt TIMESTAMP NOT NULL, stoppedAt TIMESTAMP NOT NULL, workflowData json NOT NULL, workflowId character varying, CONSTRAINT PK_e3e63bbf986767844bbe1166d4e PRIMARY KEY (id))`, undefined);
|
||||
await queryRunner.query(`CREATE INDEX IDX_c4d999a5e90784e8caccf5589d ON execution_entity (workflowId) `, undefined);
|
||||
await queryRunner.query(`CREATE TABLE workflow_entity (id SERIAL NOT NULL, name character varying(128) NOT NULL, active boolean NOT NULL, nodes json NOT NULL, connections json NOT NULL, createdAt TIMESTAMP NOT NULL, updatedAt TIMESTAMP NOT NULL, settings json, staticData json, CONSTRAINT PK_eded7d72664448da7745d551207 PRIMARY KEY (id))`, undefined);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE workflow_entity`, undefined);
|
||||
await queryRunner.query(`DROP INDEX IDX_c4d999a5e90784e8caccf5589d`, undefined);
|
||||
await queryRunner.query(`DROP TABLE execution_entity`, undefined);
|
||||
await queryRunner.query(`DROP INDEX IDX_07fde106c0b471d8cc80a64fc8`, undefined);
|
||||
await queryRunner.query(`DROP TABLE credentials_entity`, undefined);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
export * from './CredentialsEntity';
|
||||
export * from './ExecutionEntity';
|
||||
export * from './WorkflowEntity';
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import {MigrationInterface, QueryRunner} from "typeorm";
|
||||
|
||||
export class InitialMigration1587628010571 implements MigrationInterface {
|
||||
name = 'InitialMigration1587628010571'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE test_table (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
second_name TEXT NOT NULL
|
||||
)`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE test_table`);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* await queryRunner.query(`ALTER TABLE credentials_entity RENAME TO _credentials_entity_old`, undefined);
|
||||
await queryRunner.query(`CREATE TABLE credentials_entity ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(128) NOT NULL, "data" text NOT NULL, "type" varchar(32) NOT NULL, "nodesAccess" text NOT NULL, "createdAt" datetime NOT NULL, "updatedAt" datetime NOT NULL, "testCol" text)`, undefined);
|
||||
await queryRunner.query(`INSERT INTO credentials_entity (id, name, data, type, nodesAccess, createdAt, updatedAt) SELECT id, name, data, type, nodesAccess, createdAt, updatedAt FROM _credentials_entity_old`) */}
|
Loading…
Reference in a new issue