From 0e4bc4cacdb51851a74bafbf099d019a31c780c4 Mon Sep 17 00:00:00 2001 From: jo Date: Sat, 28 May 2022 16:27:57 +0200 Subject: [PATCH] feat: replace php migration with django migration - keep latest legacy version in initial migration file - move propel schema to api legacy app - remove legacy upgrade tool --- api/libretime_api/legacy/__init__.py | 0 api/libretime_api/legacy/apps.py | 7 + .../legacy/migrations/0001_initial.py | 29 + .../legacy/migrations/0003_2_5_2.py | 28 + .../legacy/migrations/0004_2_5_3.py | 28 + .../legacy/migrations/0005_2_5_4.py | 61 ++ .../legacy/migrations/0006_2_5_5.py | 27 + .../legacy/migrations/0007_2_5_9.py | 33 + .../legacy/migrations/0008_2_5_10.py | 24 + .../legacy/migrations/0009_2_5_11.py | 33 + .../legacy/migrations/0010_2_5_12.py | 24 + .../legacy/migrations/0011_2_5_13.py | 37 + .../legacy/migrations/0012_2_5_14.py | 28 + .../legacy/migrations/0013_2_5_15.py | 30 + .../legacy/migrations/0014_2_5_16.py | 35 + .../legacy/migrations/0015_2_5_17.py | 23 + .../legacy/migrations/0016_3_0_0_alpha.py | 29 + .../legacy/migrations/0017_3_0_0_alpha_1.py | 31 + .../legacy/migrations/0018_3_0_0_alpha_6.py | 25 + .../legacy/migrations/0019_3_0_0_alpha_7.py | 29 + .../legacy/migrations/0020_3_0_0_alpha_7_1.py | 30 + .../legacy/migrations/0021_3_0_0_alpha_7_2.py | 28 + .../legacy/migrations/0022_3_0_0_alpha_7_3.py | 25 + .../legacy/migrations/0023_3_0_0_alpha_9_1.py | 25 + .../legacy/migrations/0024_3_0_0_alpha_9_2.py | 28 +- .../legacy/migrations/0025_3_0_0_alpha_9_3.py | 34 + .../legacy/migrations/0026_3_0_0_alpha_9_4.py | 33 + .../legacy/migrations/__init__.py | 1 + .../legacy/migrations/_migrations.py | 77 ++ .../legacy/migrations/_version.py | 38 + .../legacy/migrations/sql/data.sql | 3 - .../legacy/migrations}/sql/schema.sql | 0 .../legacy/migrations/sql/sqldb.map | 2 + api/libretime_api/legacy/tests/__init__.py | 0 .../legacy/tests/migrations/__init__.py | 0 .../legacy/tests/migrations/test_version.py | 44 ++ api/libretime_api/settings/_internal.py | 1 + api/setup.py | 3 + docs/developer-manual/README.md | 1 + .../design/database-migrations.md | 51 ++ legacy/application/common/TaskManager.php | 26 - .../controllers/UpgradeController.php | 63 -- .../airtime_2.5.13/downgrade.sql | 11 - .../airtime_2.5.15/downgrade.sql | 1 - .../airtime_2.5.16/downgrade.sql | 11 - .../airtime_3.0.0-alpha.1/downgrade.sql | 4 - .../airtime_3.0.0-alpha.6/downgrade.sql | 1 - .../airtime_3.0.0-alpha.7.1/downgrade.sql | 2 - .../airtime_3.0.0-alpha.7.2/downgrade.sql | 1 - .../airtime_3.0.0-alpha.7.3/downgrade.sql | 1 - .../airtime_3.0.0-alpha.7/downgrade.sql | 1 - .../airtime_3.0.0-alpha.9.1/downgrade.sql | 1 - .../airtime_3.0.0-alpha.9.2/downgrade.sql | 3 - .../airtime_3.0.0-alpha.9.3/downgrade.sql | 10 - .../airtime_3.0.0-alpha.9.4/downgrade.sql | 6 - .../airtime_3.0.0-alpha/downgrade.sql | 3 - .../plugins/PageLayoutInitPlugin.php | 8 +- .../upgrade_sql/airtime_2.5.10/upgrade.sql | 2 - .../upgrade_sql/airtime_2.5.12/upgrade.sql | 2 - .../upgrade_sql/airtime_2.5.14/upgrade.sql | 3 - .../upgrade_sql/airtime_2.5.15/upgrade.sql | 1 - .../upgrade_sql/airtime_2.5.17/upgrade.sql | 1 - .../upgrade_sql/airtime_2.5.2/upgrade.sql | 6 - .../upgrade_sql/airtime_2.5.3/upgrade.sql | 6 - .../upgrade_sql/airtime_2.5.5/upgrade.sql | 5 - .../upgrade_sql/airtime_2.5.9/upgrade.sql | 11 - .../airtime_3.0.0-alpha.1/upgrade.sql | 4 - .../airtime_3.0.0-alpha.6/upgrade.sql | 1 - .../airtime_3.0.0-alpha.7.1/upgrade.sql | 2 - .../airtime_3.0.0-alpha.7.2/upgrade.sql | 1 - .../airtime_3.0.0-alpha.7.3/upgrade.sql | 1 - .../airtime_3.0.0-alpha.7/upgrade.sql | 1 - .../airtime_3.0.0-alpha.9.1/upgrade.sql | 1 - .../airtime_3.0.0-alpha.9.3/upgrade.sql | 1 - .../airtime_3.0.0-alpha.9.4/upgrade.sql | 5 - .../airtime_3.0.0-alpha/upgrade.sql | 3 - legacy/application/upgrade/Upgrades.php | 726 ------------------ legacy/build/build.properties | 3 +- legacy/build/sql/sequences.sql | 7 - legacy/build/sql/sqldb.map | 6 - legacy/build/sql/triggers.sql | 6 - legacy/build/sql/views.sql | 5 - legacy/public/setup/database-setup.php | 24 +- .../application/helpers/AirtimeInstall.php | 32 +- .../tests/application/helpers/TestHelper.php | 1 - 85 files changed, 1005 insertions(+), 1000 deletions(-) create mode 100644 api/libretime_api/legacy/__init__.py create mode 100644 api/libretime_api/legacy/apps.py create mode 100644 api/libretime_api/legacy/migrations/0001_initial.py create mode 100644 api/libretime_api/legacy/migrations/0003_2_5_2.py create mode 100644 api/libretime_api/legacy/migrations/0004_2_5_3.py create mode 100644 api/libretime_api/legacy/migrations/0005_2_5_4.py create mode 100644 api/libretime_api/legacy/migrations/0006_2_5_5.py create mode 100644 api/libretime_api/legacy/migrations/0007_2_5_9.py create mode 100644 api/libretime_api/legacy/migrations/0008_2_5_10.py create mode 100644 api/libretime_api/legacy/migrations/0009_2_5_11.py create mode 100644 api/libretime_api/legacy/migrations/0010_2_5_12.py rename legacy/application/controllers/upgrade_sql/airtime_2.5.13/upgrade.sql => api/libretime_api/legacy/migrations/0011_2_5_13.py (56%) create mode 100644 api/libretime_api/legacy/migrations/0012_2_5_14.py create mode 100644 api/libretime_api/legacy/migrations/0013_2_5_15.py rename legacy/application/controllers/upgrade_sql/airtime_2.5.16/upgrade.sql => api/libretime_api/legacy/migrations/0014_2_5_16.py (79%) create mode 100644 api/libretime_api/legacy/migrations/0015_2_5_17.py create mode 100644 api/libretime_api/legacy/migrations/0016_3_0_0_alpha.py create mode 100644 api/libretime_api/legacy/migrations/0017_3_0_0_alpha_1.py create mode 100644 api/libretime_api/legacy/migrations/0018_3_0_0_alpha_6.py create mode 100644 api/libretime_api/legacy/migrations/0019_3_0_0_alpha_7.py create mode 100644 api/libretime_api/legacy/migrations/0020_3_0_0_alpha_7_1.py create mode 100644 api/libretime_api/legacy/migrations/0021_3_0_0_alpha_7_2.py create mode 100644 api/libretime_api/legacy/migrations/0022_3_0_0_alpha_7_3.py create mode 100644 api/libretime_api/legacy/migrations/0023_3_0_0_alpha_9_1.py rename legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.2/upgrade.sql => api/libretime_api/legacy/migrations/0024_3_0_0_alpha_9_2.py (76%) create mode 100644 api/libretime_api/legacy/migrations/0025_3_0_0_alpha_9_3.py create mode 100644 api/libretime_api/legacy/migrations/0026_3_0_0_alpha_9_4.py create mode 100644 api/libretime_api/legacy/migrations/__init__.py create mode 100644 api/libretime_api/legacy/migrations/_migrations.py create mode 100644 api/libretime_api/legacy/migrations/_version.py rename legacy/build/sql/defaultdata.sql => api/libretime_api/legacy/migrations/sql/data.sql (99%) rename {legacy/build => api/libretime_api/legacy/migrations}/sql/schema.sql (100%) create mode 100644 api/libretime_api/legacy/migrations/sql/sqldb.map create mode 100644 api/libretime_api/legacy/tests/__init__.py create mode 100644 api/libretime_api/legacy/tests/migrations/__init__.py create mode 100644 api/libretime_api/legacy/tests/migrations/test_version.py create mode 100644 docs/developer-manual/design/database-migrations.md delete mode 100644 legacy/application/controllers/UpgradeController.php delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_2.5.13/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_2.5.15/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_2.5.16/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.1/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.6/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.1/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.2/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.3/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.1/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.2/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.3/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.4/downgrade.sql delete mode 100644 legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha/downgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.10/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.12/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.14/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.15/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.17/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.2/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.3/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.5/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_2.5.9/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.1/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.6/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.1/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.2/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.3/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.1/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.3/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.4/upgrade.sql delete mode 100644 legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha/upgrade.sql delete mode 100644 legacy/application/upgrade/Upgrades.php delete mode 100644 legacy/build/sql/sequences.sql delete mode 100644 legacy/build/sql/sqldb.map delete mode 100644 legacy/build/sql/triggers.sql delete mode 100644 legacy/build/sql/views.sql diff --git a/api/libretime_api/legacy/__init__.py b/api/libretime_api/legacy/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/libretime_api/legacy/apps.py b/api/libretime_api/legacy/apps.py new file mode 100644 index 000000000..39337817e --- /dev/null +++ b/api/libretime_api/legacy/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig + + +class LegacyConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "libretime_api.legacy" + verbose_name = "LibreTime Legacy" diff --git a/api/libretime_api/legacy/migrations/0001_initial.py b/api/libretime_api/legacy/migrations/0001_initial.py new file mode 100644 index 000000000..1757482b1 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0001_initial.py @@ -0,0 +1,29 @@ +from pathlib import Path + +from django.db import connection, migrations + +from . import LEGACY_SCHEMA_VERSION +from ._migrations import get_schema_version, set_schema_version + +here = Path(__file__).resolve().parent + + +def create_schema(_apps, _schema_editor): + schema_version = get_schema_version() + + # A schema already exists, don't overwrite ! + if schema_version is not None: + return + + with connection.cursor() as cursor: + for migration_filename in ("schema.sql", "data.sql"): + raw = (here / "sql" / migration_filename).read_text(encoding="utf-8") + cursor.execute(raw) + + set_schema_version(cursor, LEGACY_SCHEMA_VERSION) + + +class Migration(migrations.Migration): + initial = True + dependencies = [] + operations = [migrations.RunPython(create_schema)] diff --git a/api/libretime_api/legacy/migrations/0003_2_5_2.py b/api/libretime_api/legacy/migrations/0003_2_5_2.py new file mode 100644 index 000000000..3b3b7d1a0 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0003_2_5_2.py @@ -0,0 +1,28 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +-- Replacing system_version with schema_version +DELETE FROM cc_pref WHERE keystr = 'system_version'; +INSERT INTO cc_pref (keystr, valstr) VALUES ('schema_version', '2.5.2'); + +ALTER TABLE cc_show ADD COLUMN image_path varchar(255) DEFAULT ''; +ALTER TABLE cc_show_instances ADD COLUMN description varchar(255) DEFAULT ''; +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0001_initial"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.2", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0004_2_5_3.py b/api/libretime_api/legacy/migrations/0004_2_5_3.py new file mode 100644 index 000000000..84151bd25 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0004_2_5_3.py @@ -0,0 +1,28 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +-- DELETE FROM cc_pref WHERE keystr = 'system_version'; +-- INSERT INTO cc_pref (keystr, valstr) VALUES ('system_version', '2.5.3'); + +ALTER TABLE cc_files DROP COLUMN state; +ALTER TABLE cc_files ADD import_status integer default 1; -- Default is "pending" +UPDATE cc_files SET import_status=0; -- Existing files are already "imported" +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0003_2_5_2"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.3", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0005_2_5_4.py b/api/libretime_api/legacy/migrations/0005_2_5_4.py new file mode 100644 index 000000000..a55875890 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0005_2_5_4.py @@ -0,0 +1,61 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = None + +DOWN = None + + +def promote_admin_to_superadmin(cursor): + # Ensure there are no superadmins already + super_admin_count = cursor.execute( + """ + SELECT COUNT(id) + FROM cc_subjs + WHERE type = 'S' + AND login != 'sourcefabric_admin'; + """ + ).fetchone() + if super_admin_count != 0: + return + + # Promote the "admin" user to superadmin + cursor.execute( + """ + UPDATE cc_subjs SET type = 'S' + WHERE login = 'admin'; + """ + ) + if cursor.rowcount == 0: + # Otherwise promote the administrator with the lowest ID + cursor.execute( + """ + UPDATE cc_subjs SET type = 'S' + WHERE id = ( + SELECT id + FROM cc_subjs + WHERE type = 'A' + ORDER BY id + LIMIT 1 + ); + """ + ) + if cursor.rowcount == 0: + raise RuntimeError("Failed to find any users of type 'admin' ('A')") + + # Ignoring the sourcefabric_admin user + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0004_2_5_3"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.4", + before=promote_admin_to_superadmin, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0006_2_5_5.py b/api/libretime_api/legacy/migrations/0006_2_5_5.py new file mode 100644 index 000000000..af7ce07bd --- /dev/null +++ b/api/libretime_api/legacy/migrations/0006_2_5_5.py @@ -0,0 +1,27 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +-- DELETE FROM cc_pref WHERE keystr = 'system_version'; +-- INSERT INTO cc_pref (keystr, valstr) VALUES ('system_version', '2.5.5'); + +ALTER TABLE cc_show ADD COLUMN image_path varchar(255) DEFAULT ''; +ALTER TABLE cc_show_instances ADD COLUMN description varchar(255) DEFAULT ''; +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0005_2_5_4"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.5", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0007_2_5_9.py b/api/libretime_api/legacy/migrations/0007_2_5_9.py new file mode 100644 index 000000000..774498a33 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0007_2_5_9.py @@ -0,0 +1,33 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +CREATE TABLE cloud_file +( + id serial NOT NULL, + resource_id text NOT NULL, + storage_backend text NOT NULL, + cc_file_id integer NOT NULL, + CONSTRAINT cloud_file_pkey PRIMARY KEY (id), + CONSTRAINT "cloud_file_FK_1" FOREIGN KEY (cc_file_id) + REFERENCES cc_files (id) MATCH SIMPLE + ON UPDATE NO ACTION ON DELETE CASCADE +) +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0006_2_5_5"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.9", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0008_2_5_10.py b/api/libretime_api/legacy/migrations/0008_2_5_10.py new file mode 100644 index 000000000..c672822ac --- /dev/null +++ b/api/libretime_api/legacy/migrations/0008_2_5_10.py @@ -0,0 +1,24 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_files ADD COLUMN filesize integer NOT NULL +CONSTRAINT filesize_default DEFAULT 0 +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0007_2_5_9"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.10", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0009_2_5_11.py b/api/libretime_api/legacy/migrations/0009_2_5_11.py new file mode 100644 index 000000000..22b4d4526 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0009_2_5_11.py @@ -0,0 +1,33 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = None + +DOWN = None + + +def update_disk_usage(cursor): + cursor.execute( + """ + UPDATE cc_pref SET valstr = ( + SELECT SUM(filesize) + FROM cc_files + ) + WHERE keystr = 'disk_usage'; + """ + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0008_2_5_10"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.11", + sql=update_disk_usage, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0010_2_5_12.py b/api/libretime_api/legacy/migrations/0010_2_5_12.py new file mode 100644 index 000000000..f5f1f6d98 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0010_2_5_12.py @@ -0,0 +1,24 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_show ALTER COLUMN description TYPE varchar(8192); +ALTER TABLE cc_show_instances ALTER COLUMN description TYPE varchar(8192); +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0009_2_5_11"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.12", + sql=UP, + ) + ) + ] diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.13/upgrade.sql b/api/libretime_api/legacy/migrations/0011_2_5_13.py similarity index 56% rename from legacy/application/controllers/upgrade_sql/airtime_2.5.13/upgrade.sql rename to api/libretime_api/legacy/migrations/0011_2_5_13.py index 42ba8a8d0..c21b2bfd1 100644 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.13/upgrade.sql +++ b/api/libretime_api/legacy/migrations/0011_2_5_13.py @@ -1,3 +1,8 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ ----------------------------------------------------------------------- -- third_party_track_references ----------------------------------------------------------------------- @@ -40,3 +45,35 @@ ALTER TABLE "celery_tasks" ADD CONSTRAINT "celery_service_fkey" FOREIGN KEY ("track_reference") REFERENCES "third_party_track_references" ("id") ON DELETE CASCADE; +""" + +DOWN = """ +----------------------------------------------------------------------- +-- third_party_track_references +----------------------------------------------------------------------- +DROP TABLE IF EXISTS "third_party_track_references" CASCADE; + +----------------------------------------------------------------------- +-- celery_tasks +----------------------------------------------------------------------- +DROP TABLE IF EXISTS "celery_tasks" CASCADE; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0010_2_5_12"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.13", + sql=UP, + ), + reverse_code=legacy_migration_factory( + target="2.5.12", + sql=DOWN, + reverse=True, + ), + ) + ] diff --git a/api/libretime_api/legacy/migrations/0012_2_5_14.py b/api/libretime_api/legacy/migrations/0012_2_5_14.py new file mode 100644 index 000000000..dc6cabc1c --- /dev/null +++ b/api/libretime_api/legacy/migrations/0012_2_5_14.py @@ -0,0 +1,28 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +# SAAS-923 +# Add a partial constraint to cc_pref so that keystrings must be unique + +UP = """ +ALTER TABLE cc_pref ALTER COLUMN subjid SET DEFAULT NULL; +CREATE UNIQUE INDEX cc_pref_key_idx ON cc_pref (keystr) WHERE subjid IS NULL; +ANALYZE cc_pref; +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0011_2_5_13"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.14", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0013_2_5_15.py b/api/libretime_api/legacy/migrations/0013_2_5_15.py new file mode 100644 index 000000000..395718677 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0013_2_5_15.py @@ -0,0 +1,30 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +# SAAS-1071 +# Remove not null constraint from file_id fk in third_party_track_references +# so that we can create track references for downloads (which won't have a +# file ID until the task is run and the file is POSTed back to Airtime) + +UP = """ +ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP NOT NULL; +""" + +DOWN = """ +ALTER TABLE third_party_track_references ALTER COLUMN file_id SET NOT NULL; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0012_2_5_14"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.15", + sql=UP, + ) + ) + ] diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.16/upgrade.sql b/api/libretime_api/legacy/migrations/0014_2_5_16.py similarity index 79% rename from legacy/application/controllers/upgrade_sql/airtime_2.5.16/upgrade.sql rename to api/libretime_api/legacy/migrations/0014_2_5_16.py index ff1806ca6..2a845b8ff 100644 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.16/upgrade.sql +++ b/api/libretime_api/legacy/migrations/0014_2_5_16.py @@ -1,3 +1,8 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ ALTER TABLE cc_files ADD COLUMN description VARCHAR(512); ----------------------------------------------------------------------- @@ -88,3 +93,33 @@ ALTER TABLE "podcast_episodes" ADD CONSTRAINT "podcast_episodes_podcast_id_fkey" FOREIGN KEY ("podcast_id") REFERENCES "podcast" ("id") ON DELETE CASCADE; + +""" + +DOWN = """ +ALTER TABLE cc_files DROP COLUMN description; + +DELETE FROM cc_pref WHERE keystr = 'station_podcast_id'; + +DROP TABLE IF EXISTS "podcast" CASCADE; + +DROP TABLE IF EXISTS "imported_podcast" CASCADE; + +DROP TABLE IF EXISTS "station_podcast" CASCADE; + +DROP TABLE IF EXISTS "podcast_episodes" CASCADE; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0013_2_5_15"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.16", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0015_2_5_17.py b/api/libretime_api/legacy/migrations/0015_2_5_17.py new file mode 100644 index 000000000..bb26f7536 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0015_2_5_17.py @@ -0,0 +1,23 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_files ADD COLUMN artwork TYPE character varying(255); +""" + +DOWN = None + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0014_2_5_16"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="2.5.17", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0016_3_0_0_alpha.py b/api/libretime_api/legacy/migrations/0016_3_0_0_alpha.py new file mode 100644 index 000000000..bc9fe742d --- /dev/null +++ b/api/libretime_api/legacy/migrations/0016_3_0_0_alpha.py @@ -0,0 +1,29 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_show ADD COLUMN has_autoplaylist boolean default 'f' NOT NULL; +ALTER TABLE cc_show ADD COLUMN autoplaylist_id integer DEFAULT NULL; +ALTER TABLE cc_show_instances ADD COLUMN autoplaylist_built boolean default 'f' NOT NULL; +""" + +DOWN = """ +ALTER TABLE cc_show_instances DROP COLUMN IF EXISTS autoplaylist_built; +ALTER TABLE cc_show DROP COLUMN IF EXISTS has_autoplaylist; +ALTER TABLE cc_show DROP COLUMN IF EXISTS autoplaylist_id; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0015_2_5_17"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0017_3_0_0_alpha_1.py b/api/libretime_api/legacy/migrations/0017_3_0_0_alpha_1.py new file mode 100644 index 000000000..6e5022140 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0017_3_0_0_alpha_1.py @@ -0,0 +1,31 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE imported_podcast ADD COLUMN album_override boolean default 'f' NOT NULL; +ALTER TABLE third_party_track_references ALTER COLUMN file_id SET DEFAULT 0; +ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP NOT NULL; +ALTER TABLE cc_show ADD COLUMN autoplaylist_repeat boolean default 'f' NOT NULL; +""" + +DOWN = """ +ALTER TABLE imported_podcast DROP COLUMN IF EXISTS album_override; +ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP DEFAULT; +ALTER TABLE third_party_track_references ALTER COLUMN file_id SET NOT NULL; +ALTER TABLE cc_show DROP COLUMN IF EXISTS autoplaylist_repeat; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0016_3_0_0_alpha"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.1", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0018_3_0_0_alpha_6.py b/api/libretime_api/legacy/migrations/0018_3_0_0_alpha_6.py new file mode 100644 index 000000000..6a7a13f26 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0018_3_0_0_alpha_6.py @@ -0,0 +1,25 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_service_register ALTER COLUMN ip TYPE character varying(45); +""" + +DOWN = """ +ALTER TABLE cc_service_register ALTER COLUMN ip TYPE character varying(18); +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0017_3_0_0_alpha_1"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.6", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0019_3_0_0_alpha_7.py b/api/libretime_api/legacy/migrations/0019_3_0_0_alpha_7.py new file mode 100644 index 000000000..b5d1bb8de --- /dev/null +++ b/api/libretime_api/legacy/migrations/0019_3_0_0_alpha_7.py @@ -0,0 +1,29 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +# https://github.com/libretime/libretime/pull/636 +# Change dynamic smartblock to be default smartblock type + + +UP = """ +ALTER TABLE cc_block ALTER COLUMN type SET DEFAULT 'dynamic'; +""" + +DOWN = """ +ALTER TABLE cc_block ALTER COLUMN type SET DEFAULT 'static'; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0018_3_0_0_alpha_6"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.7", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0020_3_0_0_alpha_7_1.py b/api/libretime_api/legacy/migrations/0020_3_0_0_alpha_7_1.py new file mode 100644 index 000000000..7491d5d27 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0020_3_0_0_alpha_7_1.py @@ -0,0 +1,30 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +# https://github.com/libretime/libretime/pull/659 +# Add description and title to podcast episodes database table + +UP = """ +ALTER TABLE podcast_episodes ADD COLUMN episode_title VARCHAR(4096); +ALTER TABLE podcast_episodes ADD COLUMN episode_description VARCHAR(4096); +""" + +DOWN = """ +ALTER TABLE podcast_episodes DROP COLUMN IF EXISTS episode_title; +ALTER TABLE podcast_episodes DROP COLUMN IF EXISTS episode_description; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0019_3_0_0_alpha_7"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.7.1", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0021_3_0_0_alpha_7_2.py b/api/libretime_api/legacy/migrations/0021_3_0_0_alpha_7_2.py new file mode 100644 index 000000000..7f8272b88 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0021_3_0_0_alpha_7_2.py @@ -0,0 +1,28 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +# https://github.com/libretime/libretime/pull/704 +# Add criteria group to smartblock table to enable database to store separately + +UP = """ +ALTER TABLE cc_blockcriteria ADD COLUMN criteriagroup integer; +""" + +DOWN = """ +ALTER TABLE cc_blockcriteria DROP COLUMN IF EXISTS criteriagroup; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0020_3_0_0_alpha_7_1"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.7.2", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0022_3_0_0_alpha_7_3.py b/api/libretime_api/legacy/migrations/0022_3_0_0_alpha_7_3.py new file mode 100644 index 000000000..b8e0d8817 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0022_3_0_0_alpha_7_3.py @@ -0,0 +1,25 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE podcast_episodes ALTER COLUMN episode_description TYPE text; +""" + +DOWN = """ +ALTER TABLE podcast_episodes ALTER COLUMN episode_description TYPE VARCHAR(4096); +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0021_3_0_0_alpha_7_2"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.7.3", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0023_3_0_0_alpha_9_1.py b/api/libretime_api/legacy/migrations/0023_3_0_0_alpha_9_1.py new file mode 100644 index 000000000..101e9f525 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0023_3_0_0_alpha_9_1.py @@ -0,0 +1,25 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_files ADD COLUMN artwork VARCHAR(4096); +""" + +DOWN = """ +ALTER TABLE cc_files DROP COLUMN IF EXISTS artwork; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0022_3_0_0_alpha_7_3"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.9.1", + sql=UP, + ) + ) + ] diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.2/upgrade.sql b/api/libretime_api/legacy/migrations/0024_3_0_0_alpha_9_2.py similarity index 76% rename from legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.2/upgrade.sql rename to api/libretime_api/legacy/migrations/0024_3_0_0_alpha_9_2.py index 682e94314..46baca19a 100644 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.2/upgrade.sql +++ b/api/libretime_api/legacy/migrations/0024_3_0_0_alpha_9_2.py @@ -1,3 +1,8 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ ALTER TABLE cc_files ADD COLUMN track_type VARCHAR(16); CREATE TABLE IF NOT EXISTS "cc_track_types" @@ -20,6 +25,27 @@ INSERT INTO cc_track_types VALUES (6, 'JIN', 'Jingle', 'A short song or tune, no INSERT INTO cc_track_types VALUES (7, 'PRO', 'Promo', 'For promotional use.', true); INSERT INTO cc_track_types VALUES (8, 'SHO', 'Shout Out', 'A message of congratulation, greeting. support, or appreciation. ', true); INSERT INTO cc_track_types VALUES (9, 'NWS', 'News', 'This is used for noteworthy information, announcements.', true); -INSERT INTO cc_track_types VALUES (10, 'COM', 'Commercial', 'This is used for commerical advertising.', true); +INSERT INTO cc_track_types VALUES (10, 'COM', 'Commercial', 'This is used for commercial advertising.', true); INSERT INTO cc_track_types VALUES (11, 'ITV', 'Interview', 'This is used for radio interviews', true); INSERT INTO cc_track_types VALUES (12, 'VTR', 'Voice Tracking', 'Also referred as robojock or taped. Make announcements without actually being in the station.', true); +""" + +DOWN = """ +ALTER TABLE cc_files DROP COLUMN IF EXISTS track_type; + +DROP TABLE IF EXISTS "cc_track_types" CASCADE; +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0023_3_0_0_alpha_9_1"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.9.2", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0025_3_0_0_alpha_9_3.py b/api/libretime_api/legacy/migrations/0025_3_0_0_alpha_9_3.py new file mode 100644 index 000000000..cf6ce015e --- /dev/null +++ b/api/libretime_api/legacy/migrations/0025_3_0_0_alpha_9_3.py @@ -0,0 +1,34 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +DROP TABLE IF EXISTS "cc_smemb" CASCADE; +""" + +DOWN = """ +CREATE TABLE "cc_smemb" +( + "id" INTEGER NOT NULL, + "uid" INTEGER DEFAULT 0 NOT NULL, + "gid" INTEGER DEFAULT 0 NOT NULL, + "level" INTEGER DEFAULT 0 NOT NULL, + "mid" INTEGER, + PRIMARY KEY ("id"), + CONSTRAINT "cc_smemb_id_idx" UNIQUE ("id") +); +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0024_3_0_0_alpha_9_2"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.9.3", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/0026_3_0_0_alpha_9_4.py b/api/libretime_api/legacy/migrations/0026_3_0_0_alpha_9_4.py new file mode 100644 index 000000000..ff2a28917 --- /dev/null +++ b/api/libretime_api/legacy/migrations/0026_3_0_0_alpha_9_4.py @@ -0,0 +1,33 @@ +from django.db import migrations + +from ._migrations import legacy_migration_factory + +UP = """ +ALTER TABLE cc_files DROP COLUMN soundcloud_id; +ALTER TABLE cc_files DROP COLUMN soundcloud_error_code; +ALTER TABLE cc_files DROP COLUMN soundcloud_error_msg; +ALTER TABLE cc_files DROP COLUMN soundcloud_link_to_file; +ALTER TABLE cc_files DROP COLUMN soundcloud_upload_time; +""" + +DOWN = """ +ALTER TABLE cc_files ADD COLUMN soundcloud_id INTEGER; +ALTER TABLE cc_files ADD COLUMN soundcloud_error_code INTEGER; +ALTER TABLE cc_files ADD COLUMN soundcloud_error_msg VARCHAR(512); +ALTER TABLE cc_files ADD COLUMN soundcloud_link_to_file VARCHAR(4096); +ALTER TABLE cc_files ADD COLUMN soundcloud_upload_time TIMESTAMP(6); +""" + + +class Migration(migrations.Migration): + dependencies = [ + ("legacy", "0025_3_0_0_alpha_9_3"), + ] + operations = [ + migrations.RunPython( + code=legacy_migration_factory( + target="3.0.0-alpha.9.4", + sql=UP, + ) + ) + ] diff --git a/api/libretime_api/legacy/migrations/__init__.py b/api/libretime_api/legacy/migrations/__init__.py new file mode 100644 index 000000000..c58f3c8ea --- /dev/null +++ b/api/libretime_api/legacy/migrations/__init__.py @@ -0,0 +1 @@ +LEGACY_SCHEMA_VERSION = "3.0.0-alpha.9.4" diff --git a/api/libretime_api/legacy/migrations/_migrations.py b/api/libretime_api/legacy/migrations/_migrations.py new file mode 100644 index 000000000..9f79967c2 --- /dev/null +++ b/api/libretime_api/legacy/migrations/_migrations.py @@ -0,0 +1,77 @@ +from typing import Callable, Optional + +from django.db import 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. + """ + + if "cc_pref" not in connection.introspection.table_names(): + return None + + with connection.cursor() as cursor: + cursor.execute("SELECT valstr FROM cc_pref WHERE keystr = 'schema_version'") + row = cursor.fetchone() + return row[0] if row else 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 Exception("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 diff --git a/api/libretime_api/legacy/migrations/_version.py b/api/libretime_api/legacy/migrations/_version.py new file mode 100644 index 000000000..d2e544bab --- /dev/null +++ b/api/libretime_api/legacy/migrations/_version.py @@ -0,0 +1,38 @@ +import re + +VERSION_RE = re.compile( + r""" + (?P[0-9]+(?:\.[0-9]+)*) + (?: + -(?P(alpha|beta)) + (?: + \.(?P[0-9]+ + (?: + \.[0-9]+ + )? + ) + )? + )? + """, + re.VERBOSE | re.IGNORECASE, +) + + +def parse_version(version: str): + match = VERSION_RE.search(version) + if not match: + raise Exception(f"invalid version {version}") + + major, minor, patch = map(int, match.group("release").split(".")) + + pre_mapping = {"alpha": -2, "beta": -1, None: 0, "": 0} + pre = pre_mapping[match.group("pre_l")] + + pre_major, pre_minor = 0, 0 + pre_version = match.group("pre_n") + if pre_version: + pre_version_list = pre_version.split(".") + pre_major = int(pre_version_list.pop(0)) if len(pre_version_list) else 0 + pre_minor = int(pre_version_list.pop(0)) if len(pre_version_list) else 0 + + return (major, minor, patch, pre, pre_major, pre_minor) diff --git a/legacy/build/sql/defaultdata.sql b/api/libretime_api/legacy/migrations/sql/data.sql similarity index 99% rename from legacy/build/sql/defaultdata.sql rename to api/libretime_api/legacy/migrations/sql/data.sql index 1b58b34b4..9e75a1710 100644 --- a/legacy/build/sql/defaultdata.sql +++ b/api/libretime_api/legacy/migrations/sql/data.sql @@ -1,6 +1,3 @@ --- Schema version -INSERT INTO cc_pref("keystr", "valstr") VALUES('schema_version', '3.0.0-alpha'); - INSERT INTO cc_subjs ("login", "type", "pass") VALUES ('admin', 'A', md5('admin')); -- added in 2.3 INSERT INTO cc_stream_setting ("keyname", "value", "type") VALUES ('off_air_meta', 'LibreTime - offline', 'string'); diff --git a/legacy/build/sql/schema.sql b/api/libretime_api/legacy/migrations/sql/schema.sql similarity index 100% rename from legacy/build/sql/schema.sql rename to api/libretime_api/legacy/migrations/sql/schema.sql diff --git a/api/libretime_api/legacy/migrations/sql/sqldb.map b/api/libretime_api/legacy/migrations/sql/sqldb.map new file mode 100644 index 000000000..afb3b6917 --- /dev/null +++ b/api/libretime_api/legacy/migrations/sql/sqldb.map @@ -0,0 +1,2 @@ +# Sqlfile -> Database map +schema.sql=airtime diff --git a/api/libretime_api/legacy/tests/__init__.py b/api/libretime_api/legacy/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/libretime_api/legacy/tests/migrations/__init__.py b/api/libretime_api/legacy/tests/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/libretime_api/legacy/tests/migrations/test_version.py b/api/libretime_api/legacy/tests/migrations/test_version.py new file mode 100644 index 000000000..a7740cbb5 --- /dev/null +++ b/api/libretime_api/legacy/tests/migrations/test_version.py @@ -0,0 +1,44 @@ +import pytest + +from ...migrations._version import parse_version + + +@pytest.mark.parametrize( + "version,expected", + [ + # fmt: off + ("3.0.0-alpha", (3, 0, 0, -2, 0, 0)), + ("3.1.0-alpha.1", (3, 1, 0, -2, 1, 0)), + ("3.0.1-alpha.1.2", (3, 0, 1, -2, 1, 2)), + ("3.0.0-beta", (3, 0, 0, -1, 0, 0)), + ("3.0.0-beta.10", (3, 0, 0, -1, 10, 0)), + ("2.5.2", (2, 5, 2, 0, 0, 0)), + ("3.0.0", (3, 0, 0, 0, 0, 0)), + # fmt: on + ], +) +def test_parse_version(version: str, expected): + assert parse_version(version) == expected + + +@pytest.mark.parametrize( + "before,after", + [ + # fmt: off + ("3.0.0-alpha", "3.0.0-alpha.1"), + ("3.0.0-alpha.1", "3.0.0-alpha.2"), + ("3.0.0-alpha.1", "3.0.0-alpha.1.1"), + ("3.0.0-alpha", "3.0.0-beta"), + ("3.0.0-beta", "3.0.0"), + ("3.0.0", "3.0.1"), + ("3.0.0", "3.1.0"), + ("3.0.0", "4.0.0"), + ("2.5.3", "3.0.0"), + ("3.0.0", "3.0.0"), + # fmt: on + ], +) +def test_version_compare(before: str, after: str): + version_before = parse_version(before) + version_after = parse_version(after) + assert version_before <= version_after diff --git a/api/libretime_api/settings/_internal.py b/api/libretime_api/settings/_internal.py index ba1d67eef..507e4f8f7 100644 --- a/api/libretime_api/settings/_internal.py +++ b/api/libretime_api/settings/_internal.py @@ -9,6 +9,7 @@ DEBUG = getenv("LIBRETIME_DEBUG", "false").lower() == "true" # Application definition INSTALLED_APPS = [ + "libretime_api.legacy", "libretime_api.core", "libretime_api.history", "libretime_api.storage", diff --git a/api/setup.py b/api/setup.py index 0d91383e4..96f9f103e 100644 --- a/api/setup.py +++ b/api/setup.py @@ -21,6 +21,9 @@ setup( }, license="AGPLv3", packages=find_packages(), + package_data={ + "libretime_api": ["legacy/migrations/sql/*.sql"], + }, include_package_data=True, python_requires=">=3.6", entry_points={ diff --git a/docs/developer-manual/README.md b/docs/developer-manual/README.md index 2ad2cf809..d065a7af4 100644 --- a/docs/developer-manual/README.md +++ b/docs/developer-manual/README.md @@ -11,3 +11,4 @@ Welcome to the **LibreTime developer manual**, you should find guides to integra ## Improve and contribute to LibreTime - Learn about the [architecture of LibreTime](./design/architecture.md) +- Learn about the [database migrations](./design/database-migrations.md) diff --git a/docs/developer-manual/design/database-migrations.md b/docs/developer-manual/design/database-migrations.md new file mode 100644 index 000000000..bf4034b5f --- /dev/null +++ b/docs/developer-manual/design/database-migrations.md @@ -0,0 +1,51 @@ +# Database schema creation and migrations + +The method to maintain the database schema, is to write both a migration file for already installed databases and to update a `schema.sql` file for fresh databases. On fresh installation, the database is filled with the `schema.sql` and `data.sql` files and LibreTime won't run any sql migration on top of it. Previously, when LibreTime was upgraded, the missing migrations were run using a custom php based migration tool, those migrations are now handled by Django. The missing migrations are tracked using both a `schema_version` field in the `cc_pref` table and a Django migration id. + +:::note + +Since LibreTime forked, the `schema_version` in the `schema.sql` was locked on `3.0.0-alpha` and all the migrations were run during the first user connection. This has been fixed during the move to the Django based migrations. + +::: + +Django does not maintain a `schema.sql` file, it applies every migrations until it reaches the targeted schema represented by the code. The legacy `schema_version` has to be tracked until we remove the Propel schema generation and let Django handle all the schema migrations. Until then Propel generate the schema and Django handle migrations from already installed databases. + +:::info + +The first Django migration is the initial schema creation using the `schema.sql` and `data.sql` files. + +::: + +``` +stateDiagram-v2 + state is_django_migration_applied <> + [*] --> is_django_migration_applied: Is the django migration ID in the DB ? + + is_django_migration_applied --> [*]: Yes, ignoring... + + state "Apply django migration" as apply_django_migration + is_django_migration_applied --> apply_django_migration: No + + state apply_django_migration { + state is_legacy_migration <> + [*] --> is_legacy_migration: Is it a legacy migration ? + + state "Run django migration" as run_django_migration + state "Apply changes" as run_django_migration + state "Save migration ID in DB" as run_django_migration + is_legacy_migration --> run_django_migration: No + run_legacy_migration --> run_django_migration + run_django_migration --> [*] + + state is_legacy_migration_applied <> + is_legacy_migration_applied --> [*]: Yes, ignoring... + + state "Run legacy migration" as run_legacy_migration + state "Apply changes" as run_legacy_migration + state "Bump legacy schema version" as run_legacy_migration + is_legacy_migration_applied --> run_legacy_migration: No + is_legacy_migration --> is_legacy_migration_applied: Yes, is the DB schema version >= legacy migration schema version ? + } + + apply_django_migration --> [*] +``` diff --git a/legacy/application/common/TaskManager.php b/legacy/application/common/TaskManager.php index 4f99060bd..5027cf86f 100644 --- a/legacy/application/common/TaskManager.php +++ b/legacy/application/common/TaskManager.php @@ -180,32 +180,6 @@ interface AirtimeTask public function run(); } -/** - * Class UpgradeTask. - * - * Checks the current Airtime version and runs any outstanding upgrades - */ -class UpgradeTask implements AirtimeTask -{ - /** - * Check the current Airtime schema version to see if an upgrade should be run. - * - * @return bool true if an upgrade is needed - */ - public function shouldBeRun() - { - return UpgradeManager::checkIfUpgradeIsNeeded(); - } - - /** - * Run all upgrades above the current schema version. - */ - public function run() - { - UpgradeManager::doUpgrade(); - } -} - /** * Class CeleryTask. * diff --git a/legacy/application/controllers/UpgradeController.php b/legacy/application/controllers/UpgradeController.php deleted file mode 100644 index 320c8a43f..000000000 --- a/legacy/application/controllers/UpgradeController.php +++ /dev/null @@ -1,63 +0,0 @@ -view->layout()->disableLayout(); - $this->_helper->viewRenderer->setNoRender(true); - - if (!RestAuth::verifyAuth(true, false, $this)) { - return; - } - - try { - $didWePerformAnUpgrade = UpgradeManager::doUpgrade(); - - if (!$didWePerformAnUpgrade) { - $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody('No upgrade was performed. The current schema version is ' . Application_Model_Preference::GetSchemaVersion() . '.
'); - } else { - $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody('Upgrade to Airtime schema version ' . Application_Model_Preference::GetSchemaVersion() . ' OK
'); - } - } catch (Exception $e) { - $this->getResponse() - ->setHttpResponseCode(400) - ->appendBody($e->getMessage()); - } - } - - public function downgradeAction() - { - $this->view->layout()->disableLayout(); - $this->_helper->viewRenderer->setNoRender(true); - - if (!RestAuth::verifyAuth(true, false, $this)) { - return; - } - - $request = $this->getRequest(); - $toVersion = $request->getParam('version'); - - try { - $downgradePerformed = UpgradeManager::doDowngrade($toVersion); - - if (!$downgradePerformed) { - $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody('No downgrade was performed. The current schema version is ' . Application_Model_Preference::GetSchemaVersion() . '.
'); - } else { - $this->getResponse() - ->setHttpResponseCode(200) - ->appendBody('Downgrade to Airtime schema version ' . Application_Model_Preference::GetSchemaVersion() . ' OK
'); - } - } catch (Exception $e) { - $this->getResponse() - ->setHttpResponseCode(400) - ->appendBody($e->getMessage()); - } - } -} diff --git a/legacy/application/controllers/downgrade_sql/airtime_2.5.13/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_2.5.13/downgrade.sql deleted file mode 100644 index 70ee99ae7..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_2.5.13/downgrade.sql +++ /dev/null @@ -1,11 +0,0 @@ ------------------------------------------------------------------------ --- third_party_track_references ------------------------------------------------------------------------ - -DROP TABLE IF EXISTS "third_party_track_references" CASCADE; - ------------------------------------------------------------------------ --- celery_tasks ------------------------------------------------------------------------ - -DROP TABLE IF EXISTS "celery_tasks" CASCADE; diff --git a/legacy/application/controllers/downgrade_sql/airtime_2.5.15/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_2.5.15/downgrade.sql deleted file mode 100644 index ddb9bbc6d..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_2.5.15/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE third_party_track_references ALTER COLUMN file_id SET NOT NULL; diff --git a/legacy/application/controllers/downgrade_sql/airtime_2.5.16/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_2.5.16/downgrade.sql deleted file mode 100644 index f61be62a8..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_2.5.16/downgrade.sql +++ /dev/null @@ -1,11 +0,0 @@ -ALTER TABLE cc_files DROP COLUMN description; - -DELETE FROM cc_pref WHERE keystr = 'station_podcast_id'; - -DROP TABLE IF EXISTS "podcast" CASCADE; - -DROP TABLE IF EXISTS "imported_podcast" CASCADE; - -DROP TABLE IF EXISTS "station_podcast" CASCADE; - -DROP TABLE IF EXISTS "podcast_episodes" CASCADE; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.1/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.1/downgrade.sql deleted file mode 100644 index 5f4c8110c..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.1/downgrade.sql +++ /dev/null @@ -1,4 +0,0 @@ -ALTER TABLE imported_podcast DROP COLUMN IF EXISTS album_override; -ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP DEFAULT; -ALTER TABLE third_party_track_references ALTER COLUMN file_id SET NOT NULL; -ALTER TABLE cc_show DROP COLUMN IF EXISTS autoplaylist_repeat; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.6/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.6/downgrade.sql deleted file mode 100644 index c5705ae2c..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.6/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_service_register ALTER COLUMN ip TYPE character varying(18); diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.1/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.1/downgrade.sql deleted file mode 100644 index dfc39db7f..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.1/downgrade.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE podcast_episodes DROP COLUMN IF EXISTS episode_title; -ALTER TABLE podcast_episodes DROP COLUMN IF EXISTS episode_description; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.2/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.2/downgrade.sql deleted file mode 100644 index 915be5e56..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.2/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_blockcriteria DROP COLUMN IF EXISTS criteriagroup; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.3/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.3/downgrade.sql deleted file mode 100644 index 9f77eceae..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7.3/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE podcast_episodes ALTER COLUMN episode_description TYPE VARCHAR(4096); diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7/downgrade.sql deleted file mode 100644 index 1f562169c..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.7/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_block ALTER COLUMN type SET DEFAULT 'static'; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.1/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.1/downgrade.sql deleted file mode 100644 index 983f34ef7..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.1/downgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_files DROP COLUMN IF EXISTS artwork; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.2/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.2/downgrade.sql deleted file mode 100644 index abeb03634..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.2/downgrade.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE cc_files DROP COLUMN IF EXISTS track_type; - -DROP TABLE IF EXISTS "cc_track_types" CASCADE; diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.3/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.3/downgrade.sql deleted file mode 100644 index 5cc45ffb5..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.3/downgrade.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE "cc_smemb" -( - "id" INTEGER NOT NULL, - "uid" INTEGER DEFAULT 0 NOT NULL, - "gid" INTEGER DEFAULT 0 NOT NULL, - "level" INTEGER DEFAULT 0 NOT NULL, - "mid" INTEGER, - PRIMARY KEY ("id"), - CONSTRAINT "cc_smemb_id_idx" UNIQUE ("id") -); diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.4/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.4/downgrade.sql deleted file mode 100644 index b0baed129..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha.9.4/downgrade.sql +++ /dev/null @@ -1,6 +0,0 @@ --- we can restore the schema here but you'll need to restore data from a backup -ALTER TABLE cc_files ADD COLUMN soundcloud_id INTEGER; -ALTER TABLE cc_files ADD COLUMN soundcloud_error_code INTEGER; -ALTER TABLE cc_files ADD COLUMN soundcloud_error_msg VARCHAR(512); -ALTER TABLE cc_files ADD COLUMN soundcloud_link_to_file VARCHAR(4096); -ALTER TABLE cc_files ADD COLUMN soundcloud_upload_time TIMESTAMP(6); diff --git a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha/downgrade.sql b/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha/downgrade.sql deleted file mode 100644 index 49097985b..000000000 --- a/legacy/application/controllers/downgrade_sql/airtime_3.0.0-alpha/downgrade.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE cc_show_instances DROP COLUMN IF EXISTS autoplaylist_built; -ALTER TABLE cc_show DROP COLUMN IF EXISTS has_autoplaylist; -ALTER TABLE cc_show DROP COLUMN IF EXISTS autoplaylist_id; diff --git a/legacy/application/controllers/plugins/PageLayoutInitPlugin.php b/legacy/application/controllers/plugins/PageLayoutInitPlugin.php index 1ce81c445..2d3b85416 100644 --- a/legacy/application/controllers/plugins/PageLayoutInitPlugin.php +++ b/legacy/application/controllers/plugins/PageLayoutInitPlugin.php @@ -54,16 +54,10 @@ class PageLayoutInitPlugin extends Zend_Controller_Plugin_Abstract $this->_initViewHelpers(); } - // Skip upgrades and task management when running unit tests + // Skip task management when running unit tests if (getenv('AIRTIME_UNIT_TEST') != 1) { $taskManager = TaskManager::getInstance(); - // Run the upgrade on each request (if it needs to be run) - // We can't afford to wait 7 minutes to run an upgrade: users could - // have several minutes of database errors while waiting for a - // schema change upgrade to happen after a deployment - $taskManager->runTask('UpgradeTask'); - // Piggyback the TaskManager onto API calls. This provides guaranteed consistency // (there is at least one API call made from pypo to Airtime every 7 minutes) and // greatly reduces the chances of lock contention on cc_pref while the TaskManager runs diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.10/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.10/upgrade.sql deleted file mode 100644 index 25310cfb2..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.10/upgrade.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE cc_files ADD COLUMN filesize integer NOT NULL -CONSTRAINT filesize_default DEFAULT 0 diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.12/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.12/upgrade.sql deleted file mode 100644 index 92ca30ade..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.12/upgrade.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE cc_show ALTER COLUMN description TYPE varchar(8192); -ALTER TABLE cc_show_instances ALTER COLUMN description TYPE varchar(8192); diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.14/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.14/upgrade.sql deleted file mode 100644 index 0e721b538..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.14/upgrade.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE cc_pref ALTER COLUMN subjid SET DEFAULT NULL; -CREATE UNIQUE INDEX cc_pref_key_idx ON cc_pref (keystr) WHERE subjid IS NULL; -ANALYZE cc_pref; diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.15/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.15/upgrade.sql deleted file mode 100644 index 9d5051ae5..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.15/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP NOT NULL; diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.17/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.17/upgrade.sql deleted file mode 100644 index 09a7a75f0..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.17/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_files ADD COLUMN artwork TYPE character varying(255); diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.2/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.2/upgrade.sql deleted file mode 100644 index 2f805382d..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.2/upgrade.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Replacing system_version with schema_version -DELETE FROM cc_pref WHERE keystr = 'system_version'; -INSERT INTO cc_pref (keystr, valstr) VALUES ('schema_version', '2.5.2'); - -ALTER TABLE cc_show ADD COLUMN image_path varchar(255) DEFAULT ''; -ALTER TABLE cc_show_instances ADD COLUMN description varchar(255) DEFAULT ''; diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.3/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.3/upgrade.sql deleted file mode 100644 index 6c7980983..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.3/upgrade.sql +++ /dev/null @@ -1,6 +0,0 @@ -DELETE FROM cc_pref WHERE keystr = 'system_version'; -INSERT INTO cc_pref (keystr, valstr) VALUES ('system_version', '2.5.3'); - -ALTER TABLE cc_files DROP COLUMN state; -ALTER TABLE cc_files ADD import_status integer default 1; -- Default is "pending" -UPDATE cc_files SET import_status=0; -- Existing files are already "imported" diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.5/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.5/upgrade.sql deleted file mode 100644 index 191eb2084..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.5/upgrade.sql +++ /dev/null @@ -1,5 +0,0 @@ -DELETE FROM cc_pref WHERE keystr = 'system_version'; -INSERT INTO cc_pref (keystr, valstr) VALUES ('system_version', '2.5.5'); - -ALTER TABLE cc_show ADD COLUMN image_path varchar(255) DEFAULT ''; -ALTER TABLE cc_show_instances ADD COLUMN description varchar(255) DEFAULT ''; diff --git a/legacy/application/controllers/upgrade_sql/airtime_2.5.9/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_2.5.9/upgrade.sql deleted file mode 100644 index 3e6011851..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_2.5.9/upgrade.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE cloud_file -( - id serial NOT NULL, - resource_id text NOT NULL, - storage_backend text NOT NULL, - cc_file_id integer NOT NULL, - CONSTRAINT cloud_file_pkey PRIMARY KEY (id), - CONSTRAINT "cloud_file_FK_1" FOREIGN KEY (cc_file_id) - REFERENCES cc_files (id) MATCH SIMPLE - ON UPDATE NO ACTION ON DELETE CASCADE -) diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.1/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.1/upgrade.sql deleted file mode 100644 index 76ffa03d7..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.1/upgrade.sql +++ /dev/null @@ -1,4 +0,0 @@ -ALTER TABLE imported_podcast ADD COLUMN album_override boolean default 'f' NOT NULL; -ALTER TABLE third_party_track_references ALTER COLUMN file_id SET DEFAULT 0; -ALTER TABLE third_party_track_references ALTER COLUMN file_id DROP NOT NULL; -ALTER TABLE cc_show ADD COLUMN autoplaylist_repeat boolean default 'f' NOT NULL; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.6/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.6/upgrade.sql deleted file mode 100644 index e5a748f51..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.6/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_service_register ALTER COLUMN ip TYPE character varying(45); diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.1/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.1/upgrade.sql deleted file mode 100644 index 8777f59c8..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.1/upgrade.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE podcast_episodes ADD COLUMN episode_title VARCHAR(4096); -ALTER TABLE podcast_episodes ADD COLUMN episode_description VARCHAR(4096); diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.2/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.2/upgrade.sql deleted file mode 100644 index a274478bf..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.2/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_blockcriteria ADD COLUMN criteriagroup integer; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.3/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.3/upgrade.sql deleted file mode 100644 index 8cc79a890..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7.3/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE podcast_episodes ALTER COLUMN episode_description TYPE text; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7/upgrade.sql deleted file mode 100644 index 08b362d0e..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.7/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_block ALTER COLUMN type SET DEFAULT 'dynamic'; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.1/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.1/upgrade.sql deleted file mode 100644 index 3c47a0fee..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.1/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE cc_files ADD COLUMN artwork VARCHAR(4096); diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.3/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.3/upgrade.sql deleted file mode 100644 index f6583ff5d..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.3/upgrade.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS "cc_smemb" CASCADE; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.4/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.4/upgrade.sql deleted file mode 100644 index 4569697ba..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha.9.4/upgrade.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE cc_files DROP COLUMN soundcloud_id; -ALTER TABLE cc_files DROP COLUMN soundcloud_error_code; -ALTER TABLE cc_files DROP COLUMN soundcloud_error_msg; -ALTER TABLE cc_files DROP COLUMN soundcloud_link_to_file; -ALTER TABLE cc_files DROP COLUMN soundcloud_upload_time; diff --git a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha/upgrade.sql b/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha/upgrade.sql deleted file mode 100644 index e49219781..000000000 --- a/legacy/application/controllers/upgrade_sql/airtime_3.0.0-alpha/upgrade.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE cc_show ADD COLUMN has_autoplaylist boolean default 'f' NOT NULL; -ALTER TABLE cc_show ADD COLUMN autoplaylist_id integer DEFAULT NULL; -ALTER TABLE cc_show_instances ADD COLUMN autoplaylist_built boolean default 'f' NOT NULL; diff --git a/legacy/application/upgrade/Upgrades.php b/legacy/application/upgrade/Upgrades.php deleted file mode 100644 index ef3a9e5cf..000000000 --- a/legacy/application/upgrade/Upgrades.php +++ /dev/null @@ -1,726 +0,0 @@ -getNewVersion() == $toVersion) { - break; // We've reached the version we wanted to downgrade to, so break - } - $downgradePerformed = self::_runDowngrade($downgrader) ? true : $downgradePerformed; - } - - return $downgradePerformed; - } - - /** - * Run the given upgrade. - * - * @param $upgrader AirtimeUpgrader the upgrader class to be executed - * - * @return bool true if the upgrade was successful, otherwise false - */ - private static function _runUpgrade(AirtimeUpgrader $upgrader) - { - return $upgrader->checkIfUpgradeSupported() && $upgrader->upgrade(); - } - - /** - * Run the given downgrade. - * - * @param $downgrader AirtimeUpgrader the upgrader class to be executed - * @param $supportedVersions array array of supported versions - * - * @return bool true if the downgrade was successful, otherwise false - */ - private static function _runDowngrade(AirtimeUpgrader $downgrader) - { - return $downgrader->checkIfDowngradeSupported() && $downgrader->downgrade(); - } -} - -abstract class AirtimeUpgrader -{ - protected $_dir; - - protected $host; - protected $port; - protected $database; - protected $username; - protected $password; - - /** - * @param $dir string directory housing upgrade files - */ - public function __construct($dir) - { - $this->_dir = $dir; - } - - /** Schema versions that this upgrader class can upgrade from (an array of version strings). */ - abstract protected function getSupportedSchemaVersions(); - - /** The schema version that this upgrader class will upgrade to. (returns a version string) */ - abstract public function getNewVersion(); - - public static function getCurrentSchemaVersion() - { - return Application_Model_Preference::GetSchemaVersion(); - } - - /** - * This function checks to see if this class can perform an upgrade of your version of Airtime. - * - * @return bool true if we can upgrade your version of Airtime - */ - public function checkIfUpgradeSupported() - { - return in_array(static::getCurrentSchemaVersion(), $this->getSupportedSchemaVersions()); - } - - /** - * This function checks to see if this class can perform a downgrade of your version of Airtime. - * - * @return bool true if we can downgrade your version of Airtime - */ - public function checkIfDowngradeSupported() - { - return static::getCurrentSchemaVersion() == $this->getNewVersion(); - } - - protected function toggleMaintenanceScreen($toggle) - { - if ($toggle) { - // Disable Airtime UI - // create a temporary maintenance notification file - // when this file is on the server, zend framework redirects all - // requests to the maintenance page and sets a 503 response code - /* DISABLED because this does not work correctly - $this->maintenanceFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."maintenance.txt" : "/tmp/maintenance.txt"; - $file = fopen($this->maintenanceFile, 'w'); - fclose($file); - */ - } - // delete maintenance.txt to give users access back to Airtime - /* DISABLED because this does not work correctly - if ($this->maintenanceFile) { - unlink($this->maintenanceFile); - }*/ - } - - /** - * Implement this for each new version of Airtime - * This function abstracts out the core upgrade functionality, - * allowing child classes to overwrite _runUpgrade to reduce duplication. - */ - public function upgrade() - { - try { - // $this->toggleMaintenanceScreen(true); - - $this->_getDbValues(); - $this->_runUpgrade(); - - Application_Model_Preference::SetSchemaVersion($this->getNewVersion()); - - // $this->toggleMaintenanceScreen(false); - } catch (Exception $e) { - // $this->toggleMaintenanceScreen(false); - Logging::error('Error in upgrade: ' . $e->getMessage()); - - return false; - } - - return true; - } - - /** - * Implement this for each new version of Airtime - * This function abstracts out the core downgrade functionality, - * allowing child classes to overwrite _runDowngrade to reduce duplication. - */ - public function downgrade() - { - try { - $this->_getDbValues(); - $this->_runDowngrade(); - - $highestSupportedVersion = null; - foreach ($this->getSupportedSchemaVersions() as $v) { - // version_compare returns 1 (true) if the second parameter is lower - if (!$highestSupportedVersion || version_compare($v, $highestSupportedVersion)) { - $highestSupportedVersion = $v; - } - } - - // Set the schema version to the highest supported version so we don't skip versions when downgrading - Application_Model_Preference::SetSchemaVersion($highestSupportedVersion); - } catch (Exception $e) { - return false; - } - - return true; - } - - protected function _getDbValues() - { - $config = Config::getConfig(); - - $this->host = $config['dsn']['host']; - $this->port = $config['dsn']['port']; - $this->username = $config['dsn']['username']; - $this->password = $config['dsn']['password']; - $this->database = $config['dsn']['database']; - } - - protected function _runPsql($args) - { - $command = <<<"END" -PGPASSWORD={$this->password} \\ -/usr/bin/psql --quiet \\ - --host={$this->host} \\ - --port={$this->port} \\ - --dbname={$this->database} \\ - --username={$this->username} \\ - {$args} -END; - passthru($command); - } - - protected function _runUpgrade() - { - $sqlFile = "{$this->_dir}/upgrade_sql/airtime_{$this->getNewVersion()}/upgrade.sql"; - $args = <<<"END" ---file={$sqlFile} 2>&1 \\ - | grep -v -E "will create implicit sequence|will create implicit index" -END; - $this->_runPsql($args); - } - - protected function _runDowngrade() - { - $sqlFile = "{$this->_dir}/downgrade_sql/airtime_{$this->getNewVersion()}/downgrade.sql"; - $args = <<<"END" ---file={$sqlFile} 2>&1 \\ - | grep -v -E "will create implicit sequence|will create implicit index" -END; - $this->_runPsql($args); - } -} - -class AirtimeUpgrader253 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return ['2.5.1', '2.5.2']; - } - - public function getNewVersion() - { - return '2.5.3'; - } - - protected function _runUpgrade() - { - // Update disk_usage value in cc_pref - $storDir = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE'] . 'srv/airtime/stor' : '/srv/airtime/stor'; - $diskUsage = shell_exec("du -sb {$storDir} | awk '{print $1}'"); - - Application_Model_Preference::setDiskUsage($diskUsage); - - parent::_runUpgrade(); - } -} - -class AirtimeUpgrader254 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return ['2.5.3']; - } - - public function getNewVersion() - { - return '2.5.4'; - } - - protected function _runUpgrade() - { - // First, ensure there are no superadmins already. - $numberOfSuperAdmins = CcSubjsQuery::create() - ->filterByDbType(UTYPE_SUPERADMIN) - ->filterByDbLogin('sourcefabric_admin', Criteria::NOT_EQUAL) // Ignore sourcefabric_admin users - ->count(); - - // Only create a super admin if there isn't one already. - if ($numberOfSuperAdmins == 0) { - // Find the "admin" user and promote them to superadmin. - $adminUser = CcSubjsQuery::create() - ->filterByDbLogin('admin') - ->findOne(); - if (!$adminUser) { - // Otherwise get the user with the lowest ID that is of type administrator: - $adminUser = CcSubjsQuery::create() - ->filterByDbType(UTYPE_ADMIN) - ->orderByDbId(Criteria::ASC) - ->findOne(); - - if (!$adminUser) { - throw new Exception("Failed to find any users of type 'admin' ('A')."); - } - } - - $adminUser = new Application_Model_User($adminUser->getDbId()); - $adminUser->setType(UTYPE_SUPERADMIN); - $adminUser->save(); - Logging::info($_SERVER['HTTP_HOST'] . ': ' . $this->getNewVersion() . ' Upgrade: Promoted user ' . $adminUser->getLogin() . ' to be a Super Admin.'); - - // Also try to promote the sourcefabric_admin user - $sofabAdminUser = CcSubjsQuery::create() - ->filterByDbLogin('sourcefabric_admin') - ->findOne(); - if ($sofabAdminUser) { - $sofabAdminUser = new Application_Model_User($sofabAdminUser->getDbId()); - $sofabAdminUser->setType(UTYPE_SUPERADMIN); - $sofabAdminUser->save(); - Logging::info($_SERVER['HTTP_HOST'] . ': ' . $this->getNewVersion() . ' Upgrade: Promoted user ' . $sofabAdminUser->getLogin() . ' to be a Super Admin.'); - } - } - } -} - -class AirtimeUpgrader255 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.4', - ]; - } - - public function getNewVersion() - { - return '2.5.5'; - } -} - -class AirtimeUpgrader259 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.5', - ]; - } - - public function getNewVersion() - { - return '2.5.9'; - } -} - -class AirtimeUpgrader2510 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.9', - ]; - } - - public function getNewVersion() - { - return '2.5.10'; - } -} - -class AirtimeUpgrader2511 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.10', - ]; - } - - public function getNewVersion() - { - return '2.5.11'; - } - - protected function _runUpgrade() - { - $queryResult = CcFilesQuery::create() - ->select(['disk_usage']) - ->withColumn('SUM(CcFiles.filesize)', 'disk_usage') - ->find(); - $disk_usage = $queryResult[0]; - Application_Model_Preference::setDiskUsage($disk_usage); - } -} - -class AirtimeUpgrader2512 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.10', - '2.5.11', - ]; - } - - public function getNewVersion() - { - return '2.5.12'; - } -} - -/** - * Class AirtimeUpgrader2513 - Celery and SoundCloud upgrade. - * - * Adds third_party_track_references and celery_tasks tables for third party service - * authentication and task architecture. - * - *
third_party_track_references schema: - * - * id -> int PK - * service -> string internal service name - * foreign_id -> int external unique service id - * file_id -> int internal FK->cc_files track id - * upload_time -> timestamp internal upload timestamp - * status -> string external service status - * - *
celery_tasks schema: - * - * id -> int PK - * task_id -> string external unique amqp results identifier - * track_reference -> int internal FK->third_party_track_references id - * name -> string external Celery task name - * dispatch_time -> timestamp internal message dispatch time - * status -> string external Celery task status - */ -class AirtimeUpgrader2513 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.12', - ]; - } - - public function getNewVersion() - { - return '2.5.13'; - } -} - -/** - * Class AirtimeUpgrader2514. - * - * SAAS-923 - Add a partial constraint to cc_pref so that keystrings must be unique - */ -class AirtimeUpgrader2514 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.13', - ]; - } - - public function getNewVersion() - { - return '2.5.14'; - } -} - -/** - * Class AirtimeUpgrader2515. - * - * SAAS-1071 - Remove not null constraint from file_id fk in third_party_track_references - * so that we can create track references for downloads (which won't have a file - * ID until the task is run and the file is POSTed back to Airtime) - */ -class AirtimeUpgrader2515 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.14', - ]; - } - - public function getNewVersion() - { - return '2.5.15'; - } -} - -class AirtimeUpgrader2516 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.15', - ]; - } - - public function getNewVersion() - { - return '2.5.16'; - } -} -class AirtimeUpgrader300alpha extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '2.5.16', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha'; - } -} - -class AirtimeUpgrader300alpha1 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.1'; - } -} - -class AirtimeUpgrader300alpha6 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.1', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.6'; - } -} - -/** - * Class AirtimeUpgrader300alpha7. - * - * GH-#636 - https://github.com/libretime/libretime/pull/636 - Change dynamic smartblock to be default smartblock type - */ -class AirtimeUpgrader300alpha7 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.6', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.7'; - } -} - -/** - * Class AirtimeUpgrader300alpha7-1. - * - * GH-#659 - https://github.com/libretime/libretime/pull/659/ - Add description and title to podcast episodes database table - */ -class AirtimeUpgrader300alpha7_1 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.7', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.7.1'; - } -} - -/** - * Class AirtimeUpgrader300alpha7-2. - * - * GH-#704 - https://github.com/libretime/libretime/pull/704/ - Add criteria group to smartblock table to enable database to store separately - */ -class AirtimeUpgrader300alpha7_2 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.7.1', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.7.2'; - } -} -class AirtimeUpgrader300alpha7_3 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.7.2', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.7.3'; - } -} - -class AirtimeUpgrader300alpha9_1 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.7.3', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.9.1'; - } -} - -class AirtimeUpgrader300alpha9_2 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.9.1', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.9.2'; - } -} - -class AirtimeUpgrader200alpha9_3 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.9.2', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.9.3'; - } -} - -class AirtimeUpgrader200alpha9_4 extends AirtimeUpgrader -{ - protected function getSupportedSchemaVersions() - { - return [ - '3.0.0-alpha.9.3', - ]; - } - - public function getNewVersion() - { - return '3.0.0-alpha.9.4'; - } -} diff --git a/legacy/build/build.properties b/legacy/build/build.properties index afc6bcaa2..b197d1741 100644 --- a/legacy/build/build.properties +++ b/legacy/build/build.properties @@ -18,7 +18,8 @@ propel.conf.dir = ${project.build} propel.output.dir = ${project.home} propel.php.dir = ${propel.output.dir}/application/models propel.phpconf.dir = ${propel.output.dir}/application/configs -propel.sql.dir = ${project.build}/sql +# propel.sql.dir = ${project.build}/sql +propel.sql.dir = ${project.home}/../api/libretime_api/legacy/migrations/sql # set the name for the configuration file # set the name for the configuration file diff --git a/legacy/build/sql/sequences.sql b/legacy/build/sql/sequences.sql deleted file mode 100644 index 1b24aeb07..000000000 --- a/legacy/build/sql/sequences.sql +++ /dev/null @@ -1,7 +0,0 @@ -DROP SEQUENCE IF EXISTS schedule_group_id_seq CASCADE; - -CREATE SEQUENCE schedule_group_id_seq; - -DROP SEQUENCE IF EXISTS show_group_id_seq CASCADE; - -CREATE SEQUENCE show_group_id_seq; diff --git a/legacy/build/sql/sqldb.map b/legacy/build/sql/sqldb.map deleted file mode 100644 index 581705ff6..000000000 --- a/legacy/build/sql/sqldb.map +++ /dev/null @@ -1,6 +0,0 @@ -# Sqlfile -> Database map -schema.sql=airtime -sequences.sql=campcaster -views.sql=campcaster -triggers.sql=campcaster -defaultdata.sql=campcaster diff --git a/legacy/build/sql/triggers.sql b/legacy/build/sql/triggers.sql deleted file mode 100644 index c28ada902..000000000 --- a/legacy/build/sql/triggers.sql +++ /dev/null @@ -1,6 +0,0 @@ ----------------------------------------------------------------------------------- --- calculate_position() ----------------------------------------------------------------------------------- -DROP FUNCTION IF EXISTS calculate_position CASCADE; - --- remove this trigger for group adds/delete diff --git a/legacy/build/sql/views.sql b/legacy/build/sql/views.sql deleted file mode 100644 index 7cbc6a4dd..000000000 --- a/legacy/build/sql/views.sql +++ /dev/null @@ -1,5 +0,0 @@ -------------------------------------------------------- --- cc_playlisttimes -------------------------------------------------------- - -DROP VIEW IF EXISTS cc_playlisttimes; diff --git a/legacy/public/setup/database-setup.php b/legacy/public/setup/database-setup.php index 134b70687..deecc7d23 100644 --- a/legacy/public/setup/database-setup.php +++ b/legacy/public/setup/database-setup.php @@ -167,23 +167,17 @@ class DatabaseSetup extends Setup */ private function createDatabaseTables() { - $sqlDir = dirname(__DIR__, 2) . '/build/sql/'; - $files = ['schema.sql', 'sequences.sql', 'views.sql', 'triggers.sql', 'defaultdata.sql']; - foreach ($files as $f) { + $sqlDir = dirname(ROOT_PATH) . '/api/libretime_api/legacy/migrations/sql/'; + $files = ['schema.sql', 'data.sql']; + foreach ($files as $file) { try { - /* - * Unfortunately, we need to use exec here due to PDO's lack of support for importing - * multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import, - * so the necessary DROPs on non-existent tables make it unusable. Prepared statements - * have multiple issues; they similarly die on any SQL errors, fail to read in multi-line - * commands, and fail on any unescaped ? or $ characters. - */ - exec('export PGPASSWORD=' . self::$_properties['password'] . ' && /usr/bin/psql -U ' . self::$_properties['user'] - . ' --dbname ' . self::$_properties['name'] . ' -h ' . self::$_properties['host'] - . " -f {$sqlDir}{$f} 2>/dev/null", $out, $status); - } catch (Exception $e) { + $sql = file_get_contents($sqlDir . $file); + self::$dbh->exec($sql); + } catch (PDOException $e) { + echo $e->getMessage(); + throw new AirtimeDatabaseException( - 'There was an error setting up the Airtime schema!', + 'There was an error setting up the Airtime schema!: ' . $e->getMessage(), [self::DB_NAME] ); } diff --git a/legacy/tests/application/helpers/AirtimeInstall.php b/legacy/tests/application/helpers/AirtimeInstall.php index b210d2272..19427572b 100644 --- a/legacy/tests/application/helpers/AirtimeInstall.php +++ b/legacy/tests/application/helpers/AirtimeInstall.php @@ -214,30 +214,22 @@ class AirtimeInstall public static function CreateDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost, $dbport) { echo ' * Creating database tables' . PHP_EOL; - // Put Propel sql files in Database - // $command = AirtimeInstall::CONF_DIR_WWW."/library/propel/generator/bin/propel-gen ".AirtimeInstall::CONF_DIR_WWW."/build/ insert-sql 2>/dev/null"; - $dir = self::GetAirtimeSrcDir() . '/build/sql/'; - $files = ['schema.sql', 'sequences.sql', 'views.sql', 'triggers.sql', 'defaultdata.sql']; - foreach ($files as $f) { - $command = <<<"END" -PGPASSWORD={$dbpasswd} \\ -/usr/bin/psql \\ - --host={$dbhost} \\ - --port={$dbport} \\ - --dbname={$dbname} \\ - --username={$dbuser} \\ - --file {$dir}{$f} 2>&1 -END; - @exec($command, $output, $results); + $con = Propel::getConnection(); + $sqlDir = dirname(ROOT_PATH) . '/api/libretime_api/legacy/migrations/sql/'; + $files = ['schema.sql', 'data.sql']; + foreach ($files as $file) { + try { + $sql = file_get_contents($sqlDir . $file); + $con->exec($sql); + } catch (PDOException $e) { + echo $e->getMessage(); + + throw $e; + } } AirtimeInstall::$databaseTablesCreated = true; } - final public static function UpdateDatabaseTables() - { - UpgradeManager::doUpgrade(); - } - public static function SetAirtimeVersion($p_version) { $con = Propel::getConnection(); diff --git a/legacy/tests/application/helpers/TestHelper.php b/legacy/tests/application/helpers/TestHelper.php index e941fa9df..e47ce4bef 100644 --- a/legacy/tests/application/helpers/TestHelper.php +++ b/legacy/tests/application/helpers/TestHelper.php @@ -126,7 +126,6 @@ class TestHelper } else { // Create all the database tables AirtimeInstall::CreateDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost, $dbport); - AirtimeInstall::UpdateDatabaseTables(); } }