[PATCH 1 of 2 cubicweb] [migrations/pdb] add to every failing migration operation a "p(db)" option

Laurent Peuch cortex at worlddomination.be
Fri Oct 18 05:11:44 CEST 2019


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1558564411 -7200
#      Thu May 23 00:33:31 2019 +0200
# Node ID e5f34c8b3a5929dbd7ee58411ea4fb8fecb957e0
# Parent  5c432a7fc442e170c9005666131b01a88a5c9334
# Available At https://hg.logilab.org/users/lpeuch/cubicweb
#              hg pull https://hg.logilab.org/users/lpeuch/cubicweb -r e5f34c8b3a59
# EXP-Topic improve-migrate-command
[migrations/pdb] add to every failing migration operation a "p(db)" option

Instead, the migration command will just crash without offering the possibility
of the user to debug or continue the migration.

Closes #17219772

diff --git a/cubicweb/server/migractions.py b/cubicweb/server/migractions.py
--- a/cubicweb/server/migractions.py
+++ b/cubicweb/server/migractions.py
@@ -38,6 +38,7 @@ from datetime import datetime
 from glob import glob
 from copy import copy
 from contextlib import contextmanager
+from functools import wraps
 
 from logilab.common.decorators import cached, clear_cache
 
@@ -76,6 +77,22 @@ class ClearGroupMap(hook.Hook):
             repo.vreg.register(ClearGroupMap)
 
 
+def ask_abort_with_pdb(function):
+    @wraps(function)
+    def _wrap(self, *args, **kwargs):
+        try:
+            to_return = function(self, *args, **kwargs)
+        except Exception:
+            _, ex, traceback_ = sys.exc_info()
+            traceback.print_exc()
+            if self.confirm('abort?', pdb=True, traceback=traceback_):
+                raise
+        else:
+            return to_return
+
+    return _wrap
+
+
 class ServerMigrationHelper(MigrationHelper):
     """specific migration helper for server side migration scripts,
     providing actions related to schema/data migration
@@ -623,9 +640,11 @@ class ServerMigrationHelper(MigrationHel
         if not ask_confirm or self.confirm('Commit now ?', shell=False):
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_add_cube(self, cube, update_database=True):
         self.cmd_add_cubes((cube,), update_database)
 
+    @ask_abort_with_pdb
     def cmd_add_cubes(self, cubes, update_database=True):
         """update_database is telling if the database schema should be updated
         or if only the relevant eproperty should be inserted (for the case where
@@ -687,6 +706,7 @@ class ServerMigrationHelper(MigrationHel
                 self.cmd_exec_event_script('postcreate', cube)
                 self.commit()
 
+    @ask_abort_with_pdb
     def cmd_drop_cube(self, cube, removedeps=False):
         removedcubes = super(ServerMigrationHelper, self).cmd_drop_cube(
             cube, removedeps)
@@ -724,6 +744,7 @@ class ServerMigrationHelper(MigrationHel
 
     # schema migration actions ################################################
 
+    @ask_abort_with_pdb
     def cmd_add_attribute(self, etype, attrname, attrtype=None, commit=True):
         """add a new attribute on the given entity type"""
         if attrtype is None:
@@ -731,6 +752,7 @@ class ServerMigrationHelper(MigrationHel
             attrtype = rschema.objects(etype)[0]
         self.cmd_add_relation_definition(etype, attrname, attrtype, commit=commit)
 
+    @ask_abort_with_pdb
     def cmd_drop_attribute(self, etype, attrname, commit=True):
         """drop an existing attribute from the given entity type
 
@@ -746,6 +768,7 @@ class ServerMigrationHelper(MigrationHel
             self.cmd_drop_relation_definition(etype, attrname, attrtype,
                                               commit=commit)
 
+    @ask_abort_with_pdb
     def cmd_rename_attribute(self, etype, oldname, newname, commit=True):
         """rename an existing attribute of the given entity type
 
@@ -769,6 +792,7 @@ class ServerMigrationHelper(MigrationHel
         # fulltextindexed change...)
         self.cmd_drop_attribute(etype, oldname, commit=commit)
 
+    @ask_abort_with_pdb
     def cmd_add_entity_type(self, etype, auto=True, commit=True):
         """register a new entity type
 
@@ -902,6 +926,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_drop_entity_type(self, etype, commit=True):
         """Drop an existing entity type.
 
@@ -916,6 +941,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_rename_entity_type(self, oldname, newname, attrs=None, commit=True):
         """rename an existing entity type in the persistent schema
 
@@ -1007,6 +1033,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_add_relation_type(self, rtype, addrdef=True, commit=True):
         """register a new relation type named `rtype`, as described in the
         schema description file.
@@ -1064,6 +1091,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_drop_relation_type(self, rtype, commit=True):
         """Drop an existing relation type.
 
@@ -1077,6 +1105,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_rename_relation_type(self, oldname, newname, commit=True, force=False):
         """rename an existing relation
 
@@ -1096,6 +1125,7 @@ class ServerMigrationHelper(MigrationHel
                          ask_confirm=self.verbosity >= 2)
         self.cmd_drop_relation_type(oldname, commit=commit)
 
+    @ask_abort_with_pdb
     def cmd_add_relation_definition(self, subjtype, rtype, objtype, commit=True):
         """register a new relation definition, from its definition found in the
         schema definition file
@@ -1129,6 +1159,7 @@ class ServerMigrationHelper(MigrationHel
                     '%s has no eid while adding %s' % (schemaobj, rdef)
         return rdef
 
+    @ask_abort_with_pdb
     def cmd_drop_relation_definition(self, subjtype, rtype, objtype, commit=True):
         """Drop an existing relation definition.
 
@@ -1151,6 +1182,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_sync_schema_props_perms(self, ertype=None, syncperms=True,
                                     syncprops=True, syncrdefs=True, commit=True):
         """synchronize the persistent schema against the current definition
@@ -1196,6 +1228,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_change_relation_props(self, subjtype, rtype, objtype,
                                   commit=True, **kwargs):
         """change some properties of a relation definition
@@ -1219,6 +1252,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_set_size_constraint(self, etype, rtype, size, commit=True):
         """set change size constraint of a string attribute
 
@@ -1259,12 +1293,14 @@ class ServerMigrationHelper(MigrationHel
 
     # Workflows handling ######################################################
 
+    @ask_abort_with_pdb
     def cmd_make_workflowable(self, etype):
         """add workflow relations to an entity type to make it workflowable"""
         self.cmd_add_relation_definition(etype, 'in_state', 'State')
         self.cmd_add_relation_definition(etype, 'custom_workflow', 'Workflow')
         self.cmd_add_relation_definition('TrInfo', 'wf_info_for', etype)
 
+    @ask_abort_with_pdb
     def cmd_add_workflow(self, name, wfof, default=True, commit=False,
                          ensure_workflowable=True, **kwargs):
         """
@@ -1308,6 +1344,7 @@ class ServerMigrationHelper(MigrationHel
             self.commit()
         return wf
 
+    @ask_abort_with_pdb
     def cmd_get_workflow_for(self, etype):
         """return default workflow for the given entity type"""
         rset = self.rqlexec('Workflow X WHERE ET default_workflow X, ET name %(et)s',
@@ -1327,6 +1364,7 @@ class ServerMigrationHelper(MigrationHel
             {'k': pkey}, ask_confirm=False)
         return rset[0][0]
 
+    @ask_abort_with_pdb
     def cmd_set_property(self, pkey, value):
         """set the site-wide persistent property value for the given key to the
         given value.
@@ -1346,6 +1384,7 @@ class ServerMigrationHelper(MigrationHel
 
     # other data migration commands ###########################################
 
+    @ask_abort_with_pdb
     def cmd_storage_changed(self, etype, attribute):
         """migrate entities to a custom storage. The new storage is expected to
         be set, it will be temporarily removed for the migration.
@@ -1367,6 +1406,7 @@ class ServerMigrationHelper(MigrationHel
         print()
         source.set_storage(etype, attribute, storage)
 
+    @ask_abort_with_pdb
     def cmd_create_entity(self, etype, commit=False, **kwargs):
         """add a new entity of the given type"""
         entity = self.cnx.create_entity(etype, **kwargs)
@@ -1374,10 +1414,12 @@ class ServerMigrationHelper(MigrationHel
             self.commit()
         return entity
 
+    @ask_abort_with_pdb
     def cmd_find(self, etype, **kwargs):
         """find entities of the given type and attribute values"""
         return self.cnx.find(etype, **kwargs)
 
+    @ask_abort_with_pdb
     def cmd_update_etype_fti_weight(self, etype, weight):
         if self.repo.system_source.dbdriver == 'postgres':
             self.sqlexec('UPDATE appears SET weight=%(weight)s '
@@ -1385,6 +1427,7 @@ class ServerMigrationHelper(MigrationHel
                          'WHERE X.eid=appears.uid AND X.type=%(type)s',
                          {'type': etype, 'weight': weight}, ask_confirm=False)
 
+    @ask_abort_with_pdb
     def cmd_reindex_entities(self, etypes=None):
         """force reindexaction of entities of the given types or of all
         indexable entity types
@@ -1494,6 +1537,7 @@ class ServerMigrationHelper(MigrationHel
 
     # low-level commands to repair broken system database ######################
 
+    @ask_abort_with_pdb
     def cmd_change_attribute_type(self, etype, attr, newtype, commit=True):
         """low level method to change the type of an entity attribute. This is
         a quick hack which has some drawback:
@@ -1527,6 +1571,7 @@ class ServerMigrationHelper(MigrationHel
             del rschema.rdefs[(eschema, oldschema)]
             rschema.rdefs[(eschema, newschema)] = rdef
 
+    @ask_abort_with_pdb
     def cmd_add_entity_type_table(self, etype, commit=True):
         """low level method to create the sql table for an existing entity.
         This may be useful on accidental desync between the repository schema
@@ -1539,6 +1584,7 @@ class ServerMigrationHelper(MigrationHel
         if commit:
             self.commit()
 
+    @ask_abort_with_pdb
     def cmd_add_relation_type_table(self, rtype, commit=True):
         """low level method to create the sql table for an existing relation.
         This may be useful on accidental desync between the repository schema



More information about the cubicweb-devel mailing list