@@ -99,7 +99,7 @@ function listDatabases() {
9999 // List all db.* directories
100100 const files = fs . readdirSync ( meteorLocalDir ) ;
101101 const dbDirs = files
102- . filter ( file => file . startsWith ( 'db.' ) && fs . statSync ( path . join ( meteorLocalDir , file ) ) . isDirectory ( ) )
102+ . filter ( file => file . startsWith ( 'db.' ) && fs . lstatSync ( path . join ( meteorLocalDir , file ) ) . isDirectory ( ) )
103103 . map ( file => file . substring ( 3 ) ) ;
104104
105105 console . log ( '\nAvailable databases:' ) ;
@@ -125,10 +125,13 @@ function switchDatabase(dbName) {
125125
126126 // Check if we're already using this database
127127 if ( fs . existsSync ( dbLink ) ) {
128- const currentTarget = fs . readlinkSync ( dbLink ) ;
129- if ( currentTarget === `db.${ dbName } ` ) {
130- console . log ( `✓ Already using database: ${ dbName } ` ) ;
131- return ;
128+ const stats = fs . lstatSync ( dbLink ) ;
129+ if ( stats . isSymbolicLink ( ) ) {
130+ const currentTarget = fs . readlinkSync ( dbLink ) ;
131+ if ( currentTarget === `db.${ dbName } ` ) {
132+ console . log ( `✓ Already using database: ${ dbName } ` ) ;
133+ return ;
134+ }
132135 }
133136 }
134137
@@ -144,14 +147,23 @@ function switchDatabase(dbName) {
144147 if ( stats . isSymbolicLink ( ) ) {
145148 fs . unlinkSync ( dbLink ) ;
146149 } else {
147- // It's a real directory - back it up as 'default'
150+ // It's a real directory - back it up with timestamp
148151 const defaultDb = path . join ( meteorLocalDir , 'db.default' ) ;
149152 if ( ! fs . existsSync ( defaultDb ) ) {
150153 console . log ( `Backing up existing database to: default` ) ;
151154 fs . renameSync ( dbLink , defaultDb ) ;
152155 } else {
153- // Default already exists, just remove current
154- fs . rmSync ( dbLink , { recursive : true , force : true } ) ;
156+ // Default already exists, create timestamped backup instead of deleting
157+ const timestamp = new Date ( ) . toISOString ( ) . replace ( / [: .] / g, '-' ) . substring ( 0 , 19 ) ;
158+ let backupName = path . join ( meteorLocalDir , `db.backup.${ timestamp } ` ) ;
159+ // Ensure unique backup name
160+ let suffix = 0 ;
161+ while ( fs . existsSync ( backupName ) ) {
162+ suffix ++ ;
163+ backupName = path . join ( meteorLocalDir , `db.backup.${ timestamp } .${ suffix } ` ) ;
164+ }
165+ console . log ( `Backing up existing database to: ${ path . basename ( backupName ) } ` ) ;
166+ fs . renameSync ( dbLink , backupName ) ;
155167 }
156168 }
157169 }
0 commit comments