-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstaller.php
More file actions
84 lines (78 loc) · 3.91 KB
/
Copy pathInstaller.php
File metadata and controls
84 lines (78 loc) · 3.91 KB
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
<?php
declare(strict_types = 1);
namespace Simbiat\Cron;
use Simbiat\Database\Manage;
use Simbiat\Database\Query;
use Simbiat\StringHelpers\Sanitize;
/**
* Installer class for CRON library.
*/
class Installer
{
use TraitForCron;
/**
* Class constructor
* @param \PDO|null $dbh PDO object to use for database connection. If not provided, the class expects the existence of `\Simbiat\Database\Pool` to use that instead.
* @param string $prefix Cron database prefix.
*/
public function __construct(\PDO|null $dbh = null, string $prefix = 'cron__')
{
$this->init($dbh, $prefix);
}
/**
* Install the necessary tables
* @return bool
*/
public function install(): bool
{
return new \Simbiat\Database\Installer($this->dbh)::install(__DIR__.'/sql/*.sql', $this->getVersion(), 'cron__', $this->prefix);
}
/**
* Get the current version of the Agent from the database perspective (can be different from the library version)
* @return string
*/
public function getVersion(): string
{
$version = '0.0.0';
#Check if the settings table exists
if (Manage::checkTable($this->prefix.'settings') === 1) {
#Assume that we have installed the database, try to get the version
$version = Query::query('SELECT `value` FROM `'.$this->prefix.'settings` WHERE `setting`=\'version\'', return: 'value');
#If an empty installer script was run before 2.1.2, we need to determine what version we have based on other things
if (!Sanitize::whiteString($version ?? '')) {
#If errors' table does not exist, and the log table does, then we are on version 2.0.0
if (Manage::checkTable($this->prefix.'errors') === 0 && Manage::checkTable($this->prefix.'log') === 1) {
$version = '2.0.0';
#If one of the schedule columns is datetime, it's 1.5.0
} elseif (Manage::getColumnType($this->prefix.'schedule', 'registered') === 'datetime') {
$version = '1.5.0';
#If `max_time` column is present in `tasks` table - 1.3.0
} elseif (Manage::checkColumn($this->prefix.'tasks', 'max_time')) {
$version = '1.3.0';
#If `max_time` column is present in `tasks` table - 1.2.0
} elseif (Manage::checkColumn($this->prefix.'schedule', 'sse')) {
$version = '1.2.0';
#If one of the settings has the name `errorLife` (and not `errorlife`), then we have version 1.1.14
} elseif (Query::query('SELECT `setting` FROM `'.$this->prefix.'settings` WHERE `setting`=\'errorLife\'', return: 'value') === 'errorLife') {
$version = '1.1.14';
#If the `arguments` column is not nullable, then we have version 1.1.12
} elseif (!Manage::isNullable($this->prefix.'schedule', 'arguments')) {
$version = '1.1.12';
#If `errors_to_arguments` Foreign Key exists in `errors` table - 1.1.8
} elseif (Manage::checkFK($this->prefix.'_errors', 'errors_to_arguments')) {
$version = '1.1.8';
#It's 1.1.7 if the old column description is used
} elseif (Manage::getColumnDescription($this->prefix.'schedule', 'arguments') === 'Optional task arguments') {
$version = '1.1.7';
#If the `maxthreads` setting exists, then we have version 1.1.0
} elseif (Query::query('SELECT `setting` FROM `'.$this->prefix.'settings` WHERE `setting`=\'maxthreads\'', return: 'value') === 'maxthreads') {
$version = '1.1.0';
#Otherwise - version 1.0.0
} else {
$version = '1.0.0';
}
}
}
return $version;
}
}