From 26ff8dfdff673691ec58f36379a9427d28c19060 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 25 May 2020 18:39:57 +0200 Subject: [PATCH] Ready to implement migration --- src/main.ts | 5 ++++ src/migration.ts | 51 ++++++++++++++++++++++++++++++++ src/migrations/MigrationsList.ts | 20 +++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/migration.ts create mode 100644 src/migrations/MigrationsList.ts diff --git a/src/main.ts b/src/main.ts index f692c04..b3bc6c9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import { ConfigurationHelper } from "./helpers/ConfigHelper"; import { DatabaseHelper } from "./helpers/DatabaseHelper"; import { startServer } from "./server"; import { showHelp } from "./help"; +import { startMigration } from "./migration"; /** * Main project script @@ -32,6 +33,10 @@ async function init() { await startServer(); break; + case "migration": + await startMigration(process.argv.slice(4)); + break; + } } diff --git a/src/migration.ts b/src/migration.ts new file mode 100644 index 0000000..b66075b --- /dev/null +++ b/src/migration.ts @@ -0,0 +1,51 @@ +/** + * Migration assistant + * + * @author Pierre Hubert + */ + +import { MigrationsList } from "./migrations/MigrationsList"; +import { createInterface } from "readline"; + + +function fatal(msg : string) { + console.error("Fatal error: " + msg); + process.exit(-1); +} + +/** + * Start a new migration + * + * @param args Arguments passed to the command line + */ +export async function startMigration(args: String[]) { + if(args.length < 1) + fatal("Missing migration ID!"); + + const migrationID = args[0]; + const migration = MigrationsList.find((m) => m.id == migrationID); + + if(!migration) + fatal("Could not find requested migration: "+migrationID+" !"); + + // Ask user confirmation before continuing + const rl = createInterface({ + input: process.stdin, + output: process.stdout + }); + + const response = await new Promise((res, _rej) => rl.question( + "Do you want to start this migration: " + migration.id + " ? (y/n) ", + (ans) => res(ans) + )); + + rl.close(); + + if(response !== "y") + fatal("Operation aborted."); + + // Do the migration + await migration.func(); + + process.exit(0); +} \ No newline at end of file diff --git a/src/migrations/MigrationsList.ts b/src/migrations/MigrationsList.ts new file mode 100644 index 0000000..f63602f --- /dev/null +++ b/src/migrations/MigrationsList.ts @@ -0,0 +1,20 @@ +/** + * List of currently available migrations + * + * @author Pierre Hubert + */ + +interface Migration { + id: string, + func: () => Promise +} + +export const MigrationsList : Migration[] = [ + + // Account image migration (files -> database, May 2020) + { + id: "account_image_2020", + func: undefined + } + +] \ No newline at end of file