Skip to content
Snippets Groups Projects
Commit 3bdb1a1f authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Prevent empty WorkerConfiguration names

parent ce9a04a3
No related branches found
No related tags found
1 merge request!2384Prevent empty WorkerConfiguration names
...@@ -459,6 +459,9 @@ class Command(BaseCommand): ...@@ -459,6 +459,9 @@ class Command(BaseCommand):
configuration, _ = WorkerConfiguration.objects.get_or_create( configuration, _ = WorkerConfiguration.objects.get_or_create(
worker=Worker.objects.get(versions__id=worker_version_id), worker=Worker.objects.get(versions__id=worker_version_id),
configuration=json.loads(row["configuration"]), configuration=json.loads(row["configuration"]),
# Configuration names are unique, but there are no configuration names in exports,
# so we use the closest unique field we have
defaults={"name": row["configuration_id"]},
) )
return self.local_process.worker_runs.get_or_create( return self.local_process.worker_runs.get_or_create(
......
# Generated by Django 5.0.6 on 2024-07-11 13:47
from django.core.validators import MinLengthValidator
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("process", "0038_remove_repository_worker_rights"),
]
operations = [
# This does not make any change in the database, since validators are only Python-side
migrations.AlterField(
model_name="workerconfiguration",
name="name",
field=models.CharField(
max_length=250,
validators=[MinLengthValidator(1)],
),
),
# Give configurations with empty names some default name, "Configuration <n>"
migrations.RunSQL(
"""
WITH to_update (id, name) AS (
SELECT id, 'Configuration ' || ROW_NUMBER() OVER (PARTITION BY worker_id ORDER BY id)
FROM process_workerconfiguration
WHERE name = ''
)
UPDATE process_workerconfiguration
SET name = to_update.name
FROM to_update
WHERE process_workerconfiguration.id = to_update.id
""",
reverse_sql=migrations.RunSQL.noop,
elidable=True,
),
migrations.AddConstraint(
model_name="workerconfiguration",
constraint=models.CheckConstraint(
check=~models.Q(name=""),
name="worker_configuration_name_not_empty",
violation_error_message="Worker configuration name cannot be empty.",
),
),
]
...@@ -819,7 +819,7 @@ class WorkerVersion(models.Model): ...@@ -819,7 +819,7 @@ class WorkerVersion(models.Model):
class WorkerConfiguration(IndexableModel): class WorkerConfiguration(IndexableModel):
name = models.CharField(max_length=250) name = models.CharField(max_length=250, validators=[MinLengthValidator(1)])
configuration = models.JSONField(default=dict) configuration = models.JSONField(default=dict)
configuration_hash = MD5HashField() configuration_hash = MD5HashField()
archived = models.BooleanField(default=False) archived = models.BooleanField(default=False)
...@@ -844,7 +844,12 @@ class WorkerConfiguration(IndexableModel): ...@@ -844,7 +844,12 @@ class WorkerConfiguration(IndexableModel):
models.CheckConstraint( models.CheckConstraint(
check=models.Q(configuration__typeof="object"), check=models.Q(configuration__typeof="object"),
name="worker_configuration_configuration_objects", name="worker_configuration_configuration_objects",
) ),
models.CheckConstraint(
check=~Q(name=""),
name="worker_configuration_name_not_empty",
violation_error_message="Worker configuration name cannot be empty.",
),
] ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment