fix: prevent data loose on track_types_id migration (#1949)

This commit is contained in:
Jonas L 2022-07-07 23:35:27 +02:00 committed by GitHub
parent 64f2f8fc68
commit d323657047
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -10,15 +10,18 @@ ALTER TABLE "cc_files" ADD CONSTRAINT "cc_files_track_type_fkey"
FOREIGN KEY ("track_type_id")
REFERENCES "cc_track_types" ("id");
UPDATE "cc_files" previous SET "track_type_id" = (
UPDATE "cc_files" SET "track_type" = NULL
WHERE "track_type" = '';
UPDATE "cc_files" file SET "track_type_id" = (
SELECT "id" FROM "cc_track_types"
WHERE "code" = previous."track_type"
WHERE "code" = file."track_type"
)
WHERE "track_type" IS NOT NULL;
UPDATE "cc_pref" previous SET "valstr" = (
UPDATE "cc_pref" file SET "valstr" = (
SELECT "id" FROM "cc_track_types"
WHERE "code" = previous."valstr"
WHERE "code" = file."valstr"
)
WHERE "keystr" = 'tracktype_default'
AND "valstr" <> '';
@ -27,17 +30,23 @@ ALTER TABLE "cc_files" DROP COLUMN IF EXISTS "track_type";
"""
DOWN = """
ALTER TABLE "cc_files" DROP CONSTRAINT "cc_files_track_type_fkey";
ALTER TABLE "cc_files" DROP COLUMN IF EXISTS "track_type_id";
ALTER TABLE "cc_files" ADD COLUMN "track_type" VARCHAR(16);
UPDATE "cc_pref" previous SET "valstr" = (
UPDATE "cc_files" file SET "track_type" = (
SELECT "code" FROM "cc_track_types"
WHERE "id" = previous."valstr"::int
WHERE "id" = file."track_type_id"
)
WHERE "track_type_id" IS NOT NULL;
UPDATE "cc_pref" pref SET "valstr" = (
SELECT "code" FROM "cc_track_types"
WHERE "id" = pref."valstr"::int
)
WHERE "keystr" = 'tracktype_default'
AND "valstr" <> '';
ALTER TABLE "cc_files" DROP CONSTRAINT "cc_files_track_type_fkey";
ALTER TABLE "cc_files" DROP COLUMN IF EXISTS "track_type_id";
"""

View File

@ -33,13 +33,15 @@ Please run this **before the upgrade procedure**!
The database files track type field was previously not constrained and this might have lead to files referencing a now renamed or missing track type. To preserve as much data as possible during the database migration process, you need to check whether some files have broken or missing track type references and fix them accordingly. To list broken track type references, you can run the following command:
```bash
sudo -u www-data libretime-api dbshell --command="
sudo -u libretime libretime-api dbshell --command="
SELECT f.id, f.track_type, f.track_title, f.artist_name, f.filepath
FROM cc_files f
WHERE NOT EXISTS (
SELECT FROM cc_track_types tt
WHERE tt.code = f.track_type
);"
)
AND f.track_type IS NOT NULL
AND f.track_type <> '';"
```
If the above command outputs the following, no file needs fixing.