diff --git a/src/db/schema/v8.ts b/src/db/schema/v8.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e681023df5f6d4f0df3d60a47c5c21b070aed0c --- /dev/null +++ b/src/db/schema/v8.ts @@ -0,0 +1,60 @@ +/* +Copyright 2018 matrix-appservice-discord + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import {IDbSchema} from "./dbschema"; +import {DiscordStore} from "../../store"; +import { Log } from "../../log"; + +const log = new Log("SchemaV8"); + +export class Schema implements IDbSchema { + public description = "create room store tables"; + public async run(store: DiscordStore): Promise<void> { + await store.create_table(` + CREATE TABLE remote_room_data ( + room_id TEXT NOT NULL, + discord_guild TEXT NOT NULL, + discord_channel TEXT NOT NULL, + discord_name TEXT DEFAULT NULL, + discord_topic TEXT DEFAULT NULL, + discord_type TEXT DEFAULT NULL, + discord_iconurl TEXT DEFAULT NULL, + discord_iconurl_mxc TEXT DEFAULT NULL, + update_name NUMERIC DEFAULT 0, + update_topic NUMERIC DEFAULT 0, + update_icon NUMERIC DEFAULT 0, + plumbed NUMERIC DEFAULT 0, + PRIMARY KEY(room_id) + );`, "remote_room_data"); + + await store.create_table(` + CREATE TABLE room_entries ( + id TEXT NOT NULL, + matrix_id TEXT, + remote_id TEXT, + PRIMARY KEY(id) + );`, "room_entries"); + } + + public async rollBack(store: DiscordStore): Promise<void> { + await store.db.Run( + `DROP TABLE IF EXISTS remote_room_data;`, + ); + await store.db.Run( + `DROP TABLE IF EXISTS room_entries;`, + ); + } +} diff --git a/src/store.ts b/src/store.ts index 62787dddc60ce7bb179dbad96148cf76e9be07e0..c18ef898927352b393bfe6c945ec6a8130687ca9 100644 --- a/src/store.ts +++ b/src/store.ts @@ -18,23 +18,22 @@ import * as fs from "fs"; import { IDbSchema } from "./db/schema/dbschema"; import { IDbData} from "./db/dbdatainterface"; import { SQLite3 } from "./db/sqlite3"; -export const CURRENT_SCHEMA = 7; +export const CURRENT_SCHEMA = 8; import { Log } from "./log"; import { DiscordBridgeConfigDatabase } from "./config"; import { Postgres } from "./db/postgres"; import { IDatabaseConnector } from "./db/connector"; +import { DbRoomStore } from "./db/roomstore"; const log = new Log("DiscordStore"); /** * Stores data for specific users and data not specific to rooms. */ export class DiscordStore { - /** - * @param {string} filepath Location of the SQLite database file. - */ public db: IDatabaseConnector; private version: number; private config: DiscordBridgeConfigDatabase; + private pRoomStore: DbRoomStore; constructor(private configOrFile: DiscordBridgeConfigDatabase|string) { if (typeof(configOrFile) === "string") { this.config = new DiscordBridgeConfigDatabase(); @@ -45,6 +44,10 @@ export class DiscordStore { this.version = 0; } + get roomStore() { + return this.pRoomStore; + } + public async backup_database(): Promise<void|{}> { if (this.config.filename == null) { log.warn("Backups not supported on non-sqlite connector"); @@ -327,6 +330,7 @@ export class DiscordStore { } try { this.db.Open(); + this.pRoomStore = new DbRoomStore(this.db); } catch (ex) { log.error("Error opening database:", ex); throw new Error("Couldn't open database. The appservice won't be able to continue.");