Upgrading an Ubuntu Linux 4.0.1 installation to 4.0.2

4.0.2 is a patch release that does run database migrations, unlike 4.0.1. On Ubuntu Linux the upgrade is a backup, a code update, a dependency sync, the migrations and a restart:

  1. Stop the background services

  2. Back up the database, and confirm the backup opens

  3. git fetch and git pull

  4. uv sync with your database’s extra

  5. python manage.py check --database default

  6. python manage.py migrate and python manage.py collectstatic

  7. Start the services again

You never name a version. The installation guide checks out the releases/4.0 branch rather than a version tag, so the branch carries each patch as it is released and a git pull is all that moves you between them.

These steps apply to any 4.0.x patch, not only this one. What changes between patches is in the next section.

Note

Run these commands as the account that owns the installation - the qatrack user in this guide’s examples. Every command below uses ~/web/qatrackplus, which only resolves for that account, and the pg_dump in the backup step authenticates by being it. If you are at a root shell, sudo -u qatrack -i gets you there; the account still needs to be able to sudo, because stopping and starting the services does.

Before you start

Back up your database and your uploads, and check the backup opens. That is the same thing you would do before any QATrack+ upgrade.

If you have edited any files inside qatrack/, read Upgrading an installation you have modified at the end of this page before you pull: it asks you to record what you changed while that is still a one-line command.

Note

4.0.2 runs migrations, the way most QATrack+ releases have; 4.0.1 was the unusual one in having none. Nothing here deletes data: the migrations record field descriptions the models have declared for several releases, and restore unique constraints that SQL Server lost during the 4.0 upgrade.

Backing up QATrack+ says what a backup set has to contain, and - if your production database is SQLite - how to take one that is actually readable, which a file copy of a live database is not.

What you will see. migrate prints one line per migration. Upgrading 4.0.1 to 4.0.2 applies eight, one for most applications plus one that creates the cache table:

Applying attachments.0011_v4_0_2_final... OK
Applying faults.0016_v4_0_2_final... OK
Applying parts.0019_v4_0_2_final... OK
Applying qa.0061_v4_0_2_final... OK
Applying qatrack_core.0002_cache_table... OK
Applying reports.0011_v4_0_2_final... OK
Applying service_log.0029_v4_0_2_final... OK
Applying units.0022_v4_0_2_final... OK

migrate applies them in dependency order rather than the alphabetical order listed above - parts first, service_log last on most sites - so the eight lines appear in a different sequence to the one here. It also prints a handful of lines that are not migrations at all: “… next run updated to …” for each scheduled task it already had, and one “qatrack.reports.tasks.run_reports schedule created”. Those are django-q2 re-reading its schedule, not something the upgrade switched on.

Between them the eight carry 194 field alterations, two model-options changes and six data steps. The field alterations are the bulk of it and are the least interesting part: they bring the recorded schema into line with what the models have declared for several releases, so most make no change to your tables at all. The qa one is the largest at 132.

Three of those lines are worth recognising:

  • The SQL Server unique-constraint repair runs in four of them - parts.0019, qa.0061, service_log.0029 and units.0022. Each restores the unique indexes that engine lost for its own application’s tables, so on SQL Server these are the four lines that matter. qa.0061 is the slowest, carrying 132 of the 194 field alterations as well as its share of the repair. Of the remaining four, three are field state only; qatrack_core.0002_cache_table is the exception and is described next.

  • The cache table migration, qatrack_core.0002_cache_table, creates the cache table. Until 4.0.2 that table was created by manage.py createcachetable and by nothing else, so a database that had been migrated but never had that command run would fail at runtime. This migration removes that gap; if you have already run createcachetable, it finds the table and does nothing.

  • The permission rename, service_log.0029_v4_0_2_final, corrects one label. In 4.0.x both “can view service event” and “can review service event” were labelled Can review service event, so the admin’s permission list showed the same words twice for two different rights and there was no way to tell them apart when assigning them. The view permission now reads Can view service event.

    This is the one change in the upgrade an administrator will see. If you had already corrected the wording yourself, the migration leaves your version alone - it only replaces the exact 4.0.x text.

    Check your groups after upgrading if you assigned either permission by picking it from that list, because until now the two were indistinguishable there. review_serviceevent is the one that also allows changing a service event’s status.

If migrate applies more than these eight, the most likely reason is simply that the site was on 4.0.0 rather than 4.0.1, so it is catching up on 4.0.1’s migrations too - see the 4.0.0 upgrade guide for what those are. Worth a look either way, since it tells you where the database actually started.

Note

These eight are the whole 4.0.1 to 4.0.2 step. A fresh installation runs the full history instead - several hundred migrations, and several minutes - which is normal, and not what an upgrading site sees.

If you are on SQL Server, read this one

The 4.0 migrations silently dropped unique indexes on SQL Server - mssql-django drops them when an unrelated field change retypes a primary key, and never puts them back. Your database has been accepting duplicate rows that other backends would reject, including on TestListInstance.user_key, whose job is to keep API submissions unique.

4.0.2 restores them. You do not have to do anything, but two things are worth knowing:

  • migrate prints a warning for each missing constraint (qatrack.W014) before it applies anything, and then the same migrate repairs them. The warnings are not telling you to act.

    To see them without migrating, the command is python manage.py check --database default. Plain manage.py check will not show them: this check has to ask the database what it enforces, so it is registered to run only when a connection is in play, and check without --database reports “System check identified no issues” on a database that is missing all sixteen.

  • Rows that already violate a constraint block its repair. Run manage.py check_unique_constraints first to see whether you have any. A constraint that cannot be created is reported and skipped; the upgrade still finishes.

Dates and times will look different, and that is this release

Dates now display as YYYY-MM-DD by default, in every language, and the date pickers write back the same format they show.

This is the fix for a released bug: 4.0 wrote a date into a form in one format and read it back in another, so a pre-filled date was silently replaced with an unrelated one - usually months away and at midnight - before anyone had touched the page.

If your site needs its previous appearance, set QATRACK_DATE_FORMAT and QATRACK_DATETIME_FORMAT in local_settings.py. The release notes give the values that reproduce 4.0’s display.

Running the upgrade

Stop the services first, so nothing runs mid-upgrade:

sudo supervisorctl stop gunicorn django-q2
sudo service nginx stop

Warning

gunicorn matters as much as django-q2 here. Supervisor runs it as its own program, so stopping nginx does not stop it: it keeps its Python already imported while you replace the dependencies and the templates underneath it. What you get is old code serving new templates and new static files, which fails in ways that look nothing like an upgrade problem — and this release changes the date formats every form renders, so the symptom is dates that do not match the release notes.

Back up the database now, with the services stopped, and confirm the backup is readable before you go any further — see Backing up QATrack+:

pg_dump -U qatrack -Fc qatrackplus > ~/qatrackplus-pre-4.0.2.dump
pg_restore --file=/dev/null ~/qatrackplus-pre-4.0.2.dump && echo "backup reads OK"

Note

Neither backup command asks for a password, and that is why the account matters. pg_dump -U qatrack connects over the local socket, where PostgreSQL matches the operating-system user to the database role of the same name - so it works because you are qatrack. Run it from any other account, root included, and it is refused. From elsewhere, either switch user first, or add -h 127.0.0.1 and give the password from local_settings.py, which is what the MySQL line below does with -p.

On MySQL, the equivalent. --single-transaction keeps the dump consistent, and the last line a complete mysqldump writes is -- Dump completed, so its absence is how you tell a truncated file from a finished one:

mysqldump -u qatrack -p --single-transaction --routines qatrackplus \
    > ~/qatrackplus-pre-4.0.2.sql
tail -1 ~/qatrackplus-pre-4.0.2.sql | grep -q 'Dump completed' && echo "backup reads OK"

Warning

If this site has backup sets from before 4.0.1, do not rely on them here. manage.py backup_site skipped the database silently on MySQL and still reported success, so an older set can hold media and settings and no database at all - see Backing up QATrack+. Take the dump above instead.

Back up the uploaded files too. They are not in the database, and a dump plus a copy of local_settings.py is not a complete set without them:

tar -czf ~/qatrackplus-media-pre-4.0.2.tar.gz -C ~/web/qatrackplus qatrack/media

manage.py backup_site archives the same folder on every engine if you would rather use it, but check where it put the file before you trust it: BACKUP_DIR is a Windows path in the shipped settings, so set it in local_settings.py on a Linux server. Backing up QATrack+ says what a complete set contains.

Fetch the new version:

cd ~/web/qatrackplus
git fetch origin
git pull

Note

If git pull reports that you are not on a branch, you are on a tag from an earlier install. git checkout releases/4.0 once, and every upgrade after that is the pull above.

Update the Python environment. Use the extra that matches your database:

cd ~/web/qatrackplus
source .venv/bin/activate
uv sync --extra postgres --no-dev

--no-dev matters as much here as it does at installation, and for the same reason: without it uv installs the dev group onto your server as well - around 200 packages including pytest, selenium, sphinx and django-debug-toolbar, none of which serve anything. Leave it off only if you intend to run the test suite on this machine, as described at the end of this page.

For MySQL
cd ~/web/qatrackplus
uv sync --extra mysql --no-dev

Check the installation before touching the database. --database default runs the checks that need a connection as well as the ones that do not; on PostgreSQL and MySQL they find nothing, because the constraint loss this release repairs only happened on SQL Server:

python manage.py check --database default

Then run the migrations and collect the static files. This release has migrations, so migrate will report work done. collectstatic is still required - this release changes JavaScript and templates - though it will usually report only a file or two copied, which is normal and not a sign it did nothing:

python manage.py migrate
python manage.py collectstatic --noinput

--noinput answers the “This will overwrite existing files! Are you sure you want to do this?” prompt that collectstatic otherwise stops at and waits for. Leave it off if you would rather answer it yourself - but a script that pipes the output will hide the question while still waiting for it.

Restart — the application servers first, so nginx has something to proxy to the moment it comes back:

sudo supervisorctl start gunicorn django-q2
sudo service nginx start

Confirm both are running, and that the site itself answers, before you call the upgrade done. supervisorctl reporting RUNNING means the processes started, not that a request reaches them:

sudo supervisorctl status
curl -I http://localhost/

Expect 200 or a redirect to your login page. A 400 means the hostname you asked for is not in ALLOWED_HOSTS - a settings answer, not a failed upgrade - so try your site’s own name. Anything from nginx about a bad gateway means gunicorn did not come back; logs/gunicorn.log says why.

After upgrading

Check a date on a form. Open a form that has a pre-filled date and confirm it shows the date you expect. That is the fix this release exists for, and it is visible in ten seconds. The quickest two:

  • a new fault, at /faults/create/, whose Occurred field pre-fills with now;

  • an existing service event, at /servicelog/event/edit/<id>/, which shows a date that was stored before the upgrade rather than today’s.

Both should read YYYY-MM-DD, and the service event’s time should be the local time you recorded it at, not an offset from it.

On SQL Server, confirm the constraints came back:

python manage.py check --database default

should now report nothing. The --database is not optional here - plain manage.py check cannot see these warnings at all, and will say there are no issues whether or not every constraint is missing.

If it still reports qatrack.W014 for a table, that constraint was skipped because of duplicate rows. manage.py check_unique_constraints names them, and once they are resolved the repair can be run again by reversing the migration for that app and applying it forward:

python manage.py migrate parts 0018_v4_0_final
python manage.py migrate

Reversing removes the constraints that app’s migration added and applying it forward puts back every one it can - including the one that was skipped. A plain migrate on its own will not do it, because the migration is already recorded as applied.

Confirm your backups once more - if you act on one thing after this release, make it reading the output of your next backup run rather than its exit status.

Running the test suite against an installed server

You do not need to, and most sites never will. It is worth knowing how, because “the suite passes on my server” is the most direct answer to did this upgrade land cleanly, and two things stop it working on a deployment that was set up by following this guide.

The database user cannot create the test database. pytest reads your local_settings.py, so it talks to your real database server, and then asks it for a second, temporary database to run against. The user this guide creates owns the QATrack+ database and nothing else, so that request is refused:

# PostgreSQL
permission denied to create database
# SQL Server
CREATE DATABASE permission denied in database 'master'. (262)

Grant it for the run and take it away afterwards, rather than leaving it in place:

# PostgreSQL
sudo -u postgres psql -c "alter role qatrack createdb"
# ... run the suite ...
sudo -u postgres psql -c "alter role qatrack nocreatedb"
-- SQL Server
ALTER SERVER ROLE [dbcreator] ADD MEMBER [qatrack];
-- ... run the suite ...
ALTER SERVER ROLE [dbcreator] DROP MEMBER [qatrack];

The GUI tests need a browser. Around 35 of them drive a real one, and on a server with none installed they error at setup rather than being skipped. That is not a failure of your installation. Deselect them by marker - ignoring the file they mostly live in is not enough, because other test modules import its base class and define browser-driven tests of their own:

.venv/bin/python -m pytest -m "not selenium"

On Windows that is .venv\Scripts\python.exe -m pytest -m "not selenium", and the grant above is the SQL Server one.

Warning

Your own settings are in force while the suite runs, because pytest loads qatrack.settings and therefore your local_settings.py. A site with local modifications can see large numbers of failures that say nothing about QATrack+ - on the public demo server, a local restriction on calculated tests failed 140 of them, all for the same reason.

If that happens, put the differing settings in a module of their own and point the run at it, rather than editing the settings your site is serving with:

# qatrack/my_test_settings.py
from qatrack.settings import *  # noqa

SOME_LOCAL_RESTRICTION = False
.venv/bin/python -m pytest --ds=qatrack.my_test_settings

Upgrading an installation you have modified

If you have edited files inside qatrack/, check whether the upgrade touches the same ones before you pull. git status lists what you changed; keep a copy of the diff somewhere outside the checkout, because an upgrade that edits the same file will make you re-apply it by hand rather than merging it for you.

If you have already pulled, nothing is lost. git pull refuses to overwrite a file you have modified, so either it completed and your edits are still there, or it stopped and told you which files it would have overwritten. git status and git diff still answer the same question afterwards as before.

4.0.2 changes qatrack/qatrack_core/apps.py, which is a common place for sites to add start-up code of their own.