sts-sponsors team mailing list archive
-
sts-sponsors team
-
Mailing list archive
-
Message #07628
[Merge] ~bjornt/maas:current-script-set-foreign-key-constraints into maas:master
Björn Tillenius has proposed merging ~bjornt/maas:current-script-set-foreign-key-constraints into maas:master with ~bjornt/maas:move-metadataserver-script-models as a prerequisite.
Commit message:
Add foreign key constraints for:
* Node.current_testing_script_set
* Node.current_installation_script_set
* Node.current_commissioning_script_set
A bug in Django caused those not be created, since they referenced
a model in a different app (maasserver -> metadataserver)
This patch adds those constraints by temporarily changing the fields
to be integer fields, clean up the data (since there might be incorrect
foreign key links), and then change back the columns to be foreign keys.
Requested reviews:
MAAS Maintainers (maas-maintainers)
For more details, see:
https://code.launchpad.net/~bjornt/maas/+git/maas/+merge/441925
--
Your team MAAS Committers is subscribed to branch maas:master.
diff --git a/src/maasserver/migrations/maasserver/0298_current_script_set_foreign_keys_drop_indexes.py b/src/maasserver/migrations/maasserver/0298_current_script_set_foreign_keys_drop_indexes.py
new file mode 100644
index 0000000..b7ac4bf
--- /dev/null
+++ b/src/maasserver/migrations/maasserver/0298_current_script_set_foreign_keys_drop_indexes.py
@@ -0,0 +1,39 @@
+# Generated by Django 3.2.12 on 2022-11-21 14:03
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("maasserver", "0297_move_metadata_script_models"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="node",
+ name="current_commissioning_script_set",
+ field=models.IntegerField(
+ blank=True,
+ db_column="current_commissioning_script_set_id",
+ null=True,
+ ),
+ ),
+ migrations.AlterField(
+ model_name="node",
+ name="current_installation_script_set",
+ field=models.IntegerField(
+ blank=True,
+ db_column="current_installation_script_set_id",
+ null=True,
+ ),
+ ),
+ migrations.AlterField(
+ model_name="node",
+ name="current_testing_script_set",
+ field=models.IntegerField(
+ blank=True,
+ db_column="current_testing_script_set_id",
+ null=True,
+ ),
+ ),
+ ]
diff --git a/src/maasserver/migrations/maasserver/0299_current_script_set_foreign_keys_cleanup.py b/src/maasserver/migrations/maasserver/0299_current_script_set_foreign_keys_cleanup.py
new file mode 100644
index 0000000..b62b994
--- /dev/null
+++ b/src/maasserver/migrations/maasserver/0299_current_script_set_foreign_keys_cleanup.py
@@ -0,0 +1,37 @@
+# Generated by Django 3.2.12 on 2022-11-21 14:03
+
+from django.db import migrations, models
+
+
+def clean_up_missing_scriptset_links(apps, schema_editor):
+ """Clean up current_foo_script_set_id that have been removed.
+
+ A node points to the current commissioning, installation, and
+ testing script set. But since there was no foreign key constraint
+ in the database, the corresponding script set might have been removed
+ already.
+ """
+ ScriptSet = apps.get_model("maasserver", "ScriptSet")
+ Node = apps.get_model("maasserver", "Node")
+ Node.objects.exclude(
+ current_testing_script_set__isnull=False,
+ current_testing_script_set__in=ScriptSet.objects.all(),
+ ).update(current_testing_script_set=None)
+ Node.objects.exclude(
+ current_commissioning_script_set__isnull=False,
+ current_commissioning_script_set__in=ScriptSet.objects.all(),
+ ).update(current_commissioning_script_set=None)
+ Node.objects.exclude(
+ current_installation_script_set__isnull=False,
+ current_installation_script_set__in=ScriptSet.objects.all(),
+ ).update(current_installation_script_set=None)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("maasserver", "0298_current_script_set_foreign_keys_drop_indexes"),
+ ]
+
+ operations = [
+ migrations.RunPython(clean_up_missing_scriptset_links),
+ ]
diff --git a/src/maasserver/migrations/maasserver/0300_current_script_set_foreign_keys_readd.py b/src/maasserver/migrations/maasserver/0300_current_script_set_foreign_keys_readd.py
new file mode 100644
index 0000000..80c728b
--- /dev/null
+++ b/src/maasserver/migrations/maasserver/0300_current_script_set_foreign_keys_readd.py
@@ -0,0 +1,46 @@
+# Generated by Django 3.2.12 on 2022-11-21 14:04
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("maasserver", "0299_current_script_set_foreign_keys_cleanup"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="node",
+ name="current_commissioning_script_set",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="+",
+ to="maasserver.scriptset",
+ ),
+ ),
+ migrations.AlterField(
+ model_name="node",
+ name="current_installation_script_set",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="+",
+ to="maasserver.scriptset",
+ ),
+ ),
+ migrations.AlterField(
+ model_name="node",
+ name="current_testing_script_set",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="+",
+ to="maasserver.scriptset",
+ ),
+ ),
+ ]
Follow ups