ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py

changeset 35
65a377b7a52c
parent 34
a91c6a1eb23f
child 38
f5055c1e4e07
equal deleted inserted replaced
34:a91c6a1eb23f 35:65a377b7a52c
21 21
22 from ..FlaskCommandDialog import FlaskCommandDialog 22 from ..FlaskCommandDialog import FlaskCommandDialog
23 23
24 24
25 # TODO: add a submenu with action for the commands with command options 25 # TODO: add a submenu with action for the commands with command options
26 # TODO: add a submenu to show the created SQL commands (--sql option)
27 class MigrateProject(QObject): 26 class MigrateProject(QObject):
28 """ 27 """
29 Class implementing the flask-migrate project support. 28 Class implementing the flask-migrate project support.
30 """ 29 """
31 def __init__(self, plugin, project, parent=None): 30 def __init__(self, plugin, project, parent=None):
43 42
44 self.__plugin = plugin 43 self.__plugin = plugin
45 self.__project = project 44 self.__project = project
46 45
47 self.__e5project = e5App().getObject("Project") 46 self.__e5project = e5App().getObject("Project")
47
48 self.__migrationSummaryDialog = None
48 49
49 def initActions(self): 50 def initActions(self):
50 """ 51 """
51 Public method to define the flask-migrate actions. 52 Public method to define the flask-migrate actions.
52 """ 53 """
154 self.upgradeDatabaseAct.setWhatsThis(self.tr( 155 self.upgradeDatabaseAct.setWhatsThis(self.tr(
155 """<b>Upgrade Database</b>""" 156 """<b>Upgrade Database</b>"""
156 """<p>Upgrades the database to the current migration.</p>""" 157 """<p>Upgrades the database to the current migration.</p>"""
157 )) 158 ))
158 self.upgradeDatabaseAct.triggered.connect( 159 self.upgradeDatabaseAct.triggered.connect(
159 self.__upgradeDatabase) 160 self.upgradeDatabase)
160 self.actions.append(self.upgradeDatabaseAct) 161 self.actions.append(self.upgradeDatabaseAct)
161 162
162 # TODO: add action for flask db downgrade
163 self.downgradeDatabaseAct = E5Action( 163 self.downgradeDatabaseAct = E5Action(
164 self.tr('Downgrade Database'), 164 self.tr('Downgrade Database'),
165 self.tr('&Downgrade Database'), 165 self.tr('&Downgrade Database'),
166 0, 0, 166 0, 0,
167 self, 'flask_downgrade_database') 167 self, 'flask_downgrade_database')
170 self.downgradeDatabaseAct.setWhatsThis(self.tr( 170 self.downgradeDatabaseAct.setWhatsThis(self.tr(
171 """<b>Downgrade Database</b>""" 171 """<b>Downgrade Database</b>"""
172 """<p>Downgrades the database to the previous version.</p>""" 172 """<p>Downgrades the database to the previous version.</p>"""
173 )) 173 ))
174 self.downgradeDatabaseAct.triggered.connect( 174 self.downgradeDatabaseAct.triggered.connect(
175 self.__downgradeDatabase) 175 self.downgradeDatabase)
176 self.actions.append(self.downgradeDatabaseAct) 176 self.actions.append(self.downgradeDatabaseAct)
177
178 #########################################################
179 ## actions to show migrations history information
180 #########################################################
181
182 self.migrationSummaryAct = E5Action(
183 self.tr('Show Migrations Summary'),
184 self.tr('Show Migrations &Summary'),
185 0, 0,
186 self, 'flask_show_migrations_summary')
187 self.migrationSummaryAct.setStatusTip(self.tr(
188 'Show a summary of the created database migrations'))
189 self.migrationSummaryAct.setWhatsThis(self.tr(
190 """<b>Show Migrations Summary</b>"""
191 """<p>Shows a summary list of the created database"""
192 """ migrations.</p>"""
193 ))
194 self.migrationSummaryAct.triggered.connect(
195 self.__showMigrationsSummary)
196 self.actions.append(self.migrationSummaryAct)
197
198 self.migrationHistoryAct = E5Action(
199 self.tr('Show Migrations History'),
200 self.tr('Show Migrations &History'),
201 0, 0,
202 self, 'flask_show_migrations_history')
203 self.migrationHistoryAct.setStatusTip(self.tr(
204 'Show the full history of the created database migrations'))
205 self.migrationHistoryAct.setWhatsThis(self.tr(
206 """<b>Show Migrations History</b>"""
207 """<p>Shows the full history of the created database"""
208 """ migrations.</p>"""
209 ))
210 self.migrationHistoryAct.triggered.connect(
211 self.__showMigrationsHistory)
212 self.actions.append(self.migrationHistoryAct)
177 213
178 def initMenu(self): 214 def initMenu(self):
179 """ 215 """
180 Public method to initialize the flask-migrate menu. 216 Public method to initialize the flask-migrate menu.
181 217
191 menu.addSeparator() 227 menu.addSeparator()
192 menu.addAction(self.migrateCreateAct) 228 menu.addAction(self.migrateCreateAct)
193 menu.addSeparator() 229 menu.addSeparator()
194 menu.addAction(self.upgradeDatabaseAct) 230 menu.addAction(self.upgradeDatabaseAct)
195 menu.addAction(self.downgradeDatabaseAct) 231 menu.addAction(self.downgradeDatabaseAct)
232 menu.addSeparator()
233 menu.addAction(self.migrationSummaryAct)
234 menu.addAction(self.migrationHistoryAct)
196 menu.addSeparator() 235 menu.addSeparator()
197 menu.addAction(self.migrateAvailabilityAct) 236 menu.addAction(self.migrateAvailabilityAct)
198 menu.addAction(self.migrateInstallAct) 237 menu.addAction(self.migrateInstallAct)
199 238
200 return menu 239 return menu
261 if abspath: 300 if abspath:
262 migrations = self.__e5project.getAbsoluteUniversalPath( 301 migrations = self.__e5project.getAbsoluteUniversalPath(
263 "migrations") 302 "migrations")
264 303
265 return migrations 304 return migrations
305
306 def projectClosed(self):
307 """
308 Public method to handle the closing of a project.
309 """
310 for dlg in (self.__migrationSummaryDialog,):
311 if dlg is not None:
312 dlg.close()
266 313
267 ######################################################## 314 ########################################################
268 ## Menu related slots below 315 ## Menu related slots below
269 ######################################################## 316 ########################################################
270 317
419 ######################################################### 466 #########################################################
420 ## slots to up- and downgrade a databse 467 ## slots to up- and downgrade a databse
421 ######################################################### 468 #########################################################
422 469
423 @pyqtSlot() 470 @pyqtSlot()
424 def __upgradeDatabase(self): 471 def upgradeDatabase(self, revision=None):
425 """ 472 """
426 Private slot to upgrade the database to the current migration. 473 Public slot to upgrade the database to the current migration.
474
475 @param revision migration revision to upgrade to
476 @type str
427 """ 477 """
428 title = self.tr("Upgrade Database") 478 title = self.tr("Upgrade Database")
429 479
430 self.__ensureMigrateConfigured() 480 self.__ensureMigrateConfigured()
431 migrations = self.__migrationsDirectory() 481 migrations = self.__migrationsDirectory()
432 482
433 args = ["upgrade"] 483 args = ["upgrade"]
434 if migrations: 484 if migrations:
435 args += ["--directory", migrations] 485 args += ["--directory", migrations]
486 if revision:
487 args.append(revision)
436 488
437 dlg = FlaskCommandDialog( 489 dlg = FlaskCommandDialog(
438 self.__project, title=title, 490 self.__project, title=title,
439 msgSuccess=self.tr("\nDatabase upgraded successfully.") 491 msgSuccess=self.tr("\nDatabase upgraded successfully.")
440 ) 492 )
441 if dlg.startCommand("db", args): 493 if dlg.startCommand("db", args):
442 dlg.exec() 494 dlg.exec()
443 495
444 @pyqtSlot() 496 @pyqtSlot()
445 def __downgradeDatabase(self): 497 def downgradeDatabase(self, revision=None):
446 """ 498 """
447 Private slot to downgrade the database to the previous version. 499 Public slot to downgrade the database to the previous version.
448 """ 500
449 title = self.tr("downgrade Database") 501 @param revision migration revision to downgrade to
502 @type str
503 """
504 title = self.tr("Downgrade Database")
450 505
451 self.__ensureMigrateConfigured() 506 self.__ensureMigrateConfigured()
452 migrations = self.__migrationsDirectory() 507 migrations = self.__migrationsDirectory()
453 508
454 args = ["downgrade"] 509 args = ["downgrade"]
455 if migrations: 510 if migrations:
456 args += ["--directory", migrations] 511 args += ["--directory", migrations]
512 if revision:
513 args.append(revision)
457 514
458 dlg = FlaskCommandDialog( 515 dlg = FlaskCommandDialog(
459 self.__project, title=title, 516 self.__project, title=title,
460 msgSuccess=self.tr("\nDatabase downgraded successfully.") 517 msgSuccess=self.tr("\nDatabase downgraded successfully.")
461 ) 518 )
462 if dlg.startCommand("db", args): 519 if dlg.startCommand("db", args):
463 dlg.exec() 520 dlg.exec()
521
522 #########################################################
523 ## slots to show migrations history information
524 #########################################################
525
526 @pyqtSlot()
527 def __showMigrationsSummary(self):
528 """
529 Private slot to show a migrations history summary.
530 """
531 from .MigrateSummaryDialog import MigrateSummaryDialog
532
533 self.__ensureMigrateConfigured()
534 migrations = self.__migrationsDirectory()
535
536 if self.__migrationSummaryDialog is None:
537 self.__migrationSummaryDialog = MigrateSummaryDialog(
538 self.__project, self, migrations=migrations)
539
540 self.__migrationSummaryDialog.showSummary()
541
542 @pyqtSlot()
543 def __showMigrationsHistory(self):
544 """
545 Private slot to show the full migrations history.
546 """
547 title = self.tr("Migrations History")
548
549 self.__ensureMigrateConfigured()
550 migrations = self.__migrationsDirectory()
551
552 args = ["history", "--indicate-current", "--verbose"]
553 if migrations:
554 args += ["--directory", migrations]
555
556 dlg = FlaskCommandDialog(self.__project, title=title)
557 if dlg.startCommand("db", args):
558 dlg.exec()

eric ide

mercurial