summaryrefslogtreecommitdiffstats
path: root/modules-available/syslog/install.inc.php
blob: 2f306f9e420fedbfa35f6f1a7dde026283c8d693 (plain) (blame)
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
<?php

$res = array();

$res[] = $tc = tableCreate('clientlog', "
	`logid` int(10) unsigned NOT NULL AUTO_INCREMENT,
	`dateline` int(10) unsigned NOT NULL,
	`logtypeid` varchar(30) NOT NULL,
	`clientip` varchar(40) NOT NULL,
	`machineuuid` varchar(36) CHARACTER SET ascii DEFAULT NULL,
	`description` varchar(255) NOT NULL,
	`extra` text NOT NULL,
	PRIMARY KEY (`logid`),
	KEY `dateline` (`dateline`),
	KEY `logtypeid` (`logtypeid`,`dateline`),
	KEY `clientip` (`clientip`,`dateline`),
	KEY `machineuuidnew` (`machineuuid`,`logid`)
");

// Update path

if (!tableHasColumn('clientlog', 'machineuuid')) {
	$ret = Database::exec('ALTER TABLE clientlog'
		. ' ADD COLUMN `machineuuid` varchar(36) CHARACTER SET ascii DEFAULT NULL AFTER clientip,'
		. ' ADD INDEX `machineuuid` (`machineuuid`,`dateline`)');
	if ($ret === false) {
		finalResponse(UPDATE_FAILED, 'Adding machineuuid to clientlog failed: ' . Database::lastError());
	}
	$res[] = UPDATE_DONE;
}

// 2017-11-03: Create proper index for query in statistics module
if (tableGetIndex('clientlog', ['machineuuid']) !== false) {
	$r = Database::exec("ALTER TABLE `clientlog` DROP INDEX `machineuuid`");
	$res[] = $r === false ? UPDATE_FAILED : UPDATE_DONE;
}
if (tableGetIndex('clientlog', ['machineuuid', 'logid']) === false) {
	$r = Database::exec("ALTER TABLE `clientlog`
		ADD INDEX `machineuuid` ( `machineuuid` , `logid` )");
	$res[] = $r === false ? UPDATE_FAILED : UPDATE_DONE;
}

// 2019-02-20: Add constraint for machineuuid
if (tableExists('machine')) {
	$rr = tableAddConstraint('clientlog', 'machineuuid', 'machine', 'machineuuid',
		'ON DELETE SET NULL ON UPDATE CASCADE', true);
	if ($rr === UPDATE_FAILED) {
		// The table might still be populated with orphaned rows
		$dups = Database::queryColumnArray("SELECT DISTINCT l.machineuuid FROM clientlog l LEFT JOIN machine m USING (machineuuid) WHERE m.machineuuid IS NULL");
		if (!empty($dups)) {
			Database::exec("UPDATE clientlog SET machineuuid = NULL WHERE machineuuid IN (:dups)", ['dups' => $dups]);
			$rr = tableAddConstraint('clientlog', 'machineuuid', 'machine', 'machineuuid',
				'ON DELETE SET NULL ON UPDATE CASCADE');
		}
	}
	$res[] = $rr;
} elseif (Module::get('statistics') !== false) {
	$res[] = UPDATE_RETRY;
}

// Create response for browser

if (in_array(UPDATE_DONE, $res)) {
	finalResponse(UPDATE_DONE, 'Tables created successfully');
}

finalResponse(UPDATE_NOOP, 'Everything already up to date');