Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): application migrations #602

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from 'discord.js'
import { type AnyFunction, constants } from '../utils'
import { type BaseHandler, SettingProvider, WebSocketManager } from '.'
import { decorate, inject, injectable, type interfaces, optional } from 'inversify'
import { decorate, inject, injectable, type interfaces, multiInject, optional } from 'inversify'
import type { BaseMigration } from './migrations'
import applicationConfig from '../configs/application'

const { TYPES } = constants
Expand Down Expand Up @@ -48,6 +49,9 @@ export default class AroraClient<Ready extends boolean = boolean> extends Client
@optional()
private readonly aroraWs?: WebSocketManager

@multiInject(TYPES.Migration)
private readonly migrations!: BaseMigration[]

public mainGuild: Guild | null = null

private currentActivity: number = 0
Expand All @@ -61,6 +65,7 @@ export default class AroraClient<Ready extends boolean = boolean> extends Client

private async ready (): Promise<void> {
await this.settingProvider.init(this)
await this.runMigrations()

const mainGuildId = process.env.NODE_ENV === 'production'
? applicationConfig.productionMainGuildId
Expand Down Expand Up @@ -132,6 +137,14 @@ export default class AroraClient<Ready extends boolean = boolean> extends Client
return usedToken
}

private async runMigrations (): Promise<void> {
for (const migration of this.migrations) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort by name

if (await migration.shouldRun?.(this as AroraClient<true>) ?? true) {
await migration.run(this as AroraClient<true>)
}
}
}

private bindEvent (eventName: keyof ClientEvents): void {
const handler = this.eventHandlerFactory(eventName)
this.on(eventName, handler.handle.bind(handler))
Expand Down
2 changes: 2 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './migrations'
export * from './websocket'
export * as eventHandlers from './events'
export * as migrations from './migrations'
export type { default as BaseHandler } from './base'
export { default as AroraClient } from './client'
export { default as Dispatcher } from './dispatcher'
Expand Down
8 changes: 8 additions & 0 deletions src/client/migrations/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { AroraClient } from '..'
import type { QueryRunner } from 'typeorm'

export default interface BaseMigration {
shouldRun? (client: AroraClient<true>, queryRunner?: QueryRunner): boolean | Promise<boolean>
run (client: AroraClient<true>, queryRunner?: QueryRunner): void | Promise<void>
revert (client: AroraClient<true>, queryRunner?: QueryRunner): void | Promise<void>
}
1 change: 1 addition & 0 deletions src/client/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { default as BaseMigration } from './base'
16 changes: 16 additions & 0 deletions src/configs/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Argument, type ArgumentOptions, type BaseCommand } from '../interaction
import {
AroraClient,
type BaseHandler,
type BaseMigration,
Dispatcher,
SettingProvider,
WebSocketManager,
Expand Down Expand Up @@ -292,6 +293,21 @@ bind<interfaces.MultiFactory<managers.BaseManager<number | string, any, unknown>
}
)

// Migrations
container.onActivation(TYPES.Migration, (_context: interfaces.Context, migration: BaseMigration) => {
const handler = {
apply: function (target: (...args: any[]) => any, thisArgument: BaseMigration, argumentsList: any[]) {
return target.apply(thisArgument, [...argumentsList, dataSource.createQueryRunner()])
}
}
if (typeof migration.shouldRun !== 'undefined') {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just remove this

migration.shouldRun = new Proxy(migration.shouldRun, handler)
}
migration.run = new Proxy(migration.run, handler)
migration.revert = new Proxy(migration.revert, handler)
return migration
})

// Structures
bind<structures.ChannelGroup>(TYPES.Structure).to(structures.ChannelGroup)
.whenTargetNamed('ChannelGroup')
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const TYPES = {
Manager: Symbol.for('Manager'),
ManagerFactory: Symbol.for('ManagerFactory'),

Migration: Symbol.for('Migration'),

Structure: Symbol.for('Structure'),
StructureFactory: Symbol.for('StructureFactory'),

Expand Down