Add a migration function in order to relocate bfss
authorNoe Gaumont <ngaumont@logilab.fr>
Wed, 16 Oct 2019 14:49:38 +0200
changeset 12742 ca698656251c
parent 12741 90348f847b4b
child 12743 a74e77469540
Add a migration function in order to relocate bfss Related to https://www.cubicweb.org/ticket/1903304
cubicweb/server/migractions.py
cubicweb/server/test/unittest_migractions.py
doc/book/devrepo/migration.rst
doc/changes/3.27.rst
--- a/cubicweb/server/migractions.py	Wed Jun 12 19:58:00 2019 +0200
+++ b/cubicweb/server/migractions.py	Wed Oct 16 14:49:38 2019 +0200
@@ -1390,6 +1390,25 @@
         from cubicweb.server.checkintegrity import reindex_entities
         reindex_entities(self.repo.schema, self.cnx, etypes=etypes)
 
+    def cmd_update_bfss_path(self, old_path, new_path, commit=True):
+        """
+        Change the path of all Files from old_path to new_path.
+        """
+        changes = []
+        for f_eid, fspath in self.rqlexec(
+                'Any F, FSPATH(D) WHERE F is File, F data D'):
+            fspath = fspath.getvalue().decode('utf-8')
+            dirname = os.path.dirname(fspath)
+            if dirname == old_path:
+                newpath = os.path.join(new_path, os.path.basename(fspath))
+                changes.append({'expected': newpath, 'eid': f_eid})
+        self.repo.system_source.doexecmany(
+            self.cnx,
+            'UPDATE cw_file SET cw_data=%(expected)s WHERE cw_eid=%(eid)s',
+            changes)
+        if commit:
+            self.commit()
+
     @contextmanager
     def cmd_dropped_constraints(self, etype, attrname, cstrtype=None,
                                 droprequired=False):
--- a/cubicweb/server/test/unittest_migractions.py	Wed Jun 12 19:58:00 2019 +0200
+++ b/cubicweb/server/test/unittest_migractions.py	Wed Oct 16 14:49:38 2019 +0200
@@ -37,6 +37,7 @@
 from cubicweb.server.migractions import ServerMigrationHelper
 from cubicweb.server.sources import storages
 from cubicweb.server.schema2sql import build_index_name
+from cubicweb.server.test.unittest_storage import StorageTC
 
 import cubicweb.devtools
 
@@ -834,6 +835,20 @@
                 storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
 
 
+class MigrationStorageCommandsTC(StorageTC, MigrationCommandsTC):
+
+    def test_change_bfss_path(self):
+        with self.mh() as (cnx, mh):
+            file1 = mh.cmd_create_entity('File', data_name=u"foo.pdf",
+                                         data=Binary(b"xxx"), data_format=u'text/plain')
+            mh.commit()
+            current_dir = osp.dirname(self.fspath(cnx, file1))
+
+            mh.update_bfss_path(current_dir, 'loutre', commit=True)
+
+            self.assertEqual(u'loutre', osp.dirname(self.fspath(cnx, file1)))
+
+
 class MigrationCommandsComputedTC(MigrationTC):
     """ Unit tests for computed relations and attributes
     """
--- a/doc/book/devrepo/migration.rst	Wed Jun 12 19:58:00 2019 +0200
+++ b/doc/book/devrepo/migration.rst	Wed Oct 16 14:49:38 2019 +0200
@@ -164,6 +164,9 @@
 * `set_size_constraint(etype, rtype, size, commit=True)`, changes the size constraints
   for the relation <rtype> of entity type <etype>.
 
+* `update_bfss_path(old_path, new_path, commit=True)`, change the path from `old_path` to
+  `new_path` in Bytes File-System Storage (bfss).
+
 Data migration
 --------------
 The following functions for data migration are available in `repository` scripts:
--- a/doc/changes/3.27.rst	Wed Jun 12 19:58:00 2019 +0200
+++ b/doc/changes/3.27.rst	Wed Oct 16 14:49:38 2019 +0200
@@ -27,6 +27,9 @@
   default from now on. To still log to a file pass ``log_to_file=True`` to
   ``CubicWebConfiguration.config_for``
 
+* add a new migration function `update_bfss_path(old_path, new_path)` to update
+  the path in Bytes File-System Storage (bfss).
+
 Backwards incompatible changes
 ------------------------------