# Generate PostgreSQL Migrations from SQLite This script generates PostgreSQL migrations that match the SQLite migration schema. ## Prerequisites - .NET SDK installed - `dotnet-ef` tools installed: `dotnet tool install --global dotnet-ef` - PostgreSQL database running (for testing) ## Step 1: Initial Migration Generation Since the DbContext model is shared between SQLite and PostgreSQL providers, we can generate PostgreSQL migrations directly from the model: ```powershell # Navigate to PostgreSQL provider project cd E:\Projects\pgsql-jellyfin\src\Jellyfin.Database\Jellyfin.Database.Providers.Postgres # Generate the initial migration (creates all tables matching current DbContext) dotnet ef migrations add InitialCreate --context JellyfinDbContext --output-dir Migrations # This will create migration files matching the current schema ``` ## Step 2: Verify Generated Migration The generated migration should include all tables: - ActivityLogs - Users, Permissions, Preferences - Devices, DeviceOptions - DisplayPreferences, ItemDisplayPreferences, CustomItemDisplayPreferences - ImageInfos - TrickplayInfos - MediaSegments - BaseItems, AncestorIds, AttachmentStreamInfos - Chapters, ItemValues, ItemValuesMap - MediaStreamInfos, UserData - And more... ## Step 3: Apply Migration (Test) ```powershell # Test on a PostgreSQL database dotnet ef database update --context JellyfinDbContext --connection "Host=localhost;Database=jellyfin_test;Username=jellyfin;Password=test" ``` ## Alternative: Manual Migration Copying (Not Recommended) If you need to maintain separate migrations per provider (not typical for EF Core): 1. Copy SQLite migration structure 2. Replace SQLite-specific code with PostgreSQL equivalents: - `.Annotation("Sqlite:Autoincrement", true)` → `.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)` - `type: "TEXT"` → `type: "text"` or appropriate PostgreSQL type - `type: "INTEGER"` → `type: "integer"` or `type: "bigint"` - `type: "REAL"` → `type: "real"` or `type: "double precision"` ## Why Single Migration is Better EF Core migrations are **provider-agnostic** at the model level. The same migration can target different databases: - The migration C# code uses `MigrationBuilder` API (database-agnostic) - EF Core generates database-specific SQL at runtime - One migration file works for SQLite, PostgreSQL, MySQL, etc. The difference is in the migration assembly location: - SQLite migrations: `Jellyfin.Database.Providers.Sqlite` assembly - PostgreSQL migrations: `Jellyfin.Database.Providers.Postgres` assembly Each provider has its own migration history. ## Recommended Approach **Use the InitialCreate migration** generated from the current DbContext model. This will: - ✅ Match the current schema exactly - ✅ Be PostgreSQL-optimized - ✅ Include all tables, indexes, and constraints - ✅ Work with the migration system - ✅ Be maintainable going forward ## Post-Generation Steps After generating the initial migration: 1. **Review the generated code** - ensure all tables are included 2. **Test on clean database** - verify schema creation works 3. **Compare with SQLite schema** - ensure parity 4. **Commit to source control** - save the migration files 5. **Document any PostgreSQL-specific customizations** ## Future Migrations For new schema changes: 1. Update the `JellyfinDbContext` model 2. Generate migration for BOTH providers: ```powershell # SQLite cd src\Jellyfin.Database\Jellyfin.Database.Providers.Sqlite dotnet ef migrations add YourMigrationName --context JellyfinDbContext # PostgreSQL cd ..\Jellyfin.Database.Providers.Postgres dotnet ef migrations add YourMigrationName --context JellyfinDbContext ``` ## Execution To execute this script: ```powershell # Run the generation command .\Generate-PostgreSQLMigrations.ps1 ```