a3eb4b1b57
- Added Jellyfin.Database.Providers.Postgres project with full EF Core migration support for PostgreSQL. - Implemented automatic database creation and privilege assignment on startup. - Generated initial migration and model snapshot for PostgreSQL schema. - Updated build, test, and dependency files to include PostgreSQL provider and Npgsql packages. - Added PowerShell script for generating and testing PostgreSQL migrations. - No changes to application logic outside database provider/migration infrastructure.
3.8 KiB
3.8 KiB
Generate PostgreSQL Migrations from SQLite
This script generates PostgreSQL migrations that match the SQLite migration schema.
Prerequisites
- .NET SDK installed
dotnet-eftools 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:
# 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)
# 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):
- Copy SQLite migration structure
- Replace SQLite-specific code with PostgreSQL equivalents:
.Annotation("Sqlite:Autoincrement", true)→.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)type: "TEXT"→type: "text"or appropriate PostgreSQL typetype: "INTEGER"→type: "integer"ortype: "bigint"type: "REAL"→type: "real"ortype: "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
MigrationBuilderAPI (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.Sqliteassembly - PostgreSQL migrations:
Jellyfin.Database.Providers.Postgresassembly
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:
- Review the generated code - ensure all tables are included
- Test on clean database - verify schema creation works
- Compare with SQLite schema - ensure parity
- Commit to source control - save the migration files
- Document any PostgreSQL-specific customizations
Future Migrations
For new schema changes:
- Update the
JellyfinDbContextmodel - Generate migration for BOTH providers:
# 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:
# Run the generation command
.\Generate-PostgreSQLMigrations.ps1