From 3af63bbf216c000cee8403f1e8692bdd08f5ad64 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 25 Aug 2026 15:38:48 -0700 Subject: [PATCH] django: Rename DB tables from plinth_ to freedombox_ - Migration files have already been renamed from plinth/migrations/*.py to freedombox/migrations/*.py. When running these migrations, ensure that database tables are first created with plinth_* names rather than freedombox_*. This is because we want the database progression path for fresh installations (with not tables) as well as the old installation (with plinth_ tables) to be the same. To achieve this, migrations.CreateModel will be provided with db_table=plinth_* values. - Ensure if a migration call (plinth, 0001_initial) ran, then the (freedombox, 0001_initial) is not run again. This is achieved by adding replaces = [] in the newly renamed migration. - Create a new migration that renames plinth_* tables to freedombox_*. - Fix an old migration (0003) to use historic model rather than current model. This ensure that it uses the plinth_ table during migration rather than freedombox_ table. Tests: - Delete the database and start plinth.service with all the patches. Notice that the database migration (fresh creation) works well. First setup, first wizard, bepasty app installation, and notifications work as expected. Notice that django_migrations table contains entries for (plinth,000[1-6]*) as well as (freedombox,000[1-7]*). - Delete the database and start plinth.service without the patches. Run first setup and wizard. Install bepasty. Then apply the patches and restart plinth.service. Notice that the database migration (renaming of tables) works well. First setup is already done. First wizard is not shown. bepasty is shown as installed. Previously shown notifications are retained . Notice that django_migrations table contains entries for (plinth,000[1-6]*) as well as (freedombox,000[1-7]*). - Delete the database and start plinth.service from trixie version. Run first setup and wizard. Install bepasty. Then apply the patches and restart plinth.service. Notice that the database migration (renaming of tables) works well. First setup is already done. First wizard is not shown. bepasty is shown as installed. Previously shown notifications are retained . Notice that django_migrations table contains entries for (plinth,000[1-6]*) as well as (freedombox,000[1-7]*). Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- freedombox/migrations/0001_initial.py | 5 ++++ freedombox/migrations/0002_modulestore.py | 5 ++++ .../0003_merge_firstboot_completed_fields.py | 8 +++++-- freedombox/migrations/0004_userprofile.py | 5 ++++ .../migrations/0005_storednotification.py | 5 ++++ freedombox/migrations/0006_userpasskey.py | 5 ++++ .../0007_rename_plinth_to_freedombox.py | 24 +++++++++++++++++++ 7 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 freedombox/migrations/0007_rename_plinth_to_freedombox.py diff --git a/freedombox/migrations/0001_initial.py b/freedombox/migrations/0001_initial.py index e5d1d01ca..f5df0c9ae 100644 --- a/freedombox/migrations/0001_initial.py +++ b/freedombox/migrations/0001_initial.py @@ -19,9 +19,14 @@ class Migration(migrations.Migration): dependencies: list = [] + replaces = [ + ('plinth', '0001_initial'), + ] + operations = [ migrations.CreateModel( name='KVStore', + options={'db_table': 'plinth_kvstore'}, fields=[ ('key', models.TextField(primary_key=True, serialize=False)), ('value_json', models.TextField()), diff --git a/freedombox/migrations/0002_modulestore.py b/freedombox/migrations/0002_modulestore.py index 69ff718c9..4c684464a 100644 --- a/freedombox/migrations/0002_modulestore.py +++ b/freedombox/migrations/0002_modulestore.py @@ -11,9 +11,14 @@ class Migration(migrations.Migration): ('freedombox', '0001_initial'), ] + replaces = [ + ('plinth', '0002_modulestore'), + ] + operations = [ migrations.CreateModel( name='Module', + options={'db_table': 'plinth_module'}, fields=[ ('name', models.TextField(primary_key=True, serialize=False)), ('setup_version', models.IntegerField()), diff --git a/freedombox/migrations/0003_merge_firstboot_completed_fields.py b/freedombox/migrations/0003_merge_firstboot_completed_fields.py index a395fafc2..ff3164ce4 100644 --- a/freedombox/migrations/0003_merge_firstboot_completed_fields.py +++ b/freedombox/migrations/0003_merge_firstboot_completed_fields.py @@ -8,8 +8,6 @@ from __future__ import unicode_literals from django.db import migrations -from freedombox.models import KVStore - def merge_firstboot_finished_fields(apps, schema_editor): """ @@ -18,6 +16,8 @@ def merge_firstboot_finished_fields(apps, schema_editor): 'firstboot_completed' is the most accurate name for now, and by combining the fields we do not have to deal with legacy states/fields anymore. """ + KVStore = apps.get_model('freedombox', 'KVStore') + # Get and remove 'firstboot_state' firstboot_state = 0 try: @@ -62,6 +62,10 @@ class Migration(migrations.Migration): ('freedombox', '0002_modulestore'), ] + replaces = [ + ('plinth', '0003_merge_firstboot_completed_fields'), + ] + operations = [ migrations.RunPython(merge_firstboot_finished_fields), ] diff --git a/freedombox/migrations/0004_userprofile.py b/freedombox/migrations/0004_userprofile.py index 679d9bd54..797e6e9a1 100644 --- a/freedombox/migrations/0004_userprofile.py +++ b/freedombox/migrations/0004_userprofile.py @@ -27,9 +27,14 @@ class Migration(migrations.Migration): ('freedombox', '0003_merge_firstboot_completed_fields'), ] + replaces = [ + ('plinth', '0004_userprofile'), + ] + operations = [ migrations.CreateModel( name='UserProfile', + options={'db_table': 'plinth_userprofile'}, fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, diff --git a/freedombox/migrations/0005_storednotification.py b/freedombox/migrations/0005_storednotification.py index 6850ba6cd..e0d87e4a4 100644 --- a/freedombox/migrations/0005_storednotification.py +++ b/freedombox/migrations/0005_storednotification.py @@ -18,9 +18,14 @@ class Migration(migrations.Migration): ('freedombox', '0004_userprofile'), ] + replaces = [ + ('plinth', '0005_storednotification'), + ] + operations = [ migrations.CreateModel( name='StoredNotification', + options={'db_table': 'plinth_storednotification'}, fields=[ ('id', models.CharField(max_length=128, primary_key=True, diff --git a/freedombox/migrations/0006_userpasskey.py b/freedombox/migrations/0006_userpasskey.py index 91788541c..9f2d70de1 100644 --- a/freedombox/migrations/0006_userpasskey.py +++ b/freedombox/migrations/0006_userpasskey.py @@ -19,9 +19,14 @@ class Migration(migrations.Migration): ('freedombox', '0005_storednotification'), ] + replaces = [ + ('plinth', '0006_userpasskey'), + ] + operations = [ migrations.CreateModel( name='UserPasskey', + options={'db_table': 'plinth_userpasskey'}, fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, diff --git a/freedombox/migrations/0007_rename_plinth_to_freedombox.py b/freedombox/migrations/0007_rename_plinth_to_freedombox.py new file mode 100644 index 000000000..cf93f91d9 --- /dev/null +++ b/freedombox/migrations/0007_rename_plinth_to_freedombox.py @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +""" +Django migration for renaming all the DB tables from plinth_ to freedombox_. +""" + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('freedombox', '0006_userpasskey'), + ] + + operations = [ + migrations.AlterModelTable(name='KVStore', table='freedombox_kvstore'), + migrations.AlterModelTable(name='Module', table='freedombox_module'), + migrations.AlterModelTable(name='UserProfile', + table='freedombox_userprofile'), + migrations.AlterModelTable(name='StoredNotification', + table='freedombox_storednotification'), + migrations.AlterModelTable(name='UserPasskey', + table='freedombox_userpasskey'), + ]