summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/install.inc.php
blob: 46a6544c5a36e5660ef2ec0135f7d2c52481c702 (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[] = tableCreate('setting_location', '
	`locationid` INT(11) NOT NULL,
	`setting` VARCHAR(28) NOT NULL,
	`value` TEXT NOT NULL,
	`displayvalue` TEXT NOT NULL,
	PRIMARY KEY (`locationid`,`setting`),
	KEY `setting` (`setting`)
');

$res[] = tableCreate('location', '
	`locationid` INT(11) NOT NULL AUTO_INCREMENT,
	`parentlocationid` INT(11) NOT NULL,
	`locationname` VARCHAR(100) NOT NULL,
	`openingtime` BLOB DEFAULT NULL,
	PRIMARY KEY (`locationid`),
	KEY `locationname` (`locationname`),
	KEY `parentlocationid` (`parentlocationid`)
');

$res[] = tableCreate('subnet', '
	`subnetid` INT(11) NOT NULL AUTO_INCREMENT,
	`startaddr` DECIMAL(39,0) UNSIGNED NOT NULL,
	`endaddr` DECIMAL(39,0) UNSIGNED NOT NULL,
	`locationid` INT(11) NOT NULL,
	PRIMARY KEY (`subnetid`),
	KEY `startaddr` (`startaddr`,`endaddr`),
	KEY `locationid` (`locationid`)
');

$res[] = tableAddConstraint('subnet', 'locationid', 'location', 'locationid',
	'ON UPDATE CASCADE ON DELETE CASCADE');
$res[] = tableAddConstraint('setting_location', 'locationid', 'location', 'locationid',
	'ON UPDATE CASCADE ON DELETE CASCADE');

// Update

// 2020-07-14 Add openingtime column to location table, then migrate data and delete the column from locationinfo
if (!tableHasColumn('location', 'openingtime')) {
	if (Database::exec("ALTER TABLE location ADD openingtime BLOB DEFAULT NULL") === false) {
		finalResponse(UPDATE_FAILED, 'Could not create openingtime column');
	}
	$res[] = UPDATE_DONE;
}
if (tableHasColumn('locationinfo_locationconfig', 'openingtime')) {
	if (Database::exec(
			"UPDATE location, locationinfo_locationconfig
			SET location.openingtime = locationinfo_locationconfig.openingtime
			WHERE location.locationid = locationinfo_locationconfig.locationid
			  AND (Length(location.openingtime) < 5 OR location.openingtime IS NULL)
			  AND Length(locationinfo_locationconfig.openingtime) > 5") === false) {
		finalResponse(UPDATE_FAILED, 'Could not migrate openingtime data from table to table');
	}
	if (Database::exec("ALTER TABLE locationinfo_locationconfig DROP COLUMN openingtime") === false) {
		finalResponse(UPDATE_FAILED, 'Could not delete old openingtime column');
	}
	$res[] = UPDATE_DONE;
}

// 2021-03-19: Fix this. No idea how this came to be, maybe during dev only? But better be safe...
Database::exec("UPDATE location SET openingtime = NULL WHERE openingtime = ''");

// Create response for browser
responseFromArray($res);