Skip to content

Commit 95485b6

Browse files
committed
Upgrade to new technicalguru/database version
1 parent d9c6997 commit 95485b6

File tree

5 files changed

+82
-99
lines changed

5 files changed

+82
-99
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php" : ">=7.0.0",
1515
"technicalguru/vault" : "^1",
1616
"technicalguru/utils" : "^1",
17-
"technicalguru/database" : "^1",
17+
"technicalguru/database" : "^1.1",
1818
"technicalguru/i18n" : "^1",
1919
"technicalguru/rest-client" : "^1",
2020
"technicalguru/email" : "^1",

src/WebApp/DataModel/LogDAO.php

+15-19
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,25 @@
66

77
class LogDAO extends \TgDatabase\DAO {
88

9-
public function __construct($database) {
10-
parent::__construct($database, '#__error_log', 'WebApp\\DataModel\\Log');
11-
$this->initialize();
9+
public function __construct($database, $checkTable = FALSE) {
10+
parent::__construct($database, '#__error_log', 'WebApp\\DataModel\\Log', 'uid', $checkTable);
1211
}
1312

14-
protected function initialize() {
15-
// Check the table existence
16-
$res = $this->database->query('SELECT * FROM '.$this->tableName);
13+
public function createTable() {
14+
// Try to create
15+
$sql =
16+
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
17+
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
18+
'`log_date` datetime NOT NULL,'.
19+
'`log_text` text COLLATE utf8mb4_bin NOT NULL,'.
20+
'`log_sent` datetime DEFAULT NULL,'.
21+
'PRIMARY KEY (`uid`)'.
22+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
23+
$res = $this->database->query($sql);
1724
if ($res === FALSE) {
18-
// Try to create
19-
$sql =
20-
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
21-
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
22-
'`log_date` datetime NOT NULL,'.
23-
'`log_text` text COLLATE utf8mb4_bin NOT NULL,'.
24-
'`log_sent` datetime DEFAULT NULL,'.
25-
'PRIMARY KEY (`uid`)'.
26-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
27-
$res = $this->database->query($sql);
28-
if ($res === FALSE) {
29-
throw new WebAppException('Cannot create log table: '.$this->database->error());
30-
}
25+
throw new WebAppException('Cannot create log table: '.$this->database->error());
3126
}
27+
return TRUE;
3228
}
3329

3430
}

src/WebApp/DataModel/SessionDAO.php

+17-22
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,27 @@
88
/** DataModel class for sessions */
99
class SessionDAO extends \TgDatabase\DAO {
1010

11-
public function __construct($database) {
12-
parent::__construct($database, '#__sessions', 'WebApp\\DataModel\\Session', 'uid');
13-
$this->initialize();
11+
public function __construct($database, $checkTable = FALSE) {
12+
parent::__construct($database, '#__sessions', 'WebApp\\DataModel\\Session', 'uid', $checkTable);
1413
}
1514

16-
17-
protected function initialize() {
18-
// Check the sessions table
19-
$res = $this->database->query('SELECT * FROM '.$this->tableName);
15+
public function createTable() {
16+
// Create it (try)
17+
$sql =
18+
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ( '.
19+
'`uid` VARCHAR(50) NOT NULL COMMENT \'ID of session\', '.
20+
'`creation_time` DATETIME NOT NULL COMMENT \'When was session created\', '.
21+
'`update_time` DATETIME NOT NULL COMMENT \'When was session last updated\', '.
22+
'`expiry_time` DATETIME NOT NULL COMMENT \'When will session expire\', '.
23+
'`data` TEXT COLLATE utf8mb4_bin NOT NULL COMMENT \'session data\', '.
24+
'`persistent` INT(1) UNSIGNED NOT NULL DEFAULT \'0\' COMMENT \'Is session persistent?\', '.
25+
'PRIMARY KEY (`uid`) '.
26+
') ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT = \'Sessions\'';
27+
$res = $this->database->query($sql);
2028
if ($res === FALSE) {
21-
// Create it (try)
22-
$sql =
23-
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ( '.
24-
'`uid` VARCHAR(50) NOT NULL COMMENT \'ID of session\', '.
25-
'`creation_time` DATETIME NOT NULL COMMENT \'When was session created\', '.
26-
'`update_time` DATETIME NOT NULL COMMENT \'When was session last updated\', '.
27-
'`expiry_time` DATETIME NOT NULL COMMENT \'When will session expire\', '.
28-
'`data` TEXT COLLATE utf8mb4_bin NOT NULL COMMENT \'session data\', '.
29-
'`persistent` INT(1) UNSIGNED NOT NULL DEFAULT \'0\' COMMENT \'Is session persistent?\', '.
30-
'PRIMARY KEY (`uid`) '.
31-
') ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT = \'Sessions\'';
32-
$res = $this->database->query($sql);
33-
if ($res === FALSE) {
34-
throw new WebAppException('Cannot create session table: '.$this->database->error());
35-
}
29+
throw new WebAppException('Cannot create session table: '.$this->database->error());
3630
}
31+
return TRUE;
3732
}
3833

3934
/** Create the given sessions */

src/WebApp/DataModel/UserDAO.php

+33-37
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,43 @@
66

77
class UserDAO extends \TgDatabase\DAO {
88

9-
public function __construct($database, $modelClass = NULL) {
10-
parent::__construct($database, '#__users', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\User');
11-
$this->initialize();
9+
public function __construct($database, $modelClass = NULL, $checkTable = FALSE) {
10+
parent::__construct($database, '#__users', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\User', 'uid', $checkTable);
1211
}
1312

14-
protected function initialize() {
15-
// Check the table existence
16-
$res = $this->database->query('SELECT * FROM '.$this->tableName);
13+
public function createTable() {
14+
// Try to create
15+
$sql =
16+
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
17+
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
18+
'`created_on` DATETIME NOT NULL,'.
19+
'`email` varchar(250) COLLATE utf8mb4_bin NOT NULL,'.
20+
'`password` varchar(150) COLLATE utf8mb4_bin NOT NULL,'.
21+
'`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'.
22+
'`roles` varchar(250) COLLATE utf8mb4_bin NOT NULL,'.
23+
'`status` varchar(20) DEFAULT \'active\' NOT NULL,'.
24+
'`data` text COLLATE utf8mb4_bin NOT NULL,'.
25+
'PRIMARY KEY (`uid`) '.
26+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
27+
$res = $this->database->query($sql);
1728
if ($res === FALSE) {
18-
// Try to create
19-
$sql =
20-
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
21-
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
22-
'`created_on` DATETIME NOT NULL,'.
23-
'`email` varchar(250) COLLATE utf8mb4_bin NOT NULL,'.
24-
'`password` varchar(150) COLLATE utf8mb4_bin NOT NULL,'.
25-
'`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'.
26-
'`roles` varchar(250) COLLATE utf8mb4_bin NOT NULL,'.
27-
'`status` varchar(20) DEFAULT \'active\' NOT NULL,'.
28-
'`data` text COLLATE utf8mb4_bin NOT NULL,'.
29-
'PRIMARY KEY (`uid`) '.
30-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
31-
$res = $this->database->query($sql);
32-
if ($res === FALSE) {
33-
throw new WebAppException('Cannot create user table: '.$this->database->error());
34-
} else {
35-
// Create initial superadmin
36-
$now = new \TgUtils\Date(time(), WFW_TIMEZONE);
37-
$password = \TgUtils\Utils::generateRandomString();
38-
$sql = 'INSERT INTO '.$this->database->quoteName($this->tableName).' (`created_on`, `email`, `password`, `name`, `roles`, `status`, `data`) VALUES '.
39-
'('.$this->database->quote($now->toMysql()).', \'[email protected]\', '.$this->database->quote($password).', \'Application Superadmin\', \'superadmin\', \'active\', \'{}\')';
40-
$res = $this->database->query($sql);
41-
if ($res === FALSE) {
42-
throw new WebAppException('Cannot create superadmin: '.$this->database->error());
43-
} else {
44-
error_log('===========> Superadmin Email: [email protected]');
45-
error_log('===========> Superadmin Password: '.$password);
46-
error_log('===========> YOU MUST CHANGE THIS IMMEDIATELY!');
47-
}
48-
}
29+
throw new WebAppException('Cannot create user table: '.$this->database->error());
4930
}
31+
32+
// Create initial superadmin
33+
$now = new \TgUtils\Date(time(), WFW_TIMEZONE);
34+
$password = \TgUtils\Utils::generateRandomString();
35+
$sql = 'INSERT INTO '.$this->database->quoteName($this->tableName).' (`created_on`, `email`, `password`, `name`, `roles`, `status`, `data`) VALUES '.
36+
'('.$this->database->quote($now->toMysql()).', \'[email protected]\', '.$this->database->quote($password).', \'Application Superadmin\', \'superadmin\', \'active\', \'{}\')';
37+
$res = $this->database->query($sql);
38+
if ($res === FALSE) {
39+
throw new WebAppException('Cannot create superadmin: '.$this->database->error());
40+
}
41+
42+
error_log('===========> Superadmin Email: [email protected]');
43+
error_log('===========> Superadmin Password: '.$password);
44+
error_log('===========> YOU MUST CHANGE THIS IMMEDIATELY!');
45+
return TRUE;
5046
}
5147

5248
public function getByEmail($email) {

src/WebApp/DataModel/UserRoleDAO.php

+16-20
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,26 @@
66

77
class UserRoleDAO extends \TgDatabase\DAO {
88

9-
public function __construct($database, $modelClass = NULL) {
10-
parent::__construct($database, '#__user_roles', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\UserRole');
11-
$this->initialize();
9+
public function __construct($database, $modelClass = NULL, $checkTable = FALSE) {
10+
parent::__construct($database, '#__user_roles', $modelClass != NULL ? $modelClass : 'WebApp\\DataModel\\UserRole', 'uid', $checkTable);
1211
}
1312

14-
protected function initialize() {
15-
// Check the table existence
16-
$res = $this->database->query('SELECT * FROM '.$this->tableName);
13+
public function createTable() {
14+
// Try to create
15+
$sql =
16+
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
17+
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
18+
'`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'.
19+
'`privileges` text COLLATE utf8mb4_bin NOT NULL,'.
20+
'`is_active` int(1) DEFAULT 1 NOT NULL,'.
21+
'PRIMARY KEY (`uid`), '.
22+
'UNIQUE(`name`) '.
23+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
24+
$res = $this->database->query($sql);
1725
if ($res === FALSE) {
18-
// Try to create
19-
$sql =
20-
'CREATE TABLE '.$this->database->quoteName($this->tableName).' ('.
21-
'`uid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,'.
22-
'`name` varchar(50) COLLATE utf8mb4_bin NOT NULL,'.
23-
'`privileges` text COLLATE utf8mb4_bin NOT NULL,'.
24-
'`is_active` int(1) DEFAULT 1 NOT NULL,'.
25-
'PRIMARY KEY (`uid`), '.
26-
'UNIQUE(`name`) '.
27-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin';
28-
$res = $this->database->query($sql);
29-
if ($res === FALSE) {
30-
throw new WebAppException('Cannot create user roles table: '.$this->database->error());
31-
}
26+
throw new WebAppException('Cannot create user roles table: '.$this->database->error());
3227
}
28+
return TRUE;
3329
}
3430

3531
}

0 commit comments

Comments
 (0)