@@ -151,18 +151,21 @@ async function createDatabaseInstance(config: DatabaseConfig, schema: AnySchema,
151151 * Apply migrations for any database type
152152 */
153153async function applyMigrations ( db : AnyDatabase , config : DatabaseConfig , logger : FastifyBaseLogger ) {
154+ // Skip migrations in test mode
155+ if ( isTestMode ( ) ) {
156+ return ;
157+ }
158+
154159 const projectRootMigrationsDir = path . join ( process . cwd ( ) , 'drizzle' ) ;
155160 const migrationsPath = path . join ( projectRootMigrationsDir , 'migrations_sqlite' ) ;
156161
157162 try {
158163 await fs . access ( migrationsPath ) ;
159164 } catch {
160- if ( ! isTestMode ( ) ) {
161- logger . info ( {
162- operation : 'apply_migrations' ,
163- migrationsPath
164- } , `Migrations directory not found at: ${ migrationsPath } , skipping migrations.` ) ;
165- }
165+ logger . info ( {
166+ operation : 'apply_migrations' ,
167+ migrationsPath
168+ } , `Migrations directory not found at: ${ migrationsPath } , skipping migrations.` ) ;
166169 return ;
167170 }
168171
@@ -369,11 +372,9 @@ async function applyMigrations(db: AnyDatabase, config: DatabaseConfig, logger:
369372 */
370373export async function initializeDatabase ( logger : FastifyBaseLogger ) : Promise < boolean > {
371374 if ( isDbInitialized ) {
372- if ( ! isTestMode ( ) ) {
373- logger . info ( {
374- operation : 'initialize_database'
375- } , 'Database already initialized.' ) ;
376- }
375+ logger . info ( {
376+ operation : 'initialize_database'
377+ } , 'Database already initialized.' ) ;
377378 return true ;
378379 }
379380
@@ -419,11 +420,9 @@ export async function initializeDatabase(logger: FastifyBaseLogger): Promise<boo
419420 } catch ( error ) {
420421 const typedError = error as Error ;
421422 if ( typedError . message . includes ( 'No database selection found' ) ) {
422- if ( ! isTestMode ( ) ) {
423- logger . info ( {
424- operation : 'initialize_database'
425- } , 'No database configured yet. Please use the /api/db/setup endpoint to configure your database.' ) ;
426- }
423+ logger . info ( {
424+ operation : 'initialize_database'
425+ } , 'No database configured yet. Please use the /api/db/setup endpoint to configure your database.' ) ;
427426 } else {
428427 logger . error ( {
429428 operation : 'initialize_database' ,
@@ -459,21 +458,53 @@ export function getSchema(): AnySchema {
459458 * Get database status
460459 */
461460export function getDbStatus ( ) {
462- if ( ! dbConfig ) {
461+ try {
462+ if ( ! dbConfig ) {
463+ // Try to get config to see if one exists
464+ const config = getDatabaseConfig ( ) ;
465+ if ( config ) {
466+ return {
467+ configured : validateDatabaseConfig ( config ) ,
468+ initialized : isDbInitialized ,
469+ dialect : config . type ,
470+ type : config . type
471+ } ;
472+ }
473+ }
474+
475+ if ( ! dbConfig ) {
476+ return {
477+ configured : false ,
478+ initialized : false ,
479+ dialect : null ,
480+ type : null
481+ } ;
482+ }
483+
484+ return {
485+ configured : validateDatabaseConfig ( dbConfig ) ,
486+ initialized : isDbInitialized ,
487+ dialect : dbConfig . type ,
488+ type : dbConfig . type
489+ } ;
490+ } catch {
463491 return {
464492 configured : false ,
465493 initialized : false ,
466494 dialect : null ,
467495 type : null
468496 } ;
469497 }
470-
471- return {
472- configured : validateDatabaseConfig ( dbConfig ) ,
473- initialized : isDbInitialized ,
474- dialect : dbConfig . type ,
475- type : dbConfig . type
476- } ;
498+ }
499+
500+ /**
501+ * Reset database state (for testing)
502+ */
503+ export function resetDatabaseState ( ) {
504+ dbInstance = null ;
505+ dbSchema = null ;
506+ dbConfig = null ;
507+ isDbInitialized = false ;
477508}
478509
479510/**
@@ -492,7 +523,7 @@ export function executeDbOperation<T>(
492523interface DatabaseExtensionWithTables extends DatabaseExtension {
493524
494525 tableDefinitions ?: Record < string , Record < string , ( columnBuilder : any ) => any > > ;
495- onDatabaseInit ?: ( db : AnyDatabase , logger : FastifyBaseLogger ) => Promise < void > ;
526+ onDatabaseInit ?: ( db : AnyDatabase , schema : AnySchema ) => Promise < void > ;
496527}
497528
498529export function registerPluginTables ( plugins : Plugin [ ] , logger ?: FastifyBaseLogger ) {
@@ -515,11 +546,9 @@ export function registerPluginTables(plugins: Plugin[], logger?: FastifyBaseLogg
515546}
516547
517548export async function createPluginTables ( plugins : Plugin [ ] , logger : FastifyBaseLogger ) {
518- if ( ! isTestMode ( ) ) {
519- logger . info ( {
520- operation : 'create_plugin_tables'
521- } , 'Plugin tables are handled by migrations.' ) ;
522- }
549+ logger . info ( {
550+ operation : 'create_plugin_tables'
551+ } , 'Plugin tables are handled by migrations.' ) ;
523552}
524553
525554export async function initializePluginDatabases ( db : AnyDatabase , plugins : Plugin [ ] , logger : FastifyBaseLogger ) {
@@ -535,7 +564,15 @@ export async function initializePluginDatabases(db: AnyDatabase, plugins: Plugin
535564 try {
536565 // Create a child logger for this plugin
537566 const pluginLogger = logger . child ( { pluginId : plugin . meta . id } ) ;
538- await ext . onDatabaseInit ( db , pluginLogger ) ;
567+ // Get the current schema - use dbSchema directly if available, otherwise generate one
568+ let schema : AnySchema ;
569+ try {
570+ schema = getSchema ( ) ;
571+ } catch {
572+ // If getSchema fails, generate a basic schema for the plugin
573+ schema = generateSchema ( ) ;
574+ }
575+ await ext . onDatabaseInit ( db , schema ) ;
539576 if ( ! isTestMode ( ) ) {
540577 logger . info ( {
541578 operation : 'initialize_plugin_databases' ,
0 commit comments