Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 5e2581c4ce02d45dbbeef7ba291f1dc723cb937e
  • master par défaut protégée
2 résultats

login.php

Blame
  • store.ts 8,95 Kio
    import * as SQLite3 from "sqlite3";
    import * as log from "npmlog";
    import * as Bluebird from "bluebird";
    import * as fs from "fs";
    import { IDbSchema } from "./db/schema/dbschema";
    import { IDbData} from "./db/dbdatainterface";
    const CURRENT_SCHEMA = 7;
    /**
     * 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: any;
      private version: number;
      private filepath: string;
      constructor (filepath: string) {
        this.version = null;
        this.filepath = filepath;
      }
    
      public backup_database(): Promise<null> {
        if (this.filepath === ":memory:") {
          log.info("DiscordStore", "Can't backup a :memory: database.");
          return Promise.resolve();
        }
        const BACKUP_NAME = this.filepath + ".backup";
    
        return new Promise((resolve, reject) => {
          // Check to see if a backup file already exists.
          fs.access(BACKUP_NAME, (err) => {
            return resolve(err === null);
          });
        }).then((result) => {
          return new Promise((resolve, reject) => {
            if (!result) {
              log.warn("DiscordStore", "NOT backing up database while a file already exists");
              resolve(true);
              return;
            }
            const rd = fs.createReadStream(this.filepath);
            rd.on("error", reject);
            const wr = fs.createWriteStream(BACKUP_NAME);
            wr.on("error", reject);
            wr.on("close", resolve);
            rd.pipe(wr);
          });
        });
      }
    
      /**
       * Checks the database has all the tables needed.
       */
      public async init (overrideSchema: number = 0) {
        log.info("DiscordStore", "Starting DB Init");
        await this.open_database();
        let version = await this.getSchemaVersion();
        const targetSchema = overrideSchema || CURRENT_SCHEMA;
        while (version < targetSchema) {
          version++;
          const schemaClass = require(`./db/schema/v${version}.js`).Schema;
          const schema = (new schemaClass() as IDbSchema);
          log.info("DiscordStore", `Updating database to v${version}, "${schema.description}"`);
          try {
            await schema.run(this);
            log.info("DiscordStore", "Updated database to version %s", version);
          } catch (ex) {
            log.error("DiscordStore", "Couldn't update database to schema %s", version);
            log.error("DiscordStore", ex);