From adb4a5ac8e4246b25d2bb3855815812edde2ebed Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Fri, 8 Feb 2019 23:02:25 +0000
Subject: [PATCH] Better logging

---
 src/bot.ts       |  6 +++---
 src/discordas.ts | 16 +++++++++++++---
 src/store.ts     |  4 ++++
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/bot.ts b/src/bot.ts
index eee7eb5..176d81c 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -71,7 +71,7 @@ export class DiscordBot {
     private discordMsgProcessor: DiscordMessageProcessor;
     private mxEventProcessor: MatrixEventProcessor;
     private presenceHandler: PresenceHandler;
-    private userSync: UserSyncroniser;
+    private userSync!: UserSyncroniser;
     private channelSync: ChannelSyncroniser;
     private roomHandler: MatrixRoomHandler;
     private provisioner: Provisioner;
@@ -100,8 +100,6 @@ export class DiscordBot {
             new MatrixEventProcessorOpts(config, bridge, this),
         );
         this.channelSync = new ChannelSyncroniser(bridge, config, this, store.roomStore);
-        this.userSync = new UserSyncroniser(bridge, config, this);
-
         // init vars
         this.sentMessages = [];
         this.discordMessageQueue = {};
@@ -140,6 +138,8 @@ export class DiscordBot {
 
     public async init(): Promise<void> {
         await this.clientFactory.init();
+        // This immediately pokes UserStore, so it must be created after the bridge has started.
+        this.userSync = new UserSyncroniser(this.bridge, this.config, this);
     }
 
     public async run(): Promise<void> {
diff --git a/src/discordas.ts b/src/discordas.ts
index 9115b3d..1a5c67d 100644
--- a/src/discordas.ts
+++ b/src/discordas.ts
@@ -141,7 +141,19 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) {
         registration,
         userStore: config.database.userStorePath,
     });
-    // Warn and deprecate old config options.
+
+    if (config.database.roomStorePath) {
+        log.warn("[DEPRECATED] The room store is now part of the SQL database."
+               + "The config option roomStorePath no longer has any use.");
+    }
+
+    try {
+        await store.init(undefined, bridge.getRoomStore());
+    } catch (ex) {
+        log.error("Failed to init database. Exiting.", ex);
+        process.exit(1);
+    }
+
     const discordbot = new DiscordBot(botUserId, config, bridge, store);
     const roomhandler = discordbot.RoomHandler;
 
@@ -162,8 +174,6 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) {
     try {
         await bridge.run(port, config);
         log.info(`Started listening on port ${port}`);
-        await store.init(undefined, bridge.getRoomStore());
-        log.info("Initing bot");
         await discordbot.init();
         await discordbot.run();
         log.info("Discordbot started successfully");
diff --git a/src/store.ts b/src/store.ts
index f17bf14..0b69ee8 100644
--- a/src/store.ts
+++ b/src/store.ts
@@ -92,11 +92,15 @@ export class DiscordStore {
         await this.open_database();
         let version = await this.getSchemaVersion();
         const targetSchema = overrideSchema || CURRENT_SCHEMA;
+        log.info(`Database schema version is ${version}, latest version is ${targetSchema}`);
         while (version < targetSchema) {
             version++;
             const schemaClass = require(`./db/schema/v${version}.js`).Schema;
             let schema: IDbSchema;
             if (version === SCHEMA_ROOM_STORE_REQUIRED) { // 8 requires access to the roomstore.
+                if (!roomStore) {
+                    throw Error("Schema 8 requires the room store, but it was not provided");
+                }
                 schema = (new schemaClass(roomStore) as IDbSchema);
             } else {
                 schema = (new schemaClass() as IDbSchema);
-- 
GitLab