fix: improve plugins functionality with server management
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class UserScopedRoomsAndMessages1000000000009 implements MigrationInterface {
|
||||
name = 'UserScopedRoomsAndMessages1000000000009';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE IF NOT EXISTS "room_owners" (
|
||||
"roomId" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"savedAt" INTEGER NOT NULL,
|
||||
PRIMARY KEY ("roomId", "userId")
|
||||
)
|
||||
`);
|
||||
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_room_owners_userId" ON "room_owners" ("userId")`);
|
||||
|
||||
const columns = await queryRunner.query(`PRAGMA table_info("messages")`) as Array<{ name?: string }>;
|
||||
const hasOwnerUserId = columns.some((column) => column.name === 'ownerUserId');
|
||||
|
||||
if (!hasOwnerUserId) {
|
||||
await queryRunner.query(`ALTER TABLE "messages" ADD COLUMN "ownerUserId" TEXT`);
|
||||
}
|
||||
|
||||
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_messages_owner_room" ON "messages" ("ownerUserId", "roomId")`);
|
||||
|
||||
const metaRows = await queryRunner.query(`SELECT "value" FROM "meta" WHERE "key" = 'currentUserId' LIMIT 1`) as Array<{ value?: string | null }>;
|
||||
const currentUserId = metaRows[0]?.value?.trim();
|
||||
|
||||
if (!currentUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
await queryRunner.query(
|
||||
`INSERT OR IGNORE INTO "room_owners" ("roomId", "userId", "savedAt") SELECT "id", ?, ? FROM "rooms"`,
|
||||
[currentUserId, now]
|
||||
);
|
||||
await queryRunner.query(
|
||||
`UPDATE "messages" SET "ownerUserId" = ? WHERE "ownerUserId" IS NULL`,
|
||||
[currentUserId]
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP INDEX IF EXISTS "idx_messages_owner_room"`);
|
||||
await queryRunner.query(`DROP INDEX IF EXISTS "idx_room_owners_userId"`);
|
||||
await queryRunner.query(`DROP TABLE IF EXISTS "room_owners"`);
|
||||
}
|
||||
}
|
||||
56
electron/migrations/1000000000010-UserScopedPluginData.ts
Normal file
56
electron/migrations/1000000000010-UserScopedPluginData.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class UserScopedPluginData1000000000010 implements MigrationInterface {
|
||||
name = 'UserScopedPluginData1000000000010';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
const columns = await queryRunner.query(`PRAGMA table_info("plugin_data")`) as Array<{ name?: string }>;
|
||||
const hasOwnerUserId = columns.some((column) => column.name === 'ownerUserId');
|
||||
|
||||
if (hasOwnerUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const metaRows = await queryRunner.query(`SELECT "value" FROM "meta" WHERE "key" = 'currentUserId' LIMIT 1`) as Array<{ value?: string | null }>;
|
||||
const currentUserId = metaRows[0]?.value?.trim() ?? '';
|
||||
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE "temporary_plugin_data" (
|
||||
"ownerUserId" TEXT NOT NULL,
|
||||
"pluginId" TEXT NOT NULL,
|
||||
"scope" TEXT NOT NULL,
|
||||
"serverId" TEXT NOT NULL,
|
||||
"key" TEXT NOT NULL,
|
||||
"valueJson" TEXT NOT NULL,
|
||||
"updatedAt" INTEGER NOT NULL,
|
||||
PRIMARY KEY ("ownerUserId", "pluginId", "scope", "serverId", "key")
|
||||
)
|
||||
`);
|
||||
await queryRunner.query(
|
||||
`INSERT INTO "temporary_plugin_data" ("ownerUserId", "pluginId", "scope", "serverId", "key", "valueJson", "updatedAt")
|
||||
SELECT ?, "pluginId", "scope", "serverId", "key", "valueJson", "updatedAt" FROM "plugin_data"`,
|
||||
[currentUserId]
|
||||
);
|
||||
await queryRunner.query(`DROP TABLE "plugin_data"`);
|
||||
await queryRunner.query(`ALTER TABLE "temporary_plugin_data" RENAME TO "plugin_data"`);
|
||||
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_plugin_data_owner_plugin_scope" ON "plugin_data" ("ownerUserId", "pluginId", "scope")`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE "temporary_plugin_data" (
|
||||
"pluginId" TEXT NOT NULL,
|
||||
"scope" TEXT NOT NULL,
|
||||
"serverId" TEXT NOT NULL,
|
||||
"key" TEXT NOT NULL,
|
||||
"valueJson" TEXT NOT NULL,
|
||||
"updatedAt" INTEGER NOT NULL,
|
||||
PRIMARY KEY ("pluginId", "scope", "serverId", "key")
|
||||
)`);
|
||||
await queryRunner.query(`INSERT OR REPLACE INTO "temporary_plugin_data" ("pluginId", "scope", "serverId", "key", "valueJson", "updatedAt")
|
||||
SELECT "pluginId", "scope", "serverId", "key", "valueJson", "updatedAt" FROM "plugin_data"`);
|
||||
await queryRunner.query(`DROP INDEX IF EXISTS "idx_plugin_data_owner_plugin_scope"`);
|
||||
await queryRunner.query(`DROP TABLE "plugin_data"`);
|
||||
await queryRunner.query(`ALTER TABLE "temporary_plugin_data" RENAME TO "plugin_data"`);
|
||||
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_plugin_data_plugin_scope" ON "plugin_data" ("pluginId", "scope")`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user