@@ -298,13 +298,17 @@ clear_original_directory() {
298298    #  List of files/directories to preserve (already backed up)
299299    local  preserve_patterns=(
300300        " data" 
301+         " data/*" 
301302        " .env" 
302303        " *.log" 
303304        " update.log" 
304305        " *.backup" 
305306        " *.bak" 
306307        " .git" 
307-         " scripts" 
308+         " scripts\ct\*" 
309+         " scripts\install\*" 
310+         " scripts\tools\*" 
311+         " scripts\vm\*" 
308312    )
309313
310314    #  Remove all files except preserved ones
@@ -412,6 +416,24 @@ restore_backup_files() {
412416    fi 
413417}
414418
419+ #  Verify database was restored correctly
420+ verify_database_restored () {
421+     log " Verifying database was restored correctly..." 
422+     
423+     if  [ !  -f  " data/database.sqlite"   ];  then 
424+         log_error " Database file not found after restore!" 
425+         return  1
426+     fi 
427+     
428+     local  db_size=$( stat -f%z " data/database.sqlite"   2> /dev/null ||  stat -c%s " data/database.sqlite"   2> /dev/null) 
429+     if  [ " $db_size "   -eq  0 ];  then 
430+         log_error " Database file is empty after restore!" 
431+         return  1
432+     fi 
433+     
434+     log_success " Database verified (size: $db_size  bytes)" 
435+ }
436+ 
415437#  Ensure DATABASE_URL is set in .env file for Prisma
416438ensure_database_url () {
417439    log " Ensuring DATABASE_URL is set in .env file..." 
@@ -525,6 +547,7 @@ update_files() {
525547        " *.backup" 
526548        " *.bak" 
527549        " scripts" 
550+         " prisma/migrations" 
528551    )
529552
530553    #  Find the actual source directory (strip the top-level directory)
@@ -555,7 +578,7 @@ update_files() {
555578        local  should_exclude=false
556579
557580        for  pattern  in  " ${exclude_patterns[@]} " ;  do 
558-             if  [[ " $rel_path "   ==  $pattern  ]];  then 
581+             if  [[ " $rel_path "   ==  $pattern  ]]  ||  [[  " $rel_path "   ==   $pattern / *  ]] ;  then 
559582                should_exclude=true
560583                break 
561584            fi 
@@ -650,6 +673,15 @@ install_and_build() {
650673    fi 
651674    log_success " Prisma client generated successfully" 
652675
676+     #  Check if Prisma migrations exist and are compatible
677+     if  [ -d  " prisma/migrations"   ];  then 
678+         log " Existing migration history detected" 
679+         local  migration_count=$( find prisma/migrations -type d -mindepth 1 |  wc -l) 
680+         log " Found $migration_count  existing migrations" 
681+     else 
682+         log_warning " No existing migration history found - this may be a fresh install" 
683+     fi 
684+     
653685    #  Run Prisma migrations
654686    log " Running Prisma migrations..." 
655687    if  !  npx prisma migrate deploy >  " $npm_log "   2>&1 ;  then 
@@ -894,6 +926,12 @@ main() {
894926    #  Restore .env and data directory before building
895927    restore_backup_files
896928
929+     #  Verify database was restored correctly
930+     if  !  verify_database_restored;  then 
931+         log_error " Database verification failed, rolling back..." 
932+         rollback
933+     fi 
934+     
897935    #  Ensure DATABASE_URL is set for Prisma
898936    ensure_database_url
899937
0 commit comments