summaryrefslogtreecommitdiff
path: root/vendor/fguillot/picodb/lib/PicoDb/Schema.php
blob: a028036804bd1c588c9f757cafbb30946cf63ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace PicoDb;

use PDOException;

/**
 * Schema migration class
 *
 * @package PicoDb
 * @author  Frederic Guillot
 */
class Schema
{
    /**
     * Database instance
     *
     * @access protected
     * @var Database
     */
    protected $db = null;

    /**
     * Schema namespace
     *
     * @access protected
     * @var string
     */
    protected $namespace = '\Schema';

    /**
     * Constructor
     *
     * @access public
     * @param  Database  $db
     */
    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    /**
     * 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;
    }

    /**
     * Check the schema version and run the migrations
     *
     * @access public
     * @param  integer  $last_version
     * @return boolean
     */
    public function check($last_version = 1)
    {
        $current_version = $this->db->getDriver()->getSchemaVersion();

        if ($current_version < $last_version) {
            return $this->migrateTo($current_version, $last_version);
        }

        return true;
    }

    /**
     * Migrate the schema to one version to another
     *
     * @access public
     * @param  integer  $current_version
     * @param  integer  $next_version
     * @return boolean
     */
    public function migrateTo($current_version, $next_version)
    {
        try {
            for ($i = $current_version + 1; $i <= $next_version; $i++) {
                $this->db->startTransaction();
                $this->db->getDriver()->disableForeignKeys();

                $function_name = $this->getNamespace().'\version_'.$i;

                if (function_exists($function_name)) {
                    $this->db->setLogMessage('Running migration '.$function_name);
                    call_user_func($function_name, $this->db->getConnection());
                }

                $this->db->getDriver()->setSchemaVersion($i);
                $this->db->getDriver()->enableForeignKeys();
                $this->db->closeTransaction();
            }
        } catch (PDOException $e) {
            $this->db->setLogMessage($e->getMessage());
            $this->db->cancelTransaction();
            $this->db->getDriver()->enableForeignKeys();
            return false;
        }

        return true;
    }
}