|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Model\Admin\Main; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\System; |
|
6
|
|
|
use Ffcms\Core\Arch\Model; |
|
7
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
8
|
|
|
use Ffcms\Core\Managers\MigrationsManager; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FormUpdateDatabase. Update database business logic model |
|
12
|
|
|
* @package Apps\Model\Admin\Main |
|
13
|
|
|
*/ |
|
14
|
|
|
class FormUpdateDatabase extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
private $dbVersion; |
|
17
|
|
|
private $scriptVersion; |
|
18
|
|
|
public $updateQueries = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* FormUpdateDatabase constructor. Pass db and script version inside |
|
22
|
|
|
* @param string $db |
|
23
|
|
|
* @param string $script |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($db, $script) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->dbVersion = $db; |
|
28
|
|
|
$this->scriptVersion = $script; |
|
29
|
|
|
parent::__construct(true); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Find update files with sql queries |
|
34
|
|
|
*/ |
|
35
|
|
|
public function findUpdateFiles() |
|
36
|
|
|
{ |
|
37
|
|
|
// find all file with update sql queries between $dbVersion<->scriptVersion (dbVer <= x <= scriptVer) |
|
38
|
|
|
$migrations = new MigrationsManager('/Private/Migrations/Updates/'); |
|
39
|
|
|
$search = $migrations->search('update'); |
|
40
|
|
|
foreach ($search as $file) { |
|
|
|
|
|
|
41
|
|
|
$fullName = Str::cleanExtension(basename($file)); |
|
42
|
|
|
$name = Str::firstIn($fullName, '-'); |
|
43
|
|
|
// get update version number from migration name |
|
44
|
|
|
list($type, $obj, $version) = explode('_', $name); |
|
45
|
|
|
$intVersion = (int)Str::replace('.', '', $this->dbVersion); |
|
46
|
|
|
// if migration version > db version - implement it |
|
47
|
|
|
if ($version > $intVersion) { |
|
48
|
|
|
$this->updateQueries[] = $file; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
sort($this->updateQueries); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Include files with update queries |
|
56
|
|
|
*/ |
|
57
|
|
|
public function make() |
|
58
|
|
|
{ |
|
59
|
|
|
// run update queries from migrations |
|
60
|
|
|
$migration = new MigrationsManager('/Private/Migrations/Updates/'); |
|
61
|
|
|
$migration->makeUp($this->updateQueries); |
|
62
|
|
|
// update version in db table |
|
63
|
|
|
$row = System::getVar('version'); |
|
64
|
|
|
$row->data = $this->scriptVersion; |
|
|
|
|
|
|
65
|
|
|
$row->save(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|