1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Admin\Main; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\System; |
7
|
|
|
use Ffcms\Core\Arch\Model; |
8
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FormUpdateDatabase. Update database business logic model |
13
|
|
|
* @package Apps\Model\Admin\Main |
14
|
|
|
*/ |
15
|
|
|
class FormUpdateDatabase extends Model |
16
|
|
|
{ |
17
|
|
|
private $dbVersion; |
18
|
|
|
private $scriptVersion; |
19
|
|
|
public $updateQueries = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* FormUpdateDatabase constructor. Pass db and script version inside |
23
|
|
|
* @param string $db |
24
|
|
|
* @param string $script |
25
|
|
|
*/ |
26
|
|
|
public function __construct($db, $script) |
27
|
|
|
{ |
28
|
|
|
$this->dbVersion = $db; |
29
|
|
|
$this->scriptVersion = $script; |
30
|
|
|
parent::__construct(true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Find update files with sql queries |
35
|
|
|
*/ |
36
|
|
|
public function findUpdateFiles() |
37
|
|
|
{ |
38
|
|
|
// find all file with update sql queries between $dbVersion<->scriptVersion (dbVer <= x <= scriptVer) |
39
|
|
|
$all = File::listFiles('/Private/Database/Updates/', ['.php'], true); |
40
|
|
|
foreach ($all as $file) { |
41
|
|
|
$file = Str::cleanExtension(basename($file)); |
42
|
|
|
// $file="3.0.0-3.0.1" become to $start = 3.0.0,$end=3.0.1 |
|
|
|
|
43
|
|
|
list($start,$end) = explode('-', $file); |
44
|
|
|
// true: start <= db & script >= $end |
45
|
|
|
if (version_compare($this->dbVersion, $start) !== 1 && version_compare($this->scriptVersion, $end) !== -1) { |
46
|
|
|
$this->updateQueries[] = $file; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
sort($this->updateQueries); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Include files with update queries |
54
|
|
|
*/ |
55
|
|
|
public function make() |
56
|
|
|
{ |
57
|
|
|
// run update queries from included files |
58
|
|
|
foreach ($this->updateQueries as $file) { |
59
|
|
|
@include root . '/Private/Database/Updates/' . $file . '.php'; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
// update version in db table |
62
|
|
|
$row = System::getVar('version'); |
63
|
|
|
$row->data = $this->scriptVersion; |
64
|
|
|
$row->save(); |
65
|
|
|
} |
66
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.