Repository: apache/airflow

Issue:
Description Field for Variables
add text column to explain what the variable is used for

same as https://github.com/apache/airflow/issues/10840 just for Variables.




Discussion:
@abhilash1in  what do you think?
That'll be useful, but I don't have the bandwidth for it right now, sorry. 
@kurtqq If you want to take a stab at it, I'll be happy to help.
@abhilash1in @eladkal I'm willing to dive into this as my first attempt to contribute if there is still interest. I'll probably need some help to start though. I think it's a bit more tricky than the Connection description as the Variables are mostly handled as a key-value pair.
@itroulli why is it tricky? it's description per variable. it doesn't matter if the variable is text or key-value. the idea is to give general information to what the variable is used/who created it and stuff in that area.
@kurtqq Sorry for the delay, I've been busy the past few days.

I had in mind some pieces of code like the import/export functions that treat variables as a key/value pair in which it would be required to change the struct to be able to import/export the description too. I might be wrong and have misunderstood something though. I'll try to give it a shot as soon as I find some time (first by skipping the description in functions like the import/export) and see what happens.
@itroulli I see the description field as a column in the DB that we are able to edit in the UI. This is a meta column that just give the ability to document why the variable was created and stuff like that.
I don't think it should involved the key,value properties of how you access to variables. So if you thinking about doing something like changing`Variable.get()` I don't think this is necessary. 
@kurtqq Tbh, that was indeed what I had in mind. So, I'll try to understand now, how the UI will be storing and retrieving the description content.
According to my understanding, I'll have to create a file in `airflow/migrations/versions/` to add a column to the `variables` table but I'm not sure that I get how these files in the migrations folder work. Could you provide some resources? 
Thanks in advance!
check https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#metadata-database-updates

Resolving patch:
diff --git a/airflow/migrations/versions/e165e7455d70_add_description_field_to_variable.py b/airflow/migrations/versions/e165e7455d70_add_description_field_to_variable.py
new file mode 100644
--- /dev/null
+++ b/airflow/migrations/versions/e165e7455d70_add_description_field_to_variable.py
@@ -0,0 +1,46 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""add description field to variable
+
+Revision ID: e165e7455d70
+Revises: 90d1635d7b86
+Create Date: 2021-04-11 22:28:02.107290
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = 'e165e7455d70'
+down_revision = '90d1635d7b86'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    """Apply add description field to variable"""
+    with op.batch_alter_table('variable', schema=None) as batch_op:
+        batch_op.add_column(sa.Column('description', sa.Text(), nullable=True))
+
+
+def downgrade():
+    """Unapply add description field to variable"""
+    with op.batch_alter_table('variable', schema=None) as batch_op:
+        batch_op.drop_column('description')
diff --git a/airflow/models/variable.py b/airflow/models/variable.py
--- a/airflow/models/variable.py
+++ b/airflow/models/variable.py
@@ -47,12 +47,14 @@ class Variable(Base, LoggingMixin):
     id = Column(Integer, primary_key=True)
     key = Column(String(ID_LEN), unique=True)
     _val = Column('val', Text)
+    description = Column(Text)
     is_encrypted = Column(Boolean, unique=False, default=False)
 
-    def __init__(self, key=None, val=None):
+    def __init__(self, key=None, val=None, description=None):
         super().__init__()
         self.key = key
         self.val = val
+        self.description = description
 
     def __repr__(self):
         # Hiding the value
diff --git a/airflow/www/views.py b/airflow/www/views.py
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -3151,9 +3151,9 @@ class VariableModelView(AirflowModelView):
         permissions.ACTION_CAN_ACCESS_MENU,
     ]
 
-    list_columns = ['key', 'val', 'is_encrypted']
-    add_columns = ['key', 'val']
-    edit_columns = ['key', 'val']
+    list_columns = ['key', 'val', 'description', 'is_encrypted']
+    add_columns = ['key', 'val', 'description']
+    edit_columns = ['key', 'val', 'description']
     search_columns = ['key', 'val']
 
     base_order = ('key', 'asc')


Test patch: