UNPKG

@coko/server

Version:

Reusable server for use by Coko's projects

46 lines 2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.down = exports.up = void 0; const useTransaction_1 = __importDefault(require("../../useTransaction")); const chatChannel_model_1 = __importDefault(require("../chatChannel.model")); const OLD_TABLE_NAME = 'chat_threads'; const NEW_TABLE_NAME = 'chat_channels'; const OLD_TYPE = 'chatThread'; const NEW_TYPE = 'chatChannel'; const OLD_COLUMN_NAME = 'chat_thread_id'; const NEW_COLUMN_NAME = 'chat_channel_id'; const MESSAGE_TABLE = 'chat_messages'; const up = async (db) => { await db.schema.renameTable(OLD_TABLE_NAME, NEW_TABLE_NAME); const channels = await chatChannel_model_1.default.query(); await (0, useTransaction_1.default)(async (trx) => Promise.all(channels.map(async (c) => { await chatChannel_model_1.default.query(trx).findById(c.id).patch({ type: NEW_TYPE, }); }))); await db.schema.table(MESSAGE_TABLE, async (table) => { table.dropForeign(OLD_COLUMN_NAME); table.renameColumn(OLD_COLUMN_NAME, NEW_COLUMN_NAME); table.foreign(NEW_COLUMN_NAME).references(`${NEW_TABLE_NAME}.id`); }); }; exports.up = up; const down = async (db) => { await db.schema.renameTable(NEW_TABLE_NAME, OLD_TABLE_NAME); const threads = await db('chat_threads'); await db.transaction(async (trx) => Promise.all(threads.map(async (thread) => { await trx('chat_threads').where('id', thread.id).update({ type: OLD_TYPE, }); }))); await db.schema.table(MESSAGE_TABLE, async (table) => { table.dropForeign(NEW_COLUMN_NAME); table.renameColumn(NEW_COLUMN_NAME, OLD_COLUMN_NAME); table.foreign(OLD_COLUMN_NAME).references(`${OLD_TABLE_NAME}.id`); }); }; exports.down = down; //# sourceMappingURL=1722494210-rename-chat-thread-to-chat-channel.js.map