Comment 3 for bug 2044851

Revision history for this message
Anton Troyanov (troyanov) wrote :

I don't think it is related to a null `sip` but rather a non-existing `maasserver_dnsresource` leading to an empty `domain.name`.

The only place where we have `sys_dns_dnsresource_ip_unlink` trigger is:
```
register_procedure(DNS_DNSRESOURCE_IP_UNLINK)
register_trigger(
    "maasserver_dnsresource_ip_addresses",
    "sys_dns_dnsresource_ip_unlink",
    "delete",
)
```

And the only thing that might lead to the failure is a null value of `domain.name` (since `sip.ip` is guarded):
```
IF sip.ip IS NOT NULL THEN
      PERFORM sys_dns_publish_update(
        'ip ' || host(sip.ip) || ' unlinked from resource ' ||
        COALESCE(resource.name, 'NULL') || ' on zone ' || domain.name);
  END IF;
```

While `name` itself cannot be null according to the schema, the following query can produce no results, because `OLD.dnsresource_id` is pointing to `maasserver_dnsresource` that was removed:
```
SELECT maasserver_dnsresource.* INTO resource
FROM maasserver_dnsresource
WHERE maasserver_dnsresource.id = OLD.dnsresource_id;
SELECT maasserver_domain.* INTO domain
FROM maasserver_domain
WHERE maasserver_domain.id = resource.domain_id;
```

Bcause trigger is executed when we *delete* row from `maasserver_dnsresource_ip_addresses`, but there are no FK relation, so `maasserver_dnsresource_ip_addresses` might end up having an orphaned `dnsresource_id`

Here is an example orphaned row found in [0]
```
select *
from maasserver_dnsresource_ip_addresses t1
left join maasserver_dnsresource t2 on t1.dnsresource_id = t2.id
where t2.name is null;

 id | dnsresource_id | staticipaddress_id | id | created | updated | name | domain_id | address_ttl
----+----------------+--------------------+----+---------+---------+------+-----------+-------------
 12 | 8 | 27 | | | | | |
(1 row)
```

[0] https://solutions.qa.canonical.com/testruns/859d8396-5fe0-4c85-9fbd-26cce6ffdee7