Scott McGrath 82d5af2dfb
fix(legacy): migrations from airtime 2.5.1 (#3123)
### Description

This fixes various problems in legacy migrations that were preventing a
successful database migration from Airtime 2.5.1. Previously, following
[the
procedure](https://libretime.org/docs/admin-manual/install/migrate-from-airtime/)
using the migrations provided in the Libretime 4.2.0 installer, without
the fixes in this PR, would either fail completely, or cause all of the
imported data to be completely deleted.


_migrations.py
If schema_version is not found in the table cc_prefs, it then checks for
system_version to have a value of '2.5.1' (in case this is an airtime
2.5.1 migration which will not have any schema_version in cc_pref). If
found, it prevents loading of the schema file, which is critical to
preserving the imported Airtime data.

0006_2_5_5
Removed a redundant addition of the image_path and description columns
to cc_show (done in earlier migration 003_2_5_2)

0015_2_5_17
Fixed a syntax error with adding the artwork column to cc_files

0023_3_0_0_alpha_9_1
Removed a redundant addition of the artwork column to cc_files (done in
earlier migration 0015_2_5_7)

### Documentation Changes

The [airtime migration
documentation](https://libretime.org/docs/admin-manual/install/migrate-from-airtime/)
already suggests a procedure to be followed, it just didn't work because
of problems within these migrations. The procedure as documented should
now work for those coming from Airtime 2.5.1.

### Testing Notes

**What I did:**

I attempted to migrate an actual airtime 2.5.1 database from a
production system containing a large amount of shows, tracks, and users.
I observed that the migration completed without errors, and that the
expected system state was achieved within Libretime. Specifically, the
calendar, library, authentication, and other aspects are populated with
the data that was present in the migrated Airtime database, and
Libretime is able to function using this data.

**How you can replicate my testing:**

Install Libretime 4.2.0. Restore a sample postrgresql database backup
from an Airtime 2.5.1 server. Apply the database migration. Restart the
services. Login and view the library, calender, etc.

### **Links**

Closes: #3121  
May also be related to, or even close (as a duplicate): #2563
2024-12-21 10:09:19 +00:00

91 lines
2.4 KiB
Python

from typing import Callable, Optional
from django.db import DataError, connection
from ._version import parse_version
def get_schema_version():
"""
Get the schema version from a legacy database.
Don't use django models as they might break in the future. Our concern is to upgrade
the legacy database schema to the point where django is in charge of the migrations.
An airtime 2.5.1 migration will not have schema_version, in that case, we look for
system_version to have a value of 2.5.1 and return that as the schema version value
(really just needs to be anything besides None, so that the next migration doesn't overwrite
the database)
"""
if "cc_pref" not in connection.introspection.table_names():
return None
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT valstr AS version
FROM cc_pref
WHERE (keystr = 'schema_version') OR (keystr = 'system_version' AND valstr = '2.5.1')
"""
)
row = cursor.fetchone()
if row and row[0]:
return row[0]
return None
def set_schema_version(cursor, version: str):
cursor.execute(
"""
UPDATE cc_pref
SET valstr = %s
WHERE keystr = 'schema_version';
""",
[version],
)
if not cursor.rowcount:
cursor.execute(
"""
INSERT INTO cc_pref (keystr, valstr)
VALUES ('schema_version', %s);
""",
[version],
)
def legacy_migration_factory(
target: str,
before: Optional[Callable] = None,
sql: Optional[str] = None,
after: Optional[Callable] = None,
reverse: bool = False,
):
target_version = parse_version(target)
def inner(_apps, _schema_editor):
current = get_schema_version()
if current is None:
raise DataError("current schema version was not found!")
current_version = parse_version(current)
if current_version >= target_version and not reverse:
return
if current_version < target_version and reverse:
return
with connection.cursor() as cursor:
if before is not None:
before(cursor)
if sql is not None:
cursor.execute(sql)
if after is not None:
after(cursor)
set_schema_version(cursor, version=target)
return inner