Completed
Pull Request — master (#23)
by
unknown
01:16
created

BitrixDatabaseStorage::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixMigrations\Storages;
4
5
use Arrilot\BitrixMigrations\Interfaces\DatabaseStorageInterface;
6
use Bitrix\Main\Application;
7
use CDatabase;
8
9
class BitrixDatabaseStorage implements DatabaseStorageInterface
10
{
11
    /**
12
     * Bitrix $DB object.
13
     *
14
     * @var CDatabase
15
     */
16
    protected $db;
17
18
    /**
19
     * Table in DB to store migrations that have been already ran.
20
     *
21
     * @var string
22
     */
23
    protected $table;
24
25
    /**
26
     * BitrixDatabaseStorage constructor.
27
     *
28
     * @param $table
29
     */
30
    public function __construct($table)
31
    {
32
        global $DB;
33
34
        $this->db = $DB;
35
        $this->table = $table;
36
    }
37
38
    /**
39
     * Check if a given table already exists.
40
     *
41
     * @return bool
42
     */
43
    public function checkMigrationTableExistence()
44
    {
45
        return (bool) $this->db->query('SHOW TABLES LIKE "'.$this->table.'"')->fetch();
46
    }
47
48
    /**
49
     * Create migration table.
50
     *
51
     * @return void
52
     */
53
    public function createMigrationTable()
54
    {
55
        $this->db->query("CREATE TABLE {$this->table} (ID INT NOT NULL AUTO_INCREMENT, MIGRATION VARCHAR(255) NOT NULL, PRIMARY KEY (ID))");
56
    }
57
58
    /**
59
     * Get an array of migrations the have been ran previously.
60
     * Must be ordered by order asc.
61
     *
62
     * @return array
63
     */
64
    public function getRanMigrations()
65
    {
66
        $migrations = [];
67
68
        $dbRes = $this->db->query("SELECT MIGRATION FROM {$this->table} ORDER BY ID ASC");
69
        while ($result = $dbRes->fetch()) {
70
            $migrations[] = $result['MIGRATION'];
71
        }
72
73
        return $migrations;
74
    }
75
76
    /**
77
     * Save migration name to the database to prevent it from running again.
78
     *
79
     * @param string $name
80
     *
81
     * @return void
82
     */
83
    public function logSuccessfulMigration($name)
84
    {
85
        $this->db->insert($this->table, [
86
            'MIGRATION' => "'".$this->db->forSql($name)."'",
87
        ]);
88
    }
89
90
    /**
91
     * Remove a migration name from the database so it can be run again.
92
     *
93
     * @param string $name
94
     *
95
     * @return void
96
     */
97
    public function removeSuccessfulMigrationFromLog($name)
98
    {
99
        $this->db->query("DELETE FROM {$this->table} WHERE MIGRATION = '".$this->db->forSql($name)."'");
100
    }
101
102
    /**
103
     * Start transaction
104
     */
105
    public function startTransaction()
106
    {
107
        $this->db->StartTransaction();
108
    }
109
110
    /**
111
     * Commit transaction
112
     */
113
    public function commitTransaction()
114
    {
115
        $this->db->Commit();
116
    }
117
118
    /**
119
     * Rollback transaction
120
     */
121
    public function rollbackTransaction()
122
    {
123
        $this->db->Rollback();
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function getTableName()
130
    {
131
        return $this->table;
132
    }
133
}
134