From daeafe68f37491131d492ced8891eee2bc83dcae Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Mon, 27 Apr 2020 12:46:09 +0200 Subject: [PATCH] init migration test files, ormconfig --- packages/cli/commands/start.ts | 6 +- packages/cli/ormconfig.ts | 49 ++++++++------ packages/cli/package.json | 1 + packages/cli/src/Db.ts | 64 ++++++++----------- .../1587669153312-InitialMigration.ts | 22 +++++++ packages/cli/src/databases/sqlite/index.ts | 1 + .../1587628010571-InitialMigration.ts | 23 +++++++ 7 files changed, 107 insertions(+), 59 deletions(-) create mode 100644 packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts create mode 100644 packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts diff --git a/packages/cli/commands/start.ts b/packages/cli/commands/start.ts index 4e2efaacfd..00789b9b43 100644 --- a/packages/cli/commands/start.ts +++ b/packages/cli/commands/start.ts @@ -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 ...'); diff --git a/packages/cli/ormconfig.ts b/packages/cli/ormconfig.ts index 34066ed8af..e35fb7e17f 100644 --- a/packages/cli/ormconfig.ts +++ b/packages/cli/ormconfig.ts @@ -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), diff --git a/packages/cli/package.json b/packages/cli/package.json index ee5933b25f..e0f9818a78 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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": { diff --git a/packages/cli/src/Db.ts b/packages/cli/src/Db.ts index 9d9998cfbc..29c94508aa 100644 --- a/packages/cli/src/Db.ts +++ b/packages/cli/src/Db.ts @@ -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 { +export async function init(synchronize?: boolean): Promise { 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 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 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 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 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 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; +}; diff --git a/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts new file mode 100644 index 0000000000..d5e897e923 --- /dev/null +++ b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts @@ -0,0 +1,22 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class InitialMigration1587669153312 implements MigrationInterface { + name = 'InitialMigration1587669153312' + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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); + } + +} diff --git a/packages/cli/src/databases/sqlite/index.ts b/packages/cli/src/databases/sqlite/index.ts index 164d67fd0c..2c7d6e25e9 100644 --- a/packages/cli/src/databases/sqlite/index.ts +++ b/packages/cli/src/databases/sqlite/index.ts @@ -1,3 +1,4 @@ export * from './CredentialsEntity'; export * from './ExecutionEntity'; export * from './WorkflowEntity'; + diff --git a/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts b/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts new file mode 100644 index 0000000000..98dd98a5be --- /dev/null +++ b/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts @@ -0,0 +1,23 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class InitialMigration1587628010571 implements MigrationInterface { + name = 'InitialMigration1587628010571' + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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`) */}