From f4cbdaec228d21cec656c3808d2bc5c0cc574fca Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sat, 10 Oct 2020 01:40:16 +0800 Subject: [PATCH] feat(mysql): add initialization for chess/monitor/rss/schedule --- packages/plugin-chess/src/index.ts | 7 ++++++- packages/plugin-monitor/src/database.ts | 3 ++- packages/plugin-mysql/src/database.ts | 2 +- packages/plugin-rss/src/index.ts | 3 ++- packages/plugin-schedule/src/database.ts | 13 +++++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/plugin-chess/src/index.ts b/packages/plugin-chess/src/index.ts index d6ed613dda..f4130f0065 100644 --- a/packages/plugin-chess/src/index.ts +++ b/packages/plugin-chess/src/index.ts @@ -1,10 +1,15 @@ -import { Context, Group } from 'koishi-core' +import { Context, extendDatabase, Group } from 'koishi-core' import { isInteger } from 'koishi-utils' import { State, MoveResult, StateData } from './state' +import MysqlDatabase from 'koishi-plugin-mysql/dist/database' import * as go from './go' import * as gomoku from './gomoku' import * as othello from './othello' +extendDatabase('koishi-plugin-mysql', ({ tables }) => { + tables.group.chess = `JSON NULL DEFAULT NULL` +}) + interface Rule { placement?: 'grid' | 'cross' create?: (this: State) => string | void diff --git a/packages/plugin-monitor/src/database.ts b/packages/plugin-monitor/src/database.ts index 9996fc749f..15fa43d785 100644 --- a/packages/plugin-monitor/src/database.ts +++ b/packages/plugin-monitor/src/database.ts @@ -75,8 +75,9 @@ extendDatabase('koishi-plugin-mysql', { }, }) -extendDatabase('koishi-plugin-mysql', ({ listFields }) => { +extendDatabase('koishi-plugin-mysql', ({ listFields, tables }) => { listFields.push('subscribe.names') + tables.group.subscribe = `JSON NULL DEFAULT NULL` }) extendDatabase('koishi-plugin-mongo', { diff --git a/packages/plugin-mysql/src/database.ts b/packages/plugin-mysql/src/database.ts index 6635953b89..3ca9af3619 100644 --- a/packages/plugin-mysql/src/database.ts +++ b/packages/plugin-mysql/src/database.ts @@ -50,7 +50,7 @@ export default class MysqlDatabase { async start() { this.pool = createPool(this.config) const tables = await this.select('information_schema.tables', ['TABLE_NAME'], 'TABLE_SCHEMA = ?', [this.config.database]) - const names = new Set(tables.map(data => data.TABLE_NAME)) + const names = new Set(tables.map(data => data.TABLE_NAME)) for (const name of Object.keys(MysqlDatabase.tables) as TableType[]) { if (names.has(name)) return const table = MysqlDatabase.tables[name] diff --git a/packages/plugin-rss/src/index.ts b/packages/plugin-rss/src/index.ts index 6b9df56090..61840c32b2 100644 --- a/packages/plugin-rss/src/index.ts +++ b/packages/plugin-rss/src/index.ts @@ -13,8 +13,9 @@ Group.extend(() => ({ rss: [], })) -extendDatabase('koishi-plugin-mysql', ({ listFields }) => { +extendDatabase('koishi-plugin-mysql', ({ listFields, tables }) => { listFields.push('group.rss') + tables.group.rss = `TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'` }) export interface Config { diff --git a/packages/plugin-schedule/src/database.ts b/packages/plugin-schedule/src/database.ts index 69802f95d3..568dca22f9 100644 --- a/packages/plugin-schedule/src/database.ts +++ b/packages/plugin-schedule/src/database.ts @@ -24,6 +24,19 @@ export interface Schedule { session: Session } +extendDatabase(MysqlDatabase, (Database) => { + Object.assign(Database.tables.schedule = [ + 'PRIMARY KEY (`id`) USING BTREE', + ], { + id: `INT(10) UNSIGNED NOT NULL AUTO_INCREMENT`, + assignee: `BIGINT(20) NOT NULL DEFAULT '0'`, + time: `TIMESTAMP NULL DEFAULT NULL`, + interval: `BIGINT(20) UNSIGNED NOT NULL DEFAULT '0'`, + command: `MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci'`, + session: `JSON NOT NULL`, + }) +}) + extendDatabase('koishi-plugin-mysql', { createSchedule(time, interval, command, session) { return this.create('schedule', { time, assignee: session.selfId, interval, command, session })