HangfireIO / HangfireIO/Hangfire
Install.sql script fails due to CREATE TABLE statements when tables exist
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
### Background/Context
We've been using Hangfire for some time now (~1yr) running against a development environment. We're on the tail end of the project and I've been working on our Release pipeline and adding support to it for releasing the database artifacts.
Previously, we've allowed **Hangfire.SqlServer** to manage and deploy the schema and any updates but we're moving away from that now that we're building out the release pipeline to handle EntityFramework migrations, etc.
### Issue
Locally I purged my database tables and re-ran my scripts and everything was fine. Hangfire and my other SQL scripts all apply fine and can be ran a 2nd/subsequent time without failure.
Once I tried to run the scripts in the Release pipeline, the Hangfire script (_**Install.sql**_) failed due to trying to create the Jobs table which already existed.
### Analysis
Knowing that I could drop the tables & recreate them from **_Install.sql_** and then run the file again without error, I inspected the development database where it was failing and found that the **Hangfire.Schema** table existed -but- it did not contain any rows.
Inspecting the **_Install.sql_** script I see that there are checks for a schema version and my local database has a row (_with the value of 7_) present but the development database did not have ANY rows at all.
It appears that the check for a 'heavy migration' doesn't handle this case.
With dropping all the tables in the development database and re-running my release pipeline it worked -and- this time it has a row with the same value of 7 present. After that, 2nd and subsequent executions of **_Install.sql_** work fine.
### Retro
- It seems like the schema management provided by **Hangfire.SqlServer** when using **_PrepareSchema=true_** doesn't ensure the **Hangfire.Schema** table is being populated (or updated) correctly.
- The **_Install.sql_** script likely needs some type of 'hardening' to this condition.
Contributor guide
Research direction
Start by inspecting Install.sql and the schema-management path used when PrepareSchema=true, focusing on the heavy-migration check involving Hangfire.Schema. Reproduce the reported state where Hangfire.Schema exists but has no rows, then verify the script can run against existing tables without failure. Done means repeated execution succeeds and the schema version is populated or updated correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100