# ✅ SQL Files Not Included in Publish - Solution

## Problem

When publishing Jellyfin, the SQL files in the `sql/` directory are not being included in the publish output.

## Root Cause

MSBuild's content copy behavior is tricky with `dotnet publish`. The files need special handling to be included in the publish directory.

## ✅ Solution: Manual Copy Post-Publish

Since the automatic copy isn't working reliably, here's a simple post-publish script:

### Option 1: PowerShell Script (Recommended)

Create `copy-sql-files.ps1` in the project root:

```powershell
# copy-sql-files.ps1
# Run this after publish to copy SQL files

param(
    [string]$PublishDir = "Jellyfin.Server\bin\Release\net11.0\publish"
)

Write-Host "Copying SQL files to $PublishDir..." -ForegroundColor Cyan

# Create sql directory
New-Item -ItemType Directory -Path "$PublishDir\sql" -Force | Out-Null
New-Item -ItemType Directory -Path "$PublishDir\sql\schema_init" -Force | Out-Null

# Copy SQL files
Copy-Item "Jellyfin.Server\sql\*.sql" "$PublishDir\sql\" -Force
Copy-Item "Jellyfin.Server\sql\schema_init\*.sql" "$PublishDir\sql\schema_init\" -Force -ErrorAction SilentlyContinue

Write-Host "✓ SQL files copied successfully!" -ForegroundColor Green

# List copied files
Get-ChildItem "$PublishDir\sql" -Recurse -File | Select-Object Name, Length
```

**Usage:**
```powershell
# After publishing
dotnet publish Jellyfin.Server\Jellyfin.Server.csproj -c Release

# Copy SQL files
.\copy-sql-files.ps1
```

### Option 2: Batch Script

Create `copy-sql-files.bat`:

```batch
@echo off
SET PUBLISH_DIR=Jellyfin.Server\bin\Release\net11.0\publish

echo Copying SQL files to %PUBLISH_DIR%...

mkdir "%PUBLISH_DIR%\sql" 2>nul
mkdir "%PUBLISH_DIR%\sql\schema_init" 2>nul

copy /Y "Jellyfin.Server\sql\*.sql" "%PUBLISH_DIR%\sql\" >nul
copy /Y "Jellyfin.Server\sql\schema_init\*.sql" "%PUBLISH_DIR%\sql\schema_init\" 2>nul >nul

echo SQL files copied successfully!
dir "%PUBLISH_DIR%\sql" /B
```

### Option 3: Automated in Publish Command

Create a combined script that does both:

```powershell
# publish-with-sql.ps1

Write-Host "Publishing Jellyfin..." -ForegroundColor Cyan
dotnet publish Jellyfin.Server\Jellyfin.Server.csproj -c Release -o Jellyfin.Server\bin\Release\net11.0\publish

if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ Publish successful" -ForegroundColor Green
    
    Write-Host "Copying SQL files..." -ForegroundColor Cyan
    
    $publishDir = "Jellyfin.Server\bin\Release\net11.0\publish"
    
    New-Item -ItemType Directory -Path "$publishDir\sql" -Force | Out-Null
    New-Item -ItemType Directory -Path "$publishDir\sql\schema_init" -Force | Out-Null
    
    Copy-Item "Jellyfin.Server\sql\*.sql" "$publishDir\sql\" -Force
    Copy-Item "Jellyfin.Server\sql\schema_init\*.sql" "$publishDir\sql\schema_init\" -Force -ErrorAction SilentlyContinue
    
    Write-Host "✓ SQL files copied" -ForegroundColor Green
    
    Write-Host "`nPublish complete at: $publishDir" -ForegroundColor Cyan
    Get-ChildItem "$publishDir\sql" -Recurse | Select-Object FullName
} else {
    Write-Host "❌ Publish failed" -ForegroundColor Red
}
```

**Usage:**
```powershell
.\publish-with-sql.ps1
```

## ✅ Solution: Fix .csproj (Alternative)

If you want to fix it properly in the .csproj, use this configuration:

### Edit `Jellyfin.Server\Jellyfin.Server.csproj`

Add this **before** the closing `</Project>` tag:

```xml
  <!-- Ensure SQL files are in the project directory -->
  <ItemGroup>
    <None Include="sql\**\*.sql">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
      <Pack>false</Pack>
    </None>
  </ItemGroup>

  <!-- Manual copy target to ensure SQL files are included -->
  <Target Name="CopySQLToOutput" AfterTargets="Build;Publish">
    <Message Importance="high" Text="Copying SQL files to output directory..." />
    
    <!-- Create sql directory -->
    <MakeDir Directories="$(OutDir)sql" />
    <MakeDir Directories="$(OutDir)sql\schema_init" />
    
    <!-- Copy SQL files -->
    <ItemGroup>
      <SQLFilesMain Include="$(ProjectDir)sql\*.sql" />
      <SQLFilesInit Include="$(ProjectDir)sql\schema_init\*.sql" />
    </ItemGroup>
    
    <Copy SourceFiles="@(SQLFilesMain)" 
          DestinationFolder="$(OutDir)sql" 
          SkipUnchangedFiles="true" />
          
    <Copy SourceFiles="@(SQLFilesInit)" 
          DestinationFolder="$(OutDir)sql\schema_init" 
          SkipUnchangedFiles="true" />
    
    <Message Importance="high" Text="✓ SQL files copied to $(OutDir)sql" />
  </Target>
```

Then rebuild:
```powershell
dotnet clean Jellyfin.Server\Jellyfin.Server.csproj -c Release
dotnet build Jellyfin.Server\Jellyfin.Server.csproj -c Release
```

## Verification

After publishing, verify SQL files are present:

```powershell
# Check if files exist
Test-Path "Jellyfin.Server\bin\Release\net11.0\publish\sql\all_performance_indexes.sql"

# List all SQL files
Get-ChildItem "Jellyfin.Server\bin\Release\net11.0\publish\sql" -Recurse | Select-Object FullName
```

Expected output:
```
FullName
--------
E:\Projects\pgsql-jellyfin\Jellyfin.Server\bin\Release\net11.0\publish\sql\all_performance_indexes.sql
E:\Projects\pgsql-jellyfin\Jellyfin.Server\bin\Release\net11.0\publish\sql\add_base_performance_indexes.sql
E:\Projects\pgsql-jellyfin\Jellyfin.Server\bin\Release\net11.0\publish\sql\diagnostics.sql
...
```

## Quick Test

```powershell
# Publish
dotnet publish Jellyfin.Server\Jellyfin.Server.csproj -c Release

# Copy SQL
.\copy-sql-files.ps1

# Verify
dir "Jellyfin.Server\bin\Release\net11.0\publish\sql"

# Should see:
# all_performance_indexes.sql
# add_base_performance_indexes.sql
# diagnostics.sql
# schema_init\
```

## Files to Create

1. ✅ `copy-sql-files.ps1` - PowerShell copy script
2. ✅ `copy-sql-files.bat` - Batch copy script  
3. ✅ `publish-with-sql.ps1` - Combined publish + copy script

All three scripts are provided above. Choose whichever fits your workflow!

## Summary

**Quick Fix:** Use `publish-with-sql.ps1` - one command does everything
**Best Fix:** Update .csproj with the Target shown above - automatic and permanent

The SQL files will be at:
```
publish/
└── sql/
    ├── all_performance_indexes.sql
    ├── add_base_performance_indexes.sql
    ├── diagnostics.sql
    └── schema_init/
        └── 10_create_supplementary_indexes.sql
```

And the `PostgresDatabaseProvider` will find them automatically at startup! 🎉
