2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoDb;
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
use PDOException;
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Schema migration class
|
|
|
|
*
|
2016-07-31 00:41:42 +02:00
|
|
|
* @package PicoDb
|
|
|
|
* @author Frederic Guillot
|
2015-08-15 03:33:39 +02:00
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
class Schema
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Database instance
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var Database
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
protected $db = null;
|
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
/**
|
|
|
|
* Schema namespace
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace = '\Schema';
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param Database $db
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function __construct(Database $db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
/**
|
|
|
|
* Set another namespace
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $namespace
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
public function setNamespace($namespace)
|
|
|
|
{
|
|
|
|
$this->namespace = $namespace;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get schema namespace
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->namespace;
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Check the schema version and run the migrations
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $last_version
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function check($last_version = 1)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$current_version = $this->db->getDriver()->getSchemaVersion();
|
2013-02-18 03:48:21 +01:00
|
|
|
|
|
|
|
if ($current_version < $last_version) {
|
|
|
|
return $this->migrateTo($current_version, $last_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Migrate the schema to one version to another
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $current_version
|
|
|
|
* @param integer $next_version
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function migrateTo($current_version, $next_version)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
for ($i = $current_version + 1; $i <= $next_version; $i++) {
|
2016-07-31 00:41:42 +02:00
|
|
|
$this->db->startTransaction();
|
|
|
|
$this->db->getDriver()->disableForeignKeys();
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
$function_name = $this->getNamespace().'\version_'.$i;
|
2013-02-18 03:48:21 +01:00
|
|
|
|
|
|
|
if (function_exists($function_name)) {
|
2016-07-31 00:41:42 +02:00
|
|
|
$this->db->setLogMessage('Running migration '.$function_name);
|
2013-02-18 03:48:21 +01:00
|
|
|
call_user_func($function_name, $this->db->getConnection());
|
|
|
|
}
|
|
|
|
|
2016-07-31 00:41:42 +02:00
|
|
|
$this->db->getDriver()->setSchemaVersion($i);
|
|
|
|
$this->db->getDriver()->enableForeignKeys();
|
|
|
|
$this->db->closeTransaction();
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->db->setLogMessage($e->getMessage());
|
2013-02-18 03:48:21 +01:00
|
|
|
$this->db->cancelTransaction();
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->db->getDriver()->enableForeignKeys();
|
2013-02-18 03:48:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-24 03:28:26 +01:00
|
|
|
}
|