forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemoncms-cli
More file actions
executable file
·83 lines (67 loc) · 2.13 KB
/
Copy pathemoncms-cli
File metadata and controls
executable file
·83 lines (67 loc) · 2.13 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
#!/usr/bin/env php
<?php
if (!in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI'.PHP_EOL;
}
define('EMONCMS_EXEC', 1);
const COMMAND_DB_UPDATE = "admin:dbupdate";
const COMMANDS = [
COMMAND_DB_UPDATE => "Run database migrations"
];
if ($argc <= 1) {
echo "Welcome to the Emoncms CLI tool\n\n";
echo "Usage: emoncms-cli <command>\n\n";
printAvailableCommands();
exit(0);
}
$commandName = $argv[1];
switch ($commandName) {
case COMMAND_DB_UPDATE:
runMigrations();
break;
default:
echo "{$commandName} is an invalid command\n\n";
printAvailableCommands();
exit(1);
}
function printAvailableCommands() {
echo "Available commands:\n";
foreach (COMMANDS as $command => $help) {
echo "- {$command} - {$help}";
}
echo "\n";
}
function runMigrations() {
require_once "process_settings.php";
if (!extension_loaded('mysql') && !extension_loaded('mysqli')) {
echo "Your PHP installation appears to be missing the MySQL extension(s) which are required by Emoncms. <br> See /php-info.php (restricted to local access)";
die;
}
$mysqli = @new mysqli(
$settings["sql"]["server"],
$settings["sql"]["username"],
$settings["sql"]["password"],
$settings["sql"]["database"],
$settings["sql"]["port"]
);
if ($mysqli->connect_error) {
echo "Can't connect to database, please verify credentials/configuration in settings.ini<br />";
if ($settings["display_errors"]) {
echo "Error message: <b>" . $mysqli->connect_error . "</b>";
}
die();
}
// Set charset to utf8
$mysqli->set_charset("utf8");
require_once "core.php";
require_once "Lib/dbschemasetup.php";
$updates = db_schema_setup($mysqli, load_db_schema(), true);
if (count($updates) === 0) {
echo "No migrations to run\n";
exit(0);
}
foreach ($updates as $update) {
echo "$update \n";
}
echo "Migrations successfully executed\n";
}