[schema] replace the 'log' by a log_file relation to a File (closes #3241606)
authorPaul Tonelli <paul.tonelli@logilab.fr>
Fri, 08 Nov 2013 16:00:37 +0100
changeset 1439 74845de2ee1d
parent 1438 9b6ede3fbe29
child 1440 f22afd087a8f
[schema] replace the 'log' by a log_file relation to a File (closes #3241606) also update tests. As the logs can be big, it is a better idea to handle them using files. It also makes it easier to download them from cubicweb.
_apycotlib/writer.py
migration/3.0.0_Any.py
schema.py
test/unittest_apycot.py
views/testexecution.py
--- a/_apycotlib/writer.py	Tue Apr 22 15:34:39 2014 +0200
+++ b/_apycotlib/writer.py	Fri Nov 08 16:00:37 2013 +0100
@@ -154,9 +154,9 @@
             self._cnxh.http_post(self.instance_url() +
                     'file-upload',
                     files=files,
+                    field='log_file',
                     eid=self.target_eid(),
-                    is_execution_log='',
-                    _cw_fields='eid,is_execution_log,log')
+                    _cw_fields='eid,field,log')
             self._logs_sent = len(log)
 
 
--- a/migration/3.0.0_Any.py	Tue Apr 22 15:34:39 2014 +0200
+++ b/migration/3.0.0_Any.py	Fri Nov 08 16:00:37 2013 +0100
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+from cubicweb import Binary
 
 # TestExecution now have a workflow
 rql('SET WF workflow_of TE, TE default_workflow WF WHERE WF workflow_of P, '
@@ -7,3 +8,22 @@
 
 rename_relation('log_file','execution_archive')
 commit()
+
+# transform log into log_files
+rset = rql('Any X, F WHERE X is IN (TestExecution, CheckResult), X log F')
+add_relation_type('log_file')
+drop_attribute('TestExecution', 'log')
+drop_attribute('CheckResult', 'log')
+
+sync_schema_props_perms()
+
+if confirm('Upgrade all log_files to file objects ?'):
+    for xeid, fcontent in rset:
+        if fcontent is not None:
+            log_file = create_entity('File', data_name=u"log_file.txt",
+                                     data=Binary(fcontent.encode('utf-8')),
+                                     data_encoding='utf-8',
+                                     ask_confirm=False)
+            log_file.cw_set(reverse_log_file=xeid)
+    commit()
+
--- a/schema.py	Tue Apr 22 15:34:39 2014 +0200
+++ b/schema.py	Fri Nov 08 16:00:37 2013 +0100
@@ -280,7 +280,6 @@
                                 _('killed'))
                     )
     branch = String(indexed=True, __permissions__=IMMUTABLE_ATTR_PERMS, required=True)
-    log = String()
 
 
 class CheckResult(EntityType):
@@ -296,7 +295,6 @@
                        )
     starttime = Datetime()
     endtime   = Datetime()
-    log = String()
 
 
 class CheckResultInfo(EntityType):
@@ -361,6 +359,15 @@
                     'repository.')
 
 
+class log_file(RelationDefinition):
+    __permissions__ = BOT_RELATION_PERMS
+    subject = ('TestExecution', 'CheckResult')
+    object = 'File'
+    cardinality = '??'
+    composite = 'subject'
+    inlined = True
+
+
 class execution_archive(RelationDefinition):
     __permissions__ = BOT_RELATION_PERMS
     subject = 'TestExecution'
--- a/test/unittest_apycot.py	Tue Apr 22 15:34:39 2014 +0200
+++ b/test/unittest_apycot.py	Fri Nov 08 16:00:37 2013 +0100
@@ -45,12 +45,12 @@
     def test_writer_log_content(self):
         checks = self.checks
         self.assertEqual(len(checks), 2)
-        self.assertMultiLineEqual(checks.get_entity(0, 0).log, '''\
+        self.assertMultiLineEqual(checks.get_entity(0, 0).log_file[0].data.read(), '''\
 20\t\t\toption=value<br/>
 40\t\t\tbouh<br/>
 50\t\t\tdi&amp;d<br/>
 ''')
-        self.assertMultiLineEqual(checks.get_entity(1, 0).log, '''\
+        self.assertMultiLineEqual(checks.get_entity(1, 0).log_file[0].data.read(), '''\
 10\t/tmp/something\t12\thip<br/>
 20\t/tmp/something\t\thop<br/>
 30\t\t\tmomo
@@ -60,7 +60,9 @@
 
     def test_log_formatting_first_check(self):
         stream = []
-        log_to_html(self.request(), '', self.checks.get_entity(0, 0).log, stream.append)
+        log_to_html(self.request(), '',
+                    self.checks.get_entity(0, 0).log_file[0].data.read(),
+                    stream.append)
         log_html = '\n'.join(stream)
         self.assertWellFormed(self.get_validator(content_type='application/xml'),
                               CW_NAMESPACE_DIV % log_html)
@@ -82,7 +84,9 @@
 
     def test_log_formatting_second_check(self):
         stream = []
-        log_to_html(self.request(), '', self.checks.get_entity(1, 0).log, stream.append)
+        log_to_html(self.request(), '',
+                    self.checks.get_entity(1, 0).log_file[0].data.read(),
+                    stream.append)
         log_html = '\n'.join(stream)
         self.assertWellFormed(self.get_validator(content_type='application/xml'),
                               CW_NAMESPACE_DIV % log_html)
--- a/views/testexecution.py	Tue Apr 22 15:34:39 2014 +0200
+++ b/views/testexecution.py	Fri Nov 08 16:00:37 2013 +0100
@@ -159,8 +159,8 @@
 _pvs.tag_attribute(('TestExecution', 'execution_log'), 'relations')
 _pvdc.tag_attribute(('TestExecution', 'execution_log'), {'vid': 'narval.formated_log',
                                                          'loglevel': 'Error'})
-_pvs.tag_attribute(('TestExecution', 'log'), 'relations')
-_pvdc.tag_attribute(('TestExecution', 'log'), {'vid': 'narval.formated_log'})
+_pvs.tag_subject_of(('TestExecution', 'log_file', '*'), 'relations')
+_pvdc.tag_subject_of(('TestExecution', 'log_file','*'), {'vid': 'narval.formated_log'})
 _pvs.tag_subject_of(('TestExecution', 'using_revision', '*'), 'hidden')
 _pvs.tag_subject_of(('TestExecution', 'using_config', '*'), 'hidden')
 _pvs.tag_subject_of(('TestExecution', 'execution_archive', '*'), 'hidden')
@@ -393,8 +393,8 @@
 
 _pvs.tag_attribute(('CheckResult', 'name'), 'hidden')
 _pvs.tag_attribute(('CheckResult', 'status'), 'hidden')
-_pvs.tag_attribute(('CheckResult', 'log'), 'relations')
-_pvdc.tag_attribute(('CheckResult', 'log'), {'vid': 'narval.formated_log'})
+_pvs.tag_subject_of(('CheckResult', 'log_file','*'), 'relations')
+_pvdc.tag_subject_of(('CheckResult', 'log_file','*'), {'vid': 'narval.formated_log'})
 _pvs.tag_subject_of(('CheckResult', 'during_execution', '*'), 'hidden')
 _pvs.tag_object_of(('*', 'for_check', '*'), 'hidden')