Skip to content

Commit

Permalink
[FEATURE]: add migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Jan 24, 2024
1 parent c8a040b commit a0e7820
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
21 changes: 16 additions & 5 deletions src/migrations/db/1706110397838-add-isbot-to-guildmember-schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { connectDB } from '../../database';
import 'dotenv/config';
import { connectDB } from '../../database';
import isBotLogic from '../utils/isBotLogic';
import { DatabaseManager } from '@togethercrew.dev/db';
import { Client, GatewayIntentBits } from 'discord.js';
import config from '../../config';
const { Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages } = GatewayIntentBits;


export const up = async () => {
await connectDB();
const connection = await DatabaseManager.getInstance().getTenantDb("681946187490000803");
const client = new Client({
intents: [Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages],
});

await connection.createCollection('my_collection');
await client.login(config.discord.botToken);
await connectDB();
const connection1 = await DatabaseManager.getInstance().getTenantDb('1023936505321881641');
const connection2 = await DatabaseManager.getInstance().getTenantDb('949124961187016764');
await isBotLogic(connection1, client, '1023936505321881641');
await isBotLogic(connection2, client, '949124961187016764');
};

export const down = async () => {
await connectDB();
// TODO: Implement rollback logic if needed
};
7 changes: 2 additions & 5 deletions src/migrations/utils/isBotLogic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Snowflake } from 'discord.js';
import { Client, Snowflake } from 'discord.js';
import { GuildMember } from 'discord.js';
import { Connection } from 'mongoose';
import parentLogger from '../../config/logger';
import { guildMemberService } from '../../database/services';
import { coreService } from '../../services';
import { IGuildMember, } from '@togethercrew.dev/db';

const logger = parentLogger.child({ module: 'Migration-isBot' });
Expand All @@ -25,9 +24,7 @@ function pushMembersToArray(arr: IGuildMember[], guildMembersArray: GuildMember[
* @param {Connection} connection - Mongoose connection object for the database.
* @param {Snowflake} guildId - The identifier of the guild to extract information from.
*/
export default async function isBotLogic(connection: Connection, guildId: Snowflake) {
const client = await coreService.DiscordBotManager.getClient();

export default async function isBotLogic(connection: Connection, client: Client, guildId: Snowflake) {
logger.info({ guild_id: guildId }, 'add-isBot-to-guilbMember-schema migration is running');
try {
console.log(guildId)
Expand Down

0 comments on commit a0e7820

Please sign in to comment.