Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1 schema patch adjustments #1413

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 55 additions & 43 deletions admin/schemamanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
?>
<div style="margin:15px;">
<label>Current version: </label>
<?php echo $curentVersion?$curentVersion:'no schema detected'; ?>
<?php echo $curentVersion ? $curentVersion : 'no schema detected'; ?>
</div>
<?php
if($verHistory){
Expand All @@ -80,48 +80,60 @@
</div>
<?php
}
?>
<fieldset style="width:800px">
<legend>Database Schema Assistant</legend>
<div class="info-div">Enter login criteria for database user that has full DDL privileges (e.g. create/alter tables, routines, indexes, etc.).<br>
We recommend creating a backup of the database before applying any database patches.</div>
<form name="databaseMaintenanceForm" action="schemamanager.php" method="post">
<div class="form-section">
<label>Schema: </label>
<select name="schemaCode">
<option value="baseInstall" <?php echo !$curentVersion || $curentVersion < 1 ? 'selected' : ''; ?>>New Install (ver. 3.0)</option>
<option value="1.1" <?php echo $curentVersion == 1.0 ? 'selected' : ''; ?>>Schema Patch 1.1</option>
<option value="1.2" <?php echo $curentVersion == 1.1 ? 'selected' : ''; ?>>Schema Patch 1.2</option>
<option value="3.0" <?php echo $curentVersion == 1.2 ? 'selected' : ''; ?>>Schema Patch 3.0</option>
<option value="" <?php echo $curentVersion == 3.0 ? 'selected' : ''; ?>>Schema is Current - nothing to do</option>
</select>
</div>
<div class="form-section">
<label for="username">Username: </label>
<input id="username" name="username" type="text" value="<?= htmlspecialchars($username, ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE) ?>" required autocomplete="off">
</div>
<div class="form-section">
<label for="password">Password: </label>
<input id="password" name="password" type="password" value="" required autocomplete="off">
</div>
<div class="form-section">
<label>Host:</label>
<?php echo $host; ?>
</div>
<div class="form-section">
<label>Database:</label>
<?php echo $database; ?>
</div>
<div class="form-section">
<label>Port:</label>
<?php echo $port; ?>
</div>
<div class="form-section">
<button name="action" type="submit" value="installSchema">Install</button>
</div>
</form>
</fieldset>
<?php
if(!$curentVersion && $schemaManager->getErrorMessage() == 'ERROR_NO_CONNECTION'){
echo '<h2>ERROR: unable to establish connection to database</h2>';
}
else{
?>
<fieldset style="width:800px">
<legend>Database Schema Assistant</legend>
<div class="info-div">Enter login criteria for database user that has full DDL privileges (e.g. create/alter tables, routines, indexes, etc.).<br>
We recommend creating a backup of the database before applying any database patches.</div>
<form name="databaseMaintenanceForm" action="schemamanager.php" method="post">
<div class="form-section">
<label>Schema: </label>
<select name="schemaCode">
<?php
if($curentVersion){
$schemaPatchArr = array('1.1', '1.2', '3.0', '3.1');
foreach($schemaPatchArr as $schemaOption){
if($schemaOption > $curentVersion) echo '<option value="' . $schemaOption . '">Schema Patch ' . $schemaOption . '</option>';
}
echo '<option value="">Schema is Current - nothing to do</option>';
}
else{
echo '<option value="baseInstall">New Install (ver. 3.0)</option>';
}
?>
</select>
</div>
<div class="form-section">
<label for="username">Username: </label>
<input id="username" name="username" type="text" value="<?= htmlspecialchars($username, ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE) ?>" required autocomplete="off">
</div>
<div class="form-section">
<label for="password">Password: </label>
<input id="password" name="password" type="password" value="" required autocomplete="off">
</div>
<div class="form-section">
<label>Host:</label>
<?php echo $host; ?>
</div>
<div class="form-section">
<label>Database:</label>
<?php echo $database; ?>
</div>
<div class="form-section">
<label>Port:</label>
<?php echo $port; ?>
</div>
<div class="form-section">
<button name="action" type="submit" value="installSchema">Install</button>
</div>
</form>
</fieldset>
<?php
}
}
else{
echo '<div>Not Authorized</div>';
Expand Down
78 changes: 50 additions & 28 deletions classes/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,35 @@ public function installPatch(){
$sql = trim($sql, ',');
if($sql){
//$this->logOrEcho('Statement: ' . $sql, 1);
if($this->conn->query($sql)){
$this->logOrEcho('Success!', 1);
if(isset($this->warningArr['updated'])){
$this->logOrEcho('Following adjustments applied:', 2);
foreach($this->warningArr['updated'] as $colName => $adjustStr){
$this->logOrEcho($colName . ': ' . $adjustStr, 3);
try{
if($this->conn->query($sql)){
$this->logOrEcho('Success!', 1);
if(isset($this->warningArr['updated'])){
$this->logOrEcho('Following adjustments applied:', 2);
foreach($this->warningArr['updated'] as $colName => $adjustStr){
$this->logOrEcho($colName . ': ' . $adjustStr, 3);
}
unset($this->warningArr['updated']);
}
unset($this->warningArr['updated']);
}
if($this->warningArr){
//Add these warnings to amendment file since they should be reapplied
if(!$this->amendmentFH) $this->amendmentFH = fopen($this->amendmentPath, 'w');
$this->logOrEcho('Following fragments excluded due to errors:', 2);
$failedSql = '';
foreach($this->warningArr as $errCode => $errArr){
foreach($errArr as $colName => $frag){
if($errCode == 'exists') $this->logOrEcho($colName.' already exists ', 3);
elseif($errCode == 'missing') $this->logOrEcho($colName.' does not exists ', 3);
$failedSql .= $frag;
if($this->warningArr){
//Add these warnings to amendment file since they should be reapplied
if(!$this->amendmentFH) $this->amendmentFH = fopen($this->amendmentPath, 'w');
$this->logOrEcho('Following fragments excluded due to errors:', 2);
$failedSql = '';
foreach($this->warningArr as $errCode => $errArr){
foreach($errArr as $colName => $frag){
if($errCode == 'exists') $this->logOrEcho($colName.' already exists ', 3);
elseif($errCode == 'missing') $this->logOrEcho($colName.' does not exists ', 3);
$failedSql .= $frag;
}
}
$failedSql = trim($failedSql, ', ') . ';';
fwrite($this->amendmentFH, '# '.$targetTable."\n");
fwrite($this->amendmentFH, $failedSql . "\n\n");
}
$failedSql = trim($failedSql, ', ') . ';';
fwrite($this->amendmentFH, '# '.$targetTable."\n");
fwrite($this->amendmentFH, $failedSql . "\n\n");
}
}
else{
catch(Exception $e){
$sql = trim($sql,', ') . ';';
if(!$this->amendmentFH) $this->amendmentFH = fopen($this->amendmentPath, 'w');
fwrite($this->amendmentFH, '# ERROR: '.$this->conn->error."\n\n");
Expand Down Expand Up @@ -143,10 +145,14 @@ private function readSchemaFile(){
if($this->targetSchema == 'baseInstall'){
$filename .= '/config/schema/3.0/db_schema-3.0.sql';
}
else{
elseif($this->targetSchema <= 3){
$filename .= '/config/schema/1.0/patches/db_schema_patch-'.$this->targetSchema.'.sql';
}
else{
$filename .= '/config/schema/3.0/patches/db_schema_patch-'.$this->targetSchema.'.sql';
}
if(file_exists($filename)){
$this->logOrEcho('Evaluating DB schema file: ' . $filename);
if($fileHandler = fopen($filename, 'r')){
$sqlArr = array();
$cnt = 1;
Expand Down Expand Up @@ -188,7 +194,8 @@ private function setActiveTable($targetTable){
if($targetTable){
$this->activeTableArr = array();
$sql = 'SHOW COLUMNS FROM ' . $targetTable;
if($rs = $this->conn->query($sql)){
try{
$rs = $this->conn->query($sql);
while($r = $rs->fetch_object()){
$fieldName = strtolower($r->Field);
$type = $r->Type;
Expand All @@ -201,7 +208,7 @@ private function setActiveTable($targetTable){
}
$rs->free();
}
else{
catch(Exception $e){
$this->logOrEcho('ERROR: '.$this->conn->error, 2);
$this->logOrEcho($sql, 2);
}
Expand Down Expand Up @@ -244,8 +251,14 @@ private function setDatabaseConnection(){
return false;
}
$password = $_POST['password'];
$this->conn = new mysqli($this->host, $this->username, $password, $this->database, $this->port);
if($this->conn->connect_error){
try{
$this->conn = new mysqli($this->host, $this->username, $password, $this->database, $this->port);
if($this->conn->connect_error){
$this->logOrEcho('Connection error: ' . $this->conn->connect_error);
return false;
}
}
catch(Exception $e){
$this->logOrEcho('Connection error: ' . $this->conn->connect_error);
return false;
}
Expand All @@ -260,7 +273,16 @@ public function getVersionHistory(){
$password = $_POST['password'];
$this->conn = new mysqli($this->host, $this->username, $password, $this->database, $this->port);
}
if(!$this->conn) return false;
if(!$this->conn){
$this->errorMessage = 'ERROR_NO_CONNECTION';
return false;
}
//Check to see if a base schema exists
if($rs = $this->conn->query('SHOW TABLES')){
if(!$rs->num_rows) return false;
$rs->free();
}
//Get version history
$sql = 'SELECT versionNumber, dateApplied FROM schemaversion ORDER BY id';
if($rs = $this->conn->query($sql)){
$versionHistory = array();
Expand Down
26 changes: 0 additions & 26 deletions config/schema/3.0/dev/proposed_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,6 @@ CREATE TABLE `omoccurrencetypes` (
) ENGINE=InnoDB;


--
-- Table structure for table `omoccurresource`
--

CREATE TABLE `omoccurresource` (
`resourceID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`occid` int(10) unsigned NOT NULL,
`reourceTitle` varchar(45) NOT NULL,
`resourceType` varchar(45) NOT NULL,
`uri` varchar(250) NOT NULL,
`source` varchar(45) DEFAULT NULL,
`resourceIdentifier` varchar(45) DEFAULT NULL,
`notes` varchar(250) DEFAULT NULL,
`modifiedUid` int(10) unsigned DEFAULT NULL,
`createdUid` int(10) unsigned DEFAULT NULL,
`initialTimestamp` timestamp NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`resourceID`),
KEY `FK_omoccurresource_occid_idx` (`occid`),
KEY `FK_omoccurresource_modUid_idx` (`modifiedUid`),
KEY `FK_omoccurresource_createdUid_idx` (`createdUid`),
CONSTRAINT `FK_omoccurresource_createdUid` FOREIGN KEY (`createdUid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_omoccurresource_modUid` FOREIGN KEY (`modifiedUid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_omoccurresource_occid` FOREIGN KEY (`occid`) REFERENCES `omoccurrences` (`occid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;


--
-- Table structure for table `taxanestedtree`
--
Expand Down
Loading