Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/130290

Change subject: Various fixes for the JSON-relational mapper
......................................................................

Various fixes for the JSON-relational mapper

* Update EventCapsule schema to 8326736.
* Infer uniqueness of UUID from the property's format specifier.
* Factor out column sort into separate function.
* Make deeply-nested properties sort after shallow-nested ones.
* Fix nullable constraint detection.

Change-Id: I36002e62f570f418bd59dd1fb5ace2a23514b098
---
M server/eventlogging/jrm.py
M server/eventlogging/schema.py
M server/tests/fixtures.py
3 files changed, 25 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/90/130290/1

diff --git a/server/eventlogging/jrm.py b/server/eventlogging/jrm.py
index 1cc46e5..c493685 100644
--- a/server/eventlogging/jrm.py
+++ b/server/eventlogging/jrm.py
@@ -90,10 +90,12 @@
     }),
     ('format', {
         'utc-millisec': {'type_': MediaWikiTimestamp, 'index': True},
+        'uuid5-hex': {'type_': sqlalchemy.CHAR(32), 'index': True,
+                      'unique': True},
     }),
     ('required', {
-        True: {'nullable': True},
-        False: {'nullable': False}
+        True: {'nullable': False},
+        False: {'nullable': True}
     })
 ))
 
@@ -151,15 +153,7 @@
     the database, issue ``CREATE TABLE`` statement."""
     schema = get_schema(scid, encapsulate=True)
 
-    # Every table gets an integer auto-increment primary key column `id`
-    # and an indexed CHAR(32) column, `uuid`. (UUIDs could be stored as
-    # binary in a CHAR(16) column, but at the cost of readability.)
-    columns = [
-        sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
-        sqlalchemy.Column('uuid', sqlalchemy.CHAR(32), index=True,
-                          unique=True, nullable=False)
-    ]
-    columns.extend(schema_mapper(schema))
+    columns = schema_mapper(schema)
 
     table_options = ENGINE_TABLE_OPTIONS.get(meta.bind.name, {})
     table_name = TABLE_NAME_FORMAT % scid
@@ -214,18 +208,29 @@
     return dict(flat)
 
 
+def column_sort_key(column):
+    """Sort key for column names. 'id' and 'uuid' come first, then the
+    top-level properties in alphabetical order, followed by the nested
+    properties (identifiable by the presence of an underscore)."""
+    return (
+        ('id', 'uuid', column.name).index(column.name),
+        column.name.count('_'),
+        column.name,
+    )
+
+
 def schema_mapper(schema):
     """Takes a schema and map its properties to database column
     definitions."""
     properties = {k: v for k, v in items(schema.get('properties', {}))
                   if k not in NO_DB_PROPERTIES}
-    columns = []
+
+    # Every table gets an integer auto-increment primary key column `id`
+    columns = [sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True)]
+
     for name, col in items(flatten(properties, f=_property_getter)):
         col.name = name
         columns.append(col)
 
-    # Sort the mapped columns lexicographically by name, with 'nested'
-    # columns (identifiable by the presence of an underscore in the
-    # name) appearing last.
-    columns.sort(key=lambda col: ('_' in col.name, col.name))
+    columns.sort(key=column_sort_key)
     return columns
diff --git a/server/eventlogging/schema.py b/server/eventlogging/schema.py
index 67dd318..9e77409 100644
--- a/server/eventlogging/schema.py
+++ b/server/eventlogging/schema.py
@@ -29,7 +29,7 @@
 schema_cache = {}
 
 #: SCID of the metadata object which wraps each event.
-CAPSULE_SCID = ('EventCapsule', 6862080)
+CAPSULE_SCID = ('EventCapsule', 8326736)
 
 
 def get_schema(scid, encapsulate=False):
diff --git a/server/tests/fixtures.py b/server/tests/fixtures.py
index 75a4c9d..7e4a6ef 100644
--- a/server/tests/fixtures.py
+++ b/server/tests/fixtures.py
@@ -62,7 +62,9 @@
                 'format': 'utc-millisec'
             },
             'uuid': {
-                'type': 'string'
+                'type': 'string',
+                'required': True,
+                'format': 'uuid5-hex'
             }
         },
         'additionalProperties': False

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I36002e62f570f418bd59dd1fb5ace2a23514b098
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

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

Reply via email to