Ottomata has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/367435 )

Change subject: Add unique index for any JSONSchema fields named 'id', not all 
fields that look like uuidv1
......................................................................

Add unique index for any JSONSchema fields named 'id', not all fields that look 
like uuidv1

Bug: T171489
Change-Id: I5483aac6ea3b49c75662e6bf15bb65128981e6e1
---
M eventlogging/jrm.py
M tests/test_jrm.py
2 files changed, 44 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/eventlogging 
refs/changes/35/367435/1

diff --git a/eventlogging/jrm.py b/eventlogging/jrm.py
index 3fe3ef5..14bc983 100644
--- a/eventlogging/jrm.py
+++ b/eventlogging/jrm.py
@@ -181,23 +181,51 @@
         False: {'nullable': True}
     }),
     ('pattern', {
-        # UUID v1 pattern, make a unique index.  T170925
+        # UUID v1 pattern, make an index.  T170925
         '^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$': {
             'type_': sqlalchemy.CHAR(38),
             'index': True,
-            'unique': True
         },
-    })
+    }),
 ))
 
 
-def typecast(property):
-    """Generates a SQL column definition from a JSON Schema property
-    specifier."""
+# Special case field name options.
+# If given a field name that matches a key here, the
+# options will be added for the column.
+name_mappers = {
+    # Any JSONSchema fields named 'id' should be unique. T171489
+    'id': {
+        'index': True,
+        'unique': True
+    }
+}
+
+print(mappers)
+
+def typecast(property, name=None):
+    """
+    Generates a SQL column definition from a JSON Schema property
+    specifier.
+
+    :param property: JSONSchema property
+    :param name:     JSONSchema field name (optional)
+    """
     options = COLUMN_DEFAULTS.copy()
+    # jsonschema attribute -> jsonschema attribute value -> sqlalchmey option,
     for attribute, mapping in items(mappers):
+        # Get the jsonschema attribute value from the jsonschema property
         value = property.get(attribute)
+        # if this attribute's sqlachemy option mapping contains a setting
+        # for this value, update the options.
         options.update(mapping.get(value, ()))
+
+    # field names are the most specific sqlalchmey option,
+    # update the options if the name_mappers
+    # has a field name key that matches this properties' name.
+    if name and name in name_mappers:
+        options.update(name_mappers[name])
+
     return sqlalchemy.Column(**options)
 
 
@@ -359,7 +387,7 @@
         if 'properties' in val:
             val = val['properties']
         elif 'type' in val:
-            val = typecast(val)
+            val = typecast(val, key)
     return key, val
 
 
diff --git a/tests/test_jrm.py b/tests/test_jrm.py
index a231a00..c9c33a7 100644
--- a/tests/test_jrm.py
+++ b/tests/test_jrm.py
@@ -93,6 +93,15 @@
         cols = {column.name for index in t.indexes for column in index.columns}
         self.assertIn('meta_id', cols)
 
+    def test_meta_id_unique_index_creation(self):
+        """A field named ``id`` (e.g. meta.id) should have a unique index."""
+        table_name = event_to_table_name(self.event_with_meta)
+        t = eventlogging.jrm.declare_table(
+            self.meta, TEST_META_SCHEMA_SCID, table_name, 
should_encapsulate=False
+        )
+        cols = {column.name for index in t.indexes for column in index.columns 
if index.unique}
+        self.assertIn('meta_id', cols)
+
     def test_flatten(self):
         """``flatten`` correctly collapses deeply nested maps."""
         flat = eventlogging.utils.flatten(self.event)

-- 
To view, visit https://gerrit.wikimedia.org/r/367435
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5483aac6ea3b49c75662e6bf15bb65128981e6e1
Gerrit-PatchSet: 1
Gerrit-Project: eventlogging
Gerrit-Branch: master
Gerrit-Owner: Ottomata <ao...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to