← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~ack/maas:rdns-hostnames-use-array-field into maas:master

 

Alberto Donato has proposed merging ~ack/maas:rdns-hostnames-use-array-field into maas:master.

Commit message:
use array instead of generic JSON list to store RDNS hostnames



Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~ack/maas/+git/maas/+merge/435962
-- 
Your team MAAS Committers is subscribed to branch maas:master.
diff --git a/src/maasserver/migrations/maasserver/0291_rdns_hostnames_as_array.py b/src/maasserver/migrations/maasserver/0291_rdns_hostnames_as_array.py
new file mode 100644
index 0000000..2c9bf23
--- /dev/null
+++ b/src/maasserver/migrations/maasserver/0291_rdns_hostnames_as_array.py
@@ -0,0 +1,43 @@
+# Generated by Django 3.2.12 on 2023-01-18 12:06
+
+import django.contrib.postgres.fields
+from django.db import migrations, models
+
+# First, convert the JSON-formatted list to an array-formatted one. The
+# AlterField at the end actually converts the column type with the default
+# text[] cast, which now works as the content is a valid text array.
+QUERIES = (
+    """
+    CREATE FUNCTION j_to_a(content text) RETURNS text[] AS $$
+    DECLARE
+      result text[] = '{}'::text[];
+    BEGIN
+      IF content != '' THEN
+        SELECT array_agg(sub.r) INTO result
+        FROM (SELECT json_array_elements_text(content::json) AS r) AS sub;
+      END IF;
+      RETURN result;
+    END;
+    $$ LANGUAGE plpgsql
+    """,
+    "UPDATE maasserver_rdns SET hostnames = j_to_a(hostnames)",
+    "DROP FUNCTION j_to_a",
+)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("maasserver", "0290_migrate_node_power_parameters"),
+    ]
+
+    operations = [
+        *(migrations.RunSQL(query) for query in QUERIES),
+        migrations.AlterField(
+            model_name="rdns",
+            name="hostnames",
+            field=django.contrib.postgres.fields.ArrayField(
+                base_field=models.TextField(), default=list, size=None
+            ),
+        ),
+    ]
diff --git a/src/maasserver/models/rdns.py b/src/maasserver/models/rdns.py
index c4bfe08..d049c76 100644
--- a/src/maasserver/models/rdns.py
+++ b/src/maasserver/models/rdns.py
@@ -6,15 +6,16 @@
 
 from typing import List
 
+from django.contrib.postgres.fields import ArrayField
 from django.db.models import (
     CASCADE,
     CharField,
     ForeignKey,
     GenericIPAddressField,
     Manager,
+    TextField,
 )
 
-from maasserver.fields import JSONObjectField
 from maasserver.models.cleansave import CleanSave
 from maasserver.models.timestampedmodel import TimestampedModel
 from provisioningserver.logger import LegacyLogger
@@ -125,14 +126,12 @@ class RDNS(CleanSave, TimestampedModel):
     # more than one entry, we'll need to make an educated guess as to which
     # is the "primary".) This will be coalesced with the other data in the
     # discovery view to present the default hostname for the IP.
-    hostname = CharField(
-        max_length=256, editable=True, null=True, blank=False, unique=False
-    )
+    hostname = CharField(max_length=256, null=True)
 
     # List of all hostnames returned by the lookup. (Useful for
     # support/debugging, in case we guess incorrectly about the "primary"
     # hostname -- and in case we ever want to show them all.)
-    hostnames = JSONObjectField()
+    hostnames = ArrayField(TextField(), default=list)
 
     # Region controller that observed the hostname.
     observer = ForeignKey(

Follow ups