[Monorail]Fix template2field table col

https://12941-0e3a203-tainted-jojwang-dot-monorail-staging.appspot.com/p/monorail/adminLabels
Bug: monorail:
Change-Id: I8dafd46bda7d561b44d88c56f82a0475b62848cf
Reviewed-on: https://chromium-review.googlesource.com/786548
Reviewed-by: Jason Robbins <jrobbins@chromium.org>
Commit-Queue: Joanna Wang <jojwang@chromium.org>
diff --git a/appengine/monorail/schema/alter-table-log.txt b/appengine/monorail/schema/alter-table-log.txt
index 6ed87b3..3b840a9 100644
--- a/appengine/monorail/schema/alter-table-log.txt
+++ b/appengine/monorail/schema/alter-table-log.txt
@@ -1125,3 +1125,8 @@
 
 ALTER TABLE Issue2FieldValue ADD COLUMN url_value VARCHAR(1024);
 ALTER TABLE Issue2FieldValue ADD INDEX (field_id, url_value);
+
+================================================================
+2017-11-22: Add url_value column to Template2FieldValue table.
+
+ALTER TABLE Template2FieldValue ADD COLUMN url_value VARCHAR(1024);
diff --git a/appengine/monorail/schema/tracker.sql b/appengine/monorail/schema/tracker.sql
index 9bdd9b1..8334775 100644
--- a/appengine/monorail/schema/tracker.sql
+++ b/appengine/monorail/schema/tracker.sql
@@ -463,6 +463,7 @@
   str_value VARCHAR(1024),
   user_id INT UNSIGNED,
   date_value INT,
+  url_value VARCHAR(1024),
 
   INDEX (template_id, field_id),
 
diff --git a/appengine/monorail/services/config_svc.py b/appengine/monorail/services/config_svc.py
index 0df7caf..16e8252 100644
--- a/appengine/monorail/services/config_svc.py
+++ b/appengine/monorail/services/config_svc.py
@@ -50,8 +50,8 @@
 TEMPLATE2COMPONENT_COLS = ['template_id', 'component_id']
 TEMPLATE2ADMIN_COLS = ['template_id', 'admin_id']
 TEMPLATE2FIELDVALUE_COLS = [
-    'template_id', 'field_id', 'int_value', 'str_value', 'date_value',
-    'user_id']
+    'template_id', 'field_id', 'int_value', 'str_value', 'user_id',
+    'date_value', 'url_value']
 PROJECTISSUECONFIG_COLS = [
     'project_id', 'statuses_offer_merge', 'exclusive_label_prefixes',
     'default_template_for_developers', 'default_template_for_users',
@@ -954,7 +954,7 @@
       for fv in template.field_values:
         template2fieldvalue_rows.append(
             (template.template_id, fv.field_id, fv.int_value, fv.str_value,
-             fv.user_id or None, fv.date_value))
+             fv.user_id or None, fv.date_value, fv.url_value))
 
     self.template2label_tbl.InsertRows(
         cnxn, TEMPLATE2LABEL_COLS, template2label_rows, ignore=True,
diff --git a/appengine/monorail/services/test/config_svc_test.py b/appengine/monorail/services/test/config_svc_test.py
index 79f6e8f..1a70cf1 100644
--- a/appengine/monorail/services/test/config_svc_test.py
+++ b/appengine/monorail/services/test/config_svc_test.py
@@ -711,7 +711,18 @@
     self.SetUpUpdateWellKnownStatuses_Default(project_id)
     self.cnxn.Commit()
 
-  def SetUpUpdateTemplates_Default(self, project_id):
+  def SetUpUpdateTemplates_Default(
+      self, project_id, component_rows=None, admin_rows=None, fv_rows=None):
+    """Replay for UpdateTemplates.
+
+    template_tbl and template2label_tbl are based on DEFAULT_TEMPLATES
+
+    Args:
+      project_id: the project id
+      component_rows: optional list of template2component_rows
+      admin_rows: optional list of template2admin_rows
+      fv_rows: optional list of template2fieldvalue_rows
+    """
     self.config_service.template_tbl.Select(
       self.cnxn, cols=['id'], project_id=project_id).AndReturn([])
     self.config_service.template2label_tbl.Delete(
@@ -751,9 +762,9 @@
         (1, 'Type-Defect'),
         (1, 'Priority-Medium'),
         ]
-    template2component_rows = []
-    template2admin_rows = []
-    template2fieldvalue_rows = []
+    template2component_rows = component_rows or []
+    template2admin_rows = admin_rows or []
+    template2fieldvalue_rows = fv_rows or []
 
     self.config_service.template2label_tbl.InsertRows(
         self.cnxn, config_svc.TEMPLATE2LABEL_COLS, template2label_rows,
@@ -830,6 +841,18 @@
     self.config_service._UpdateTemplates(self.cnxn, config)
     self.mox.VerifyAll()
 
+  def testUpdateTemplates_CustomFields(self):
+    config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
+    config.templates[0].field_values = [
+        tracker_bizobj.MakeFieldValue(
+            1, None, None, None, None, 'www.google.com', False)]
+    self.SetUpUpdateTemplates_Default(
+        789, fv_rows=[(2, 1, None, None, None, None, 'www.google.com')])
+
+    self.mox.ReplayAll()
+    self.config_service._UpdateTemplates(self.cnxn, config)
+    self.mox.VerifyAll()
+
   def testUpdateWellKnownLabels(self):
     config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
     self.SetUpUpdateWellKnownLabels_Default(789)