diff --git a/packages/cli/databases/sqlite/database.sqlite b/packages/cli/databases/sqlite/database.sqlite new file mode 100644 index 0000000000..85193ea151 Binary files /dev/null and b/packages/cli/databases/sqlite/database.sqlite differ diff --git a/packages/cli/ormconfig.ts b/packages/cli/ormconfig.ts index e35fb7e17f..ce88a16eae 100644 --- a/packages/cli/ormconfig.ts +++ b/packages/cli/ormconfig.ts @@ -6,7 +6,7 @@ module.exports = [ "type": "sqlite", "logging": true, "entities": Object.values(SQLite), - "database": "C:\Users\Ronald\.n8n\database.sqlite", + "database": "./packages/cli/database.sqlite", "migrations": [ "./src/databases/sqlite/migrations/*.ts" ], @@ -24,8 +24,10 @@ module.exports = [ "type": "mongodb", "logging": false, "entities": Object.values(MongoDb), + "url": "mongodb://root:example@localhost:27017/n8n", + "authSource": 'admin', "migrations": [ - "./src/databases/mongodb/Migrations/**/*.ts" + "./src/databases/mongodb/migrations/*.ts" ], "subscribers": [ "src/subscriber/**/*.ts" @@ -62,17 +64,44 @@ module.exports = [ { "name": "mysql", "type": "mysql", + "database": "n8n", + "username": "root", + "password": "password", + "host": "localhost", + "port": "3308", "logging": false, "entities": Object.values(MySQLDb), "migrations": [ - "./src/databases/mysqldb/Migrations/**/*.ts" + "./src/databases/mysqldb/migrations/*.ts" ], "subscribers": [ "src/subscriber/**/*.ts" ], "cli": { "entitiesDir": "./src/databases/mysqldb", - "migrationsDir": "./src/databases/mysqldb/Migrations", + "migrationsDir": "./src/databases/mysqldb/migrations", + "subscribersDir": "./src/databases/mysqldb/Subscribers" + } + }, + { + "name": "mysql", + "type": "mysql", + "database": "n8n", + "username": "root", + "password": "password", + "host": "localhost", + "port": "3308", + "logging": false, + "entities": Object.values(MySQLDb), + "migrations": [ + "./src/databases/mysqldb/migrations/*.ts" + ], + "subscribers": [ + "src/subscriber/**/*.ts" + ], + "cli": { + "entitiesDir": "./src/databases/mysqldb", + "migrationsDir": "./src/databases/mysqldb/migrations", "subscribersDir": "./src/databases/mysqldb/Subscribers" } }, diff --git a/packages/cli/package.json b/packages/cli/package.json index e0f9818a78..31e47faed0 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -103,7 +103,7 @@ "open": "^7.0.0", "pg": "^7.11.0", "request-promise-native": "^1.0.7", - "sqlite3": "^4.0.6", + "sqlite3": "^4.2.0", "sse-channel": "^3.1.1", "ts-node": "^8.9.1", "typeorm": "^0.2.24" diff --git a/packages/cli/packages/cli/database.sqlite b/packages/cli/packages/cli/database.sqlite new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/cli/src/Db.ts b/packages/cli/src/Db.ts index 25c289d7ac..eb395707cb 100644 --- a/packages/cli/src/Db.ts +++ b/packages/cli/src/Db.ts @@ -28,11 +28,21 @@ export let collections: IDatabaseCollections = { Workflow: null, }; -import InitialMigration1587669153312 from './databases/postgresdb/migrations/1587669153312-InitialMigration' +import { + InitialMigration1587669153312 +} from './databases/postgresdb/migrations'; + +import { + InitialMigration1588086339467 +} from './databases/mysqldb/migrations'; + +import { + InitialMigration1588102412422 +} from './databases/sqlite/migrations'; import * as path from 'path'; -export async function init(synchronize?: boolean): Promise { +export async function init(): Promise { const dbType = await GenericHelpers.getConfigValue('database.type') as DatabaseType; const n8nFolder = UserSettings.getUserN8nFolderPath(); @@ -49,7 +59,6 @@ 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/*.js'], }; break; @@ -65,7 +74,8 @@ 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: [InitialMigration1587669153312] + migrations: [InitialMigration1587669153312], + migrationsRun: true }; break; @@ -81,7 +91,8 @@ 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/*.js'] + migrations: [InitialMigration1588086339467], + migrationsRun: true }; break; @@ -92,7 +103,8 @@ 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/*.js'], + migrations: [InitialMigration1588102412422], + migrationsRun: true }; break; @@ -102,50 +114,29 @@ export async function init(synchronize?: boolean): Promise Object.assign(connectionOptions, { entities: Object.values(entities), - synchronize: false,//synchronize === true || process.env['NODE_ENV'] !== 'production', - logging: true, - migrationsRun: true + synchronize: false, }); - try{ + try { connection = await createConnection(connectionOptions); - let migrations = await connection.runMigrations({ + await connection.runMigrations({ transaction: 'none' }); - console.log(migrations); - - }catch(e){ - console.log(`Error: ${e}`); - return e; - } - - // 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); + + if(connection.isConnected){ + collections.Credentials = getRepository(entities.CredentialsEntity); + collections.Execution = getRepository(entities.ExecutionEntity); + collections.Workflow = getRepository(entities.WorkflowEntity); + } else { + init(); } + + } catch (e){ + console.log(e); } + return collections; }; diff --git a/packages/cli/src/databases/mysqldb/Migrations/1587563497207-InitialMigration.ts b/packages/cli/src/databases/mysqldb/Migrations/1587563497207-InitialMigration.ts deleted file mode 100644 index 1a083bd65a..0000000000 --- a/packages/cli/src/databases/mysqldb/Migrations/1587563497207-InitialMigration.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {MigrationInterface, QueryRunner} from "typeorm"; - -export class InitialMigration1587563497207 implements MigrationInterface { - - public async up(queryRunner: QueryRunner): Promise { - } - - public async down(queryRunner: QueryRunner): Promise { - } - -} diff --git a/packages/cli/src/databases/mysqldb/migrations/1588086339467-InitialMigration.ts b/packages/cli/src/databases/mysqldb/migrations/1588086339467-InitialMigration.ts new file mode 100644 index 0000000000..8cadd12d6d --- /dev/null +++ b/packages/cli/src/databases/mysqldb/migrations/1588086339467-InitialMigration.ts @@ -0,0 +1,20 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class InitialMigration1588086339467 implements MigrationInterface { + name = 'InitialMigration1588086339467' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query("CREATE TABLE `credentials_entity` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL, `data` text NOT NULL, `type` varchar(32) NOT NULL, `nodesAccess` json NOT NULL, `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, INDEX `IDX_07fde106c0b471d8cc80a64fc8` (`type`), PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined); + await queryRunner.query("CREATE TABLE `execution_entity` (`id` int NOT NULL AUTO_INCREMENT, `data` text NOT NULL, `finished` tinyint NOT NULL, `mode` varchar(255) NOT NULL, `retryOf` varchar(255) NULL, `retrySuccessId` varchar(255) NULL, `startedAt` datetime NOT NULL, `stoppedAt` datetime NOT NULL, `workflowData` json NOT NULL, `workflowId` varchar(255) NULL, INDEX `IDX_c4d999a5e90784e8caccf5589d` (`workflowId`), PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined); + await queryRunner.query("CREATE TABLE `workflow_entity` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL, `active` tinyint NOT NULL, `nodes` json NOT NULL, `connections` json NOT NULL, `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, `settings` json NULL, `staticData` json NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query("DROP TABLE `workflow_entity`", undefined); + await queryRunner.query("DROP INDEX `IDX_c4d999a5e90784e8caccf5589d` ON `execution_entity`", undefined); + await queryRunner.query("DROP TABLE `execution_entity`", undefined); + await queryRunner.query("DROP INDEX `IDX_07fde106c0b471d8cc80a64fc8` ON `credentials_entity`", undefined); + await queryRunner.query("DROP TABLE `credentials_entity`", undefined); + } + +} diff --git a/packages/cli/src/databases/mysqldb/migrations/index.ts b/packages/cli/src/databases/mysqldb/migrations/index.ts new file mode 100644 index 0000000000..22aa052ac1 --- /dev/null +++ b/packages/cli/src/databases/mysqldb/migrations/index.ts @@ -0,0 +1 @@ +export * from './1588086339467-InitialMigration'; \ No newline at end of file diff --git a/packages/cli/src/databases/postgresdb/CredentialsEntity.ts b/packages/cli/src/databases/postgresdb/CredentialsEntity.ts index e3ef375254..cddaf7559f 100644 --- a/packages/cli/src/databases/postgresdb/CredentialsEntity.ts +++ b/packages/cli/src/databases/postgresdb/CredentialsEntity.ts @@ -41,4 +41,5 @@ export class CredentialsEntity implements ICredentialsDb { @Column('timestamp') updatedAt: Date; + } diff --git a/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts index 045eb408d4..520be3fcd0 100644 --- a/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts +++ b/packages/cli/src/databases/postgresdb/migrations/1587669153312-InitialMigration.ts @@ -1,14 +1,14 @@ import {MigrationInterface, QueryRunner} from "typeorm"; -export default class InitialMigration1587669153312 implements MigrationInterface { +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 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 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); + 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 { diff --git a/packages/cli/src/databases/postgresdb/migrations/index.ts b/packages/cli/src/databases/postgresdb/migrations/index.ts new file mode 100644 index 0000000000..5bb6551492 --- /dev/null +++ b/packages/cli/src/databases/postgresdb/migrations/index.ts @@ -0,0 +1 @@ +export * from './1587669153312-InitialMigration'; diff --git a/packages/cli/src/databases/sqlite/Migrations/1587563481290-InitialMigration.ts b/packages/cli/src/databases/sqlite/Migrations/1587563481290-InitialMigration.ts deleted file mode 100644 index 20d30f068d..0000000000 --- a/packages/cli/src/databases/sqlite/Migrations/1587563481290-InitialMigration.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {MigrationInterface, QueryRunner} from "typeorm"; - -export class InitialMigration1587563481290 implements MigrationInterface { - - public async up(queryRunner: QueryRunner): Promise { - } - - public async down(queryRunner: QueryRunner): Promise { - } - -} diff --git a/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts b/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts deleted file mode 100644 index 98dd98a5be..0000000000 --- a/packages/cli/src/databases/sqlite/migrations/1587628010571-InitialMigration.ts +++ /dev/null @@ -1,23 +0,0 @@ -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`) */} diff --git a/packages/cli/src/databases/sqlite/migrations/1588102412422-InitialMigration.ts b/packages/cli/src/databases/sqlite/migrations/1588102412422-InitialMigration.ts new file mode 100644 index 0000000000..ccad7e6dd8 --- /dev/null +++ b/packages/cli/src/databases/sqlite/migrations/1588102412422-InitialMigration.ts @@ -0,0 +1,22 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class InitialMigration1588102412422 implements MigrationInterface { + name = 'InitialMigration1588102412422' + + public async up(queryRunner: QueryRunner): Promise { + 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)`, undefined); + await queryRunner.query(`CREATE INDEX "IDX_07fde106c0b471d8cc80a64fc8" ON "credentials_entity" ("type") `, undefined); + await queryRunner.query(`CREATE TABLE "execution_entity" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "data" text NOT NULL, "finished" boolean NOT NULL, "mode" varchar NOT NULL, "retryOf" varchar, "retrySuccessId" varchar, "startedAt" datetime NOT NULL, "stoppedAt" datetime NOT NULL, "workflowData" text NOT NULL, "workflowId" varchar)`, undefined); + await queryRunner.query(`CREATE INDEX "IDX_c4d999a5e90784e8caccf5589d" ON "execution_entity" ("workflowId") `, undefined); + await queryRunner.query(`CREATE TABLE "workflow_entity" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(128) NOT NULL, "active" boolean NOT NULL, "nodes" text NOT NULL, "connections" text NOT NULL, "createdAt" datetime NOT NULL, "updatedAt" datetime NOT NULL, "settings" text, "staticData" text)`, 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/migrations/index.ts b/packages/cli/src/databases/sqlite/migrations/index.ts new file mode 100644 index 0000000000..8d9a0a0b16 --- /dev/null +++ b/packages/cli/src/databases/sqlite/migrations/index.ts @@ -0,0 +1 @@ +export * from './1588102412422-InitialMigration'; \ No newline at end of file