2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoDb;
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
use PDOException;
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
class Schema
|
|
|
|
{
|
|
|
|
protected $db = null;
|
|
|
|
|
|
|
|
public function __construct(Database $db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function check($last_version = 1)
|
|
|
|
{
|
|
|
|
$current_version = $this->db->getConnection()->getSchemaVersion();
|
|
|
|
|
|
|
|
if ($current_version < $last_version) {
|
|
|
|
return $this->migrateTo($current_version, $last_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function migrateTo($current_version, $next_version)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
$this->db->startTransaction();
|
|
|
|
|
|
|
|
for ($i = $current_version + 1; $i <= $next_version; $i++) {
|
|
|
|
|
|
|
|
$function_name = '\Schema\version_'.$i;
|
|
|
|
|
|
|
|
if (function_exists($function_name)) {
|
|
|
|
call_user_func($function_name, $this->db->getConnection());
|
|
|
|
$this->db->getConnection()->setSchemaVersion($i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->closeTransaction();
|
|
|
|
}
|
2014-12-24 03:28:26 +01:00
|
|
|
catch (PDOException $e) {
|
2014-09-15 13:23:55 +02:00
|
|
|
$this->db->setLogMessage($function_name.' => '.$e->getMessage());
|
2013-02-18 03:48:21 +01:00
|
|
|
$this->db->cancelTransaction();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-24 03:28:26 +01:00
|
|
|
}
|