summaryrefslogtreecommitdiffstats
path: root/application/clicommands/MigrationCommand.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--application/clicommands/MigrationCommand.php66
1 files changed, 66 insertions, 0 deletions
diff --git a/application/clicommands/MigrationCommand.php b/application/clicommands/MigrationCommand.php
new file mode 100644
index 0000000..6a4d002
--- /dev/null
+++ b/application/clicommands/MigrationCommand.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Icinga\Module\Director\Clicommands;
+
+use Icinga\Module\Director\Cli\Command;
+use Icinga\Module\Director\Db\Migrations;
+
+/**
+ * Handle DB migrations
+ *
+ * This command retrieves information about unapplied database migration and
+ * helps applying them.
+ */
+class MigrationCommand extends Command
+{
+ /**
+ * Check whether there are pending migrations
+ *
+ * This is mostly for automation, so one could create a Puppet manifest
+ * as follows:
+ *
+ * exec { 'Icinga Director DB migration':
+ * command => 'icingacli director migration run',
+ * onlyif => 'icingacli director migration pending',
+ * }
+ *
+ * Exit code 0 means that there are pending migrations, code 1 that there
+ * are no such. Use --verbose for human readable output
+ */
+ public function pendingAction()
+ {
+ if ($count = $this->migrations()->countPendingMigrations()) {
+ if ($this->isVerbose) {
+ if ($count === 1) {
+ echo "There is 1 pending migration\n";
+ } else {
+ printf("There are %d pending migrations\n", $count);
+ }
+ }
+
+ exit(0);
+ } else {
+ if ($this->isVerbose) {
+ echo "There are no pending migrations\n";
+ }
+
+ exit(1);
+ }
+ }
+
+ /**
+ * Run any pending migrations
+ *
+ * All pending migrations will be silently applied
+ */
+ public function runAction()
+ {
+ $this->migrations()->applyPendingMigrations();
+ exit(0);
+ }
+
+ protected function migrations()
+ {
+ return new Migrations($this->db());
+ }
+}