@@ -4,61 +4,68 @@ import fs from "fs";
4
4
import path from "path" ;
5
5
import { migrate } from "drizzle-orm/bun-sqlite/migrator" ;
6
6
7
- // Define the database URL - for development we'll use a local SQLite file
8
- const dataDir = path . join ( process . cwd ( ) , "data" ) ;
9
- // Ensure data directory exists
10
- if ( ! fs . existsSync ( dataDir ) ) {
11
- fs . mkdirSync ( dataDir , { recursive : true } ) ;
12
- }
13
-
14
- const dbPath = path . join ( dataDir , "gitea-mirror.db" ) ;
7
+ // Skip database initialization in test environment
8
+ let db : ReturnType < typeof drizzle > ;
15
9
16
- // Create an empty database file if it doesn't exist
17
- if ( ! fs . existsSync ( dbPath ) ) {
18
- fs . writeFileSync ( dbPath , "" ) ;
19
- }
10
+ if ( process . env . NODE_ENV !== "test" ) {
11
+ // Define the database URL - for development we'll use a local SQLite file
12
+ const dataDir = path . join ( process . cwd ( ) , "data" ) ;
13
+ // Ensure data directory exists
14
+ if ( ! fs . existsSync ( dataDir ) ) {
15
+ fs . mkdirSync ( dataDir , { recursive : true } ) ;
16
+ }
20
17
21
- // Create SQLite database instance using Bun's native driver
22
- let sqlite : Database ;
23
- try {
24
- sqlite = new Database ( dbPath ) ;
25
- console . log ( "Successfully connected to SQLite database using Bun's native driver" ) ;
26
- } catch ( error ) {
27
- console . error ( "Error opening database:" , error ) ;
28
- throw error ;
29
- }
18
+ const dbPath = path . join ( dataDir , "gitea-mirror.db" ) ;
30
19
31
- // Create drizzle instance with the SQLite client
32
- export const db = drizzle ( { client : sqlite } ) ;
20
+ // Create an empty database file if it doesn't exist
21
+ if ( ! fs . existsSync ( dbPath ) ) {
22
+ fs . writeFileSync ( dbPath , "" ) ;
23
+ }
33
24
34
- /**
35
- * Run Drizzle migrations
36
- */
37
- function runDrizzleMigrations ( ) {
25
+ // Create SQLite database instance using Bun's native driver
26
+ let sqlite : Database ;
38
27
try {
39
- console . log ( "🔄 Checking for pending migrations..." ) ;
40
-
41
- // Check if migrations table exists
42
- const migrationsTableExists = sqlite
43
- . query ( "SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'" )
44
- . get ( ) ;
45
-
46
- if ( ! migrationsTableExists ) {
47
- console . log ( "📦 First time setup - running initial migrations..." ) ;
48
- }
49
-
50
- // Run migrations using Drizzle migrate function
51
- migrate ( db , { migrationsFolder : "./drizzle" } ) ;
52
-
53
- console . log ( "✅ Database migrations completed successfully" ) ;
28
+ sqlite = new Database ( dbPath ) ;
29
+ console . log ( "Successfully connected to SQLite database using Bun's native driver" ) ;
54
30
} catch ( error ) {
55
- console . error ( "❌ Error running migrations :" , error ) ;
31
+ console . error ( "Error opening database :" , error ) ;
56
32
throw error ;
57
33
}
34
+
35
+ // Create drizzle instance with the SQLite client
36
+ db = drizzle ( { client : sqlite } ) ;
37
+
38
+ /**
39
+ * Run Drizzle migrations
40
+ */
41
+ function runDrizzleMigrations ( ) {
42
+ try {
43
+ console . log ( "🔄 Checking for pending migrations..." ) ;
44
+
45
+ // Check if migrations table exists
46
+ const migrationsTableExists = sqlite
47
+ . query ( "SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'" )
48
+ . get ( ) ;
49
+
50
+ if ( ! migrationsTableExists ) {
51
+ console . log ( "📦 First time setup - running initial migrations..." ) ;
52
+ }
53
+
54
+ // Run migrations using Drizzle migrate function
55
+ migrate ( db , { migrationsFolder : "./drizzle" } ) ;
56
+
57
+ console . log ( "✅ Database migrations completed successfully" ) ;
58
+ } catch ( error ) {
59
+ console . error ( "❌ Error running migrations:" , error ) ;
60
+ throw error ;
61
+ }
62
+ }
63
+
64
+ // Run Drizzle migrations after db is initialized
65
+ runDrizzleMigrations ( ) ;
58
66
}
59
67
60
- // Run Drizzle migrations after db is initialized
61
- runDrizzleMigrations ( ) ;
68
+ export { db } ;
62
69
63
70
// Export all table definitions from schema
64
71
export {
0 commit comments