Skip to content

Commit 85e8cbc

Browse files
authored
[change] Changed "DeviceConnection.failure_reason" field to TextField #298
Changed "openwisp_controller.connection.base.models.DeviceConnection" to "TextField" from "CharField" with limited size. This avoids possible exception if failed_reason is very long, which may happen in some corner cases. Fixes #298
1 parent 8e0faee commit 85e8cbc

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

openwisp_controller/connection/base/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ class AbstractDeviceConnection(ConnectorMixin, TimeStampedEditableModel):
181181
)
182182
# usability improvements
183183
is_working = models.BooleanField(null=True, blank=True, default=None)
184-
failure_reason = models.CharField(
185-
_('reason of failure'), max_length=128, blank=True
186-
)
184+
failure_reason = models.TextField(_('reason of failure'), blank=True)
187185
last_attempt = models.DateTimeField(blank=True, null=True)
188186

189187
class Meta:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.db import migrations, models
2+
3+
4+
def truncate_failure_reason(apps, schema_editor):
5+
DeviceConnection = apps.get_model('connection', 'DeviceConnection')
6+
7+
for device_connection in DeviceConnection.objects.iterator():
8+
device_connection.failure_reason = device_connection.failure_reason[:128]
9+
device_connection.save()
10+
11+
12+
class Migration(migrations.Migration):
13+
14+
dependencies = [
15+
('connection', '0004_django3_1_upgrade'),
16+
]
17+
18+
operations = [
19+
migrations.AlterField(
20+
model_name='deviceconnection',
21+
name='failure_reason',
22+
field=models.TextField(blank=True, verbose_name='reason of failure'),
23+
),
24+
migrations.RunPython(migrations.RunPython.noop, truncate_failure_reason,),
25+
]

tests/openwisp2/sample_connection/migrations/0001_initial.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ class Migration(migrations.Migration):
176176
),
177177
(
178178
'failure_reason',
179-
models.CharField(
180-
blank=True, max_length=128, verbose_name='reason of failure'
181-
),
179+
models.TextField(blank=True, verbose_name='reason of failure'),
182180
),
183181
('last_attempt', models.DateTimeField(blank=True, null=True)),
184182
('details', models.CharField(blank=True, max_length=64, null=True)),

0 commit comments

Comments
 (0)