ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py

changeset 32
80fc8deac8fe
parent 31
e614add95516
child 33
c82b296a21ae
diff -r e614add95516 -r 80fc8deac8fe ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py
--- a/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Tue Nov 24 20:17:20 2020 +0100
+++ b/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Wed Nov 25 19:39:04 2020 +0100
@@ -40,6 +40,8 @@
         
         self.__plugin = plugin
         self.__project = project
+        
+        self.__e5project = e5App().getObject("Project")
     
     def initActions(self):
         """
@@ -179,11 +181,13 @@
         
         return False
     
-    def __migrationsDirectory(self):
+    def __migrationsDirectory(self, abspath=False):
         """
         Private method to calculate the path of the configured migrations
         directory.
         
+        @param abspath flag indicating to return an absolute path
+        @type bool
         @return path of the migrations directory
         @rtype str
         """
@@ -194,11 +198,19 @@
         migrations = self.__project.getData("migrate",
                                             "migrationsDirectory")
         if migrations:
-            workdir = self.__project.getApplication()[0]
-            migrations = os.path.relpath(
-                self.__e5project.getAbsoluteUniversalPath(migrations),
-                workdir
-            )
+            if abspath:
+                migrations = self.__e5project.getAbsoluteUniversalPath(
+                    migrations)
+            else:
+                workdir = self.__project.getApplication()[0]
+                migrations = os.path.relpath(
+                    self.__e5project.getAbsoluteUniversalPath(migrations),
+                    workdir
+                )
+        else:
+            if abspath:
+                migrations = self.__e5project.getAbsoluteUniversalPath(
+                    "migrations")
         
         return migrations
     
@@ -223,9 +235,6 @@
     def __ensureMigrateConfigured(self):
         """
         Private method to ensure, that flask-migrate has been configured.
-        
-        @return flag indicating successful configuration
-        @rtype bool
         """
         config = self.__project.getData("migrate", "")
         if not config:
@@ -280,6 +289,8 @@
         """
         Private slot to initialize the database migration system.
         """
+        title = self.tr("Initialize Migrations")
+        
         self.__ensureMigrateConfigured()
         migrations = self.__migrationsDirectory()
         
@@ -296,9 +307,26 @@
             args.append("--multidb")
         
         dlg = FlaskCommandDialog(
-            self.__project, title=self.tr("Initialize Migrations"))
+            self.__project, title=title,
+            msgSuccess=self.tr("\nMigrations initialized successfully.")
+        )
         if dlg.startCommand("db", args):
             dlg.exec()
+            if dlg.normalExit():
+                for root, _dirs, files in os.walk(
+                    self.__migrationsDirectory(abspath=True)
+                ):
+                    for fileName in files:
+                        fullName = os.path.join(root, fileName)
+                        self.__e5project.appendFile(fullName)
+                
+                browser = (e5App().getObject("ProjectBrowser")
+                           .getProjectBrowser("others"))
+                alembic = os.path.join(
+                    self.__migrationsDirectory(abspath=True),
+                    "alembic.ini"
+                )
+                browser.sourceFile.emit(alembic)
     
     #########################################################
     ## slot to create a new database migration

eric ide

mercurial