@@ -3,7 +3,7 @@ import { Client as APIClient } from '@revanced/bot-api'
33import { createLogger } from '@revanced/bot-shared'
44import { Client as DiscordClient , type Message , Options , Partials } from 'discord.js'
55import { drizzle } from 'drizzle-orm/bun-sqlite'
6- import { existsSync , readdirSync , readFileSync } from 'fs '
6+ import { migrate } from 'drizzle-orm/bun-sqlite/migrator '
77import { join } from 'path'
88import { __getConfig , config } from './config'
99import * as schemas from './database/schemas'
@@ -30,37 +30,17 @@ export const api = {
3030}
3131
3232const DatabasePath = process . env [ 'DATABASE_PATH' ]
33- const DatabaseSchemaDir = join ( import . meta. dir , '..' , '.drizzle' )
34-
35- let dbSchemaFileName : string | undefined
36-
37- if ( DatabasePath && ! existsSync ( DatabasePath ) ) {
38- logger . warn ( 'Database file not found, trying to create from schema...' )
39-
40- try {
41- const file = readdirSync ( DatabaseSchemaDir , { withFileTypes : true } )
42- . filter ( file => file . isFile ( ) && file . name . endsWith ( '.sql' ) )
43- . sort ( )
44- . at ( - 1 )
45-
46- if ( ! file ) throw new Error ( 'No schema file found' )
47-
48- dbSchemaFileName = file . name
49- logger . debug ( `Using schema file: ${ dbSchemaFileName } ` )
50- } catch ( e ) {
51- logger . fatal ( 'Could not create database from schema, check if the schema file exists and is accessible' )
52- logger . fatal ( e )
53- process . exit ( 1 )
54- }
55- }
5633
5734const db = new Database ( DatabasePath , { readwrite : true , create : true } )
58- if ( dbSchemaFileName ) db . run ( readFileSync ( join ( DatabaseSchemaDir , dbSchemaFileName ) ) . toString ( ) )
5935
60- export const database = drizzle ( db , {
36+ const database = drizzle ( db , {
6137 schema : schemas ,
6238} )
6339
40+ migrate ( database , { migrationsFolder : join ( import . meta. dir , '..' , '.drizzle' ) } )
41+
42+ export { database }
43+
6444export const discord = {
6545 client : new DiscordClient ( {
6646 intents : [
0 commit comments