ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py

changeset 35
65a377b7a52c
parent 34
a91c6a1eb23f
child 38
f5055c1e4e07
--- a/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Thu Nov 26 20:11:25 2020 +0100
+++ b/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Sat Nov 28 19:26:34 2020 +0100
@@ -23,7 +23,6 @@
 
 
 # TODO: add a submenu with action for the commands with command options
-# TODO: add a submenu to show the created SQL commands (--sql option)
 class MigrateProject(QObject):
     """
     Class implementing the flask-migrate project support.
@@ -45,6 +44,8 @@
         self.__project = project
         
         self.__e5project = e5App().getObject("Project")
+        
+        self.__migrationSummaryDialog = None
     
     def initActions(self):
         """
@@ -156,10 +157,9 @@
             """<p>Upgrades the database to the current migration.</p>"""
         ))
         self.upgradeDatabaseAct.triggered.connect(
-            self.__upgradeDatabase)
+            self.upgradeDatabase)
         self.actions.append(self.upgradeDatabaseAct)
         
-        # TODO: add action for flask db downgrade
         self.downgradeDatabaseAct = E5Action(
             self.tr('Downgrade Database'),
             self.tr('&Downgrade Database'),
@@ -172,8 +172,44 @@
             """<p>Downgrades the database to the previous version.</p>"""
         ))
         self.downgradeDatabaseAct.triggered.connect(
-            self.__downgradeDatabase)
+            self.downgradeDatabase)
         self.actions.append(self.downgradeDatabaseAct)
+        
+        #########################################################
+        ## actions to show migrations history information
+        #########################################################
+        
+        self.migrationSummaryAct = E5Action(
+            self.tr('Show Migrations Summary'),
+            self.tr('Show Migrations &Summary'),
+            0, 0,
+            self, 'flask_show_migrations_summary')
+        self.migrationSummaryAct.setStatusTip(self.tr(
+            'Show a summary of the created database migrations'))
+        self.migrationSummaryAct.setWhatsThis(self.tr(
+            """<b>Show Migrations Summary</b>"""
+            """<p>Shows a summary list of the created database"""
+            """ migrations.</p>"""
+        ))
+        self.migrationSummaryAct.triggered.connect(
+            self.__showMigrationsSummary)
+        self.actions.append(self.migrationSummaryAct)
+        
+        self.migrationHistoryAct = E5Action(
+            self.tr('Show Migrations History'),
+            self.tr('Show Migrations &History'),
+            0, 0,
+            self, 'flask_show_migrations_history')
+        self.migrationHistoryAct.setStatusTip(self.tr(
+            'Show the full history of the created database migrations'))
+        self.migrationHistoryAct.setWhatsThis(self.tr(
+            """<b>Show Migrations History</b>"""
+            """<p>Shows the full history of the created database"""
+            """ migrations.</p>"""
+        ))
+        self.migrationHistoryAct.triggered.connect(
+            self.__showMigrationsHistory)
+        self.actions.append(self.migrationHistoryAct)
     
     def initMenu(self):
         """
@@ -194,6 +230,9 @@
         menu.addAction(self.upgradeDatabaseAct)
         menu.addAction(self.downgradeDatabaseAct)
         menu.addSeparator()
+        menu.addAction(self.migrationSummaryAct)
+        menu.addAction(self.migrationHistoryAct)
+        menu.addSeparator()
         menu.addAction(self.migrateAvailabilityAct)
         menu.addAction(self.migrateInstallAct)
         
@@ -264,6 +303,14 @@
         
         return migrations
     
+    def projectClosed(self):
+        """
+        Public method to handle the closing of a project.
+        """
+        for dlg in (self.__migrationSummaryDialog,):
+            if dlg is not None:
+                dlg.close()
+    
     ########################################################
     ## Menu related slots below
     ########################################################
@@ -421,9 +468,12 @@
     #########################################################
     
     @pyqtSlot()
-    def __upgradeDatabase(self):
+    def upgradeDatabase(self, revision=None):
         """
-        Private slot to upgrade the database to the current migration.
+        Public slot to upgrade the database to the current migration.
+        
+        @param revision migration revision to upgrade to
+        @type str
         """
         title = self.tr("Upgrade Database")
         
@@ -433,6 +483,8 @@
         args = ["upgrade"]
         if migrations:
             args += ["--directory", migrations]
+        if revision:
+            args.append(revision)
         
         dlg = FlaskCommandDialog(
             self.__project, title=title,
@@ -442,11 +494,14 @@
             dlg.exec()
     
     @pyqtSlot()
-    def __downgradeDatabase(self):
+    def downgradeDatabase(self, revision=None):
         """
-        Private slot to downgrade the database to the previous version.
+        Public slot to downgrade the database to the previous version.
+        
+        @param revision migration revision to downgrade to
+        @type str
         """
-        title = self.tr("downgrade Database")
+        title = self.tr("Downgrade Database")
         
         self.__ensureMigrateConfigured()
         migrations = self.__migrationsDirectory()
@@ -454,6 +509,8 @@
         args = ["downgrade"]
         if migrations:
             args += ["--directory", migrations]
+        if revision:
+            args.append(revision)
         
         dlg = FlaskCommandDialog(
             self.__project, title=title,
@@ -461,3 +518,41 @@
         )
         if dlg.startCommand("db", args):
             dlg.exec()
+    
+    #########################################################
+    ## slots to show migrations history information
+    #########################################################
+    
+    @pyqtSlot()
+    def __showMigrationsSummary(self):
+        """
+        Private slot to show a migrations history summary.
+        """
+        from .MigrateSummaryDialog import MigrateSummaryDialog
+        
+        self.__ensureMigrateConfigured()
+        migrations = self.__migrationsDirectory()
+        
+        if self.__migrationSummaryDialog is None:
+            self.__migrationSummaryDialog = MigrateSummaryDialog(
+                self.__project, self, migrations=migrations)
+        
+        self.__migrationSummaryDialog.showSummary()
+    
+    @pyqtSlot()
+    def __showMigrationsHistory(self):
+        """
+        Private slot to show the full migrations history.
+        """
+        title = self.tr("Migrations History")
+        
+        self.__ensureMigrateConfigured()
+        migrations = self.__migrationsDirectory()
+        
+        args = ["history", "--indicate-current", "--verbose"]
+        if migrations:
+            args += ["--directory", migrations]
+        
+        dlg = FlaskCommandDialog(self.__project, title=title)
+        if dlg.startCommand("db", args):
+            dlg.exec()

eric ide

mercurial