summaryrefslogtreecommitdiff
path: root/app/ServiceProvider/DatabaseProvider.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-12-02 19:16:50 -0500
committerFrederic Guillot <fred@kanboard.net>2016-12-02 19:16:50 -0500
commitf2fd5d75d98e01ab58a71b718d630b9b2ae8e16f (patch)
tree6c691b5f0024b822df75a37644e804c864d8f8e7 /app/ServiceProvider/DatabaseProvider.php
parentb5671a2dc04b63a8d7787b1137e72d7858955d98 (diff)
Add config parameter to toggle automatic SQL migrations
- Add two command line opterations to show schema version and to execute SQL migrations - Add new configuration parameter to enable or disable SQL migrations
Diffstat (limited to 'app/ServiceProvider/DatabaseProvider.php')
-rw-r--r--app/ServiceProvider/DatabaseProvider.php41
1 files changed, 36 insertions, 5 deletions
diff --git a/app/ServiceProvider/DatabaseProvider.php b/app/ServiceProvider/DatabaseProvider.php
index a3f57457..9998ac43 100644
--- a/app/ServiceProvider/DatabaseProvider.php
+++ b/app/ServiceProvider/DatabaseProvider.php
@@ -27,6 +27,10 @@ class DatabaseProvider implements ServiceProviderInterface
{
$container['db'] = $this->getInstance();
+ if (DB_RUN_MIGRATIONS) {
+ self::runMigrations($container['db']);
+ }
+
if (DEBUG) {
$container['db']->getStatementHandler()
->withLogging()
@@ -38,7 +42,7 @@ class DatabaseProvider implements ServiceProviderInterface
}
/**
- * Setup the database driver and execute schema migration
+ * Setup the database driver
*
* @access public
* @return \PicoDb\Database
@@ -59,12 +63,39 @@ class DatabaseProvider implements ServiceProviderInterface
throw new LogicException('Database driver not supported');
}
- if ($db->schema()->check(\Schema\VERSION)) {
- return $db;
- } else {
+ return $db;
+ }
+
+ /**
+ * Get current database version
+ *
+ * @static
+ * @access public
+ * @param Database $db
+ * @return int
+ */
+ public static function getSchemaVersion(Database $db)
+ {
+ return $db->getDriver()->getSchemaVersion();
+ }
+
+ /**
+ * Execute database migrations
+ *
+ * @static
+ * @access public
+ * @throws RuntimeException
+ * @param Database $db
+ * @return bool
+ */
+ public static function runMigrations(Database $db)
+ {
+ if (! $db->schema()->check(\Schema\VERSION)) {
$messages = $db->getLogMessages();
throw new RuntimeException('Unable to run SQL migrations: '.implode(', ', $messages).' (You may have to fix it manually)');
}
+
+ return true;
}
/**
@@ -79,7 +110,7 @@ class DatabaseProvider implements ServiceProviderInterface
return new Database(array(
'driver' => 'sqlite',
- 'filename' => DB_FILENAME
+ 'filename' => DB_FILENAME,
));
}