Repository: mathesar-foundation/mathesar

Issue:
Description for DE queries
We want a DE query to have a description attribute that can be saved by the frontend.

- Add description attribute to `UIQuery` model
- Should be possible to get, create, patch with that
- add/run migrations


Discussion:

Resolving patch:
diff --git a/mathesar/migrations/0007_auto_20221115_0627.py b/mathesar/migrations/0007_auto_20221115_0627.py
new file mode 100644
--- /dev/null
+++ b/mathesar/migrations/0007_auto_20221115_0627.py
@@ -0,0 +1,34 @@
+# Generated by Django 3.1.14 on 2022-11-15 06:27
+
+from django.db import migrations, models
+import mathesar.models.query
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('mathesar', '0006_auto_20221110_1152'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='uiquery',
+            name='description',
+            field=models.TextField(blank=True, null=True),
+        ),
+        migrations.AlterField(
+            model_name='uiquery',
+            name='display_options',
+            field=models.JSONField(blank=True, null=True, validators=[mathesar.models.query._get_validator_for_dict]),
+        ),
+        migrations.AlterField(
+            model_name='uiquery',
+            name='initial_columns',
+            field=models.JSONField(validators=[mathesar.models.query._get_validator_for_list_of_dicts, mathesar.models.query._get_validator_for_initial_columns]),
+        ),
+        migrations.AlterField(
+            model_name='uiquery',
+            name='transformations',
+            field=models.JSONField(blank=True, null=True, validators=[mathesar.models.query._get_validator_for_list_of_dicts, mathesar.models.query._get_validator_for_transformations]),
+        ),
+    ]
diff --git a/mathesar/models/query.py b/mathesar/models/query.py
--- a/mathesar/models/query.py
+++ b/mathesar/models/query.py
@@ -120,6 +120,11 @@ class UIQuery(BaseModel, Relation):
         unique=True,
     )
 
+    description = models.TextField(
+        null=True,
+        blank=True
+    )
+
     base_table = models.ForeignKey(
         'Table', on_delete=models.CASCADE, related_name='queries'
     )


Test patch:
diff --git a/mathesar/tests/api/query/test_query_api.py b/mathesar/tests/api/query/test_query_api.py
--- a/mathesar/tests/api/query/test_query_api.py
+++ b/mathesar/tests/api/query/test_query_api.py
@@ -24,6 +24,28 @@ def post_minimal_query(_post_query, create_patents_table, get_uid):
     return _post_query(request_data)
 
 
+@pytest.fixture
+def post_query_with_description(_post_query, create_patents_table, get_uid):
+    base_table = create_patents_table(table_name=get_uid())
+    request_data = {
+        "name": get_uid(),
+        "description": "A generic description",
+        "base_table": base_table.id,
+        "initial_columns": [
+            {
+                "id": 1,
+                "jp_path": [[1, 3], [4, 5]],
+                "alias": "alias_x",
+            },
+            {
+                "id": 2,
+                "alias": "alias_y",
+            },
+        ],
+    }
+    return _post_query(request_data)
+
+
 @pytest.fixture
 def _post_query(client):
     def _f(request_data):
@@ -299,3 +321,21 @@ def _deep_equality_assert(
             )
     else:
         assert expected == actual
+
+
+def test_create_with_description(post_query_with_description):
+    request_data, response = post_query_with_description
+    response_json = response.json()
+    assert response_json['description'] == "A generic description"
+
+
+def test_update_description(post_query_with_description, client):
+    post_data, response = post_query_with_description
+    response_json = response.json()
+    query_id = response_json['id']
+    patch_data = {
+        "description": "A new description"
+    }
+    response = client.patch(f'/api/db/v0/queries/{query_id}/', data=patch_data)
+    response_json = response.json()
+    assert response_json['description'] == "A new description"