This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
forked from shlinkio/shlink-docker-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckTablesExist.php
60 lines (56 loc) · 2.06 KB
/
CheckTablesExist.php
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
<?php
/**
* User: Noah Kovacs
* Date: 2019-01-03
* Time: 15:37
*/
$dbDriver = getenv("DB_DRIVER") ?: "sqlite";
$dbHost = getenv("DB_HOST") ?: "127.0.0.1";
$dbName = getenv("DB_NAME") ?: "shlink";
$dbUser = getenv("DB_USER") ?: "root";
$dbPassword = getenv("DB_PASSWORD") ?: "password";
$dbPort = getenv("DB_PORT") ?: 3306;
if (!$dbHost || !$dbName || !$dbUser || !$dbPassword || !$dbPort || !$dbDriver) {
echo "false";
exit;
}
if ($dbDriver !== "sqlite") {
if ($dbDriver === "mysql") {
// Using string concatenation because interpolation acts weird sometimes
$mysqlDSN = "mysql:host=" . $dbHost . ";port=" . $dbPort . ";dbname=" . $dbName;
$pdo = new PDO($mysqlDSN, $dbUser, $dbPassword);
$pdo->beginTransaction();
$pdo->exec("LOCK TABLES migrations, api_keys, short_urls, short_urls_in_tags, tags, visit_locations, visits WRITE NOWAIT");
$stmt = $pdo->prepare("SELECT `version` from shlink.migrations LIMIT 1");
$stmt->execute();
$result = $stmt->fetch();
$pdo->exec("UNLOCK TABLES");
$pdo->commit();
if ($result) {
echo "true";
exit;
} else {
echo "false";
exit;
}
} else if ($dbDriver === 'postgres') {
$connString = "host=" . $dbHost . " dbname=" . $dbName . " user=" . $dbUser;
if ($dbPassword) {
$connString .= " password=$dbPassword";
}
if ($dbPort) {
$connString .= " port=$dbPort";
}
$conn = pg_connect($connString);
$q = "SELECT `version` from migrations LIMIT 1";
$res = pg_query($q) or die("false");
$row = pg_fetch_array($res, null, PGSQL_ASSOC);
if ( count($row) > 0 ) {
echo "true";
exit;
} else {
echo "false";
exit;
}
}
}