Bläddra i källkod

Added Manual Update API endpoint
Added file lock on db process upgrade

causefx 7 år sedan
förälder
incheckning
1b232713d4
2 ändrade filer med 60 tillägg och 35 borttagningar
  1. 41 35
      api/functions/api-functions.php
  2. 19 0
      api/index.php

+ 41 - 35
api/functions/api-functions.php

@@ -209,43 +209,49 @@ function createDB($path, $filename)
 // Upgrade Database
 function updateDB($oldVerNum = false)
 {
-	// Create Temp DB First
-	$migrationDB = 'tempMigration.db';
-	$pathDigest = pathinfo($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
-	if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
-		unlink($GLOBALS['dbLocation'] . $migrationDB);
-	}
-	copy($GLOBALS['dbLocation'] . $GLOBALS['dbName'], $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db');
-	@unlink($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
-	$success = createDB($GLOBALS['dbLocation'], $migrationDB);
-	try {
-		$connectOldDB = new Dibi\Connection([
-			'driver' => 'sqlite3',
-			'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
-		]);
-		$connectNewDB = new Dibi\Connection([
-			'driver' => 'sqlite3',
-			'database' => $GLOBALS['dbLocation'] . $migrationDB,
-		]);
-		$tables = $connectOldDB->fetchAll('SELECT name FROM sqlite_master WHERE type="table"');
-		foreach ($tables as $table) {
-			$data = $connectOldDB->fetchAll('SELECT * FROM ' . $table['name']);
-			foreach ($data as $row) {
-				$connectNewDB->query('INSERT into ' . $table['name'], $row);
-			}
-		}
-		$connectOldDB->disconnect();
-		$connectNewDB->disconnect();
-		// Remove Current Database
+	$tempLock = $GLOBALS['dbLocation'] . 'DBLOCK.txt';
+	if (!file_exists($tempLock)) {
+		touch($tempLock);
+		// Create Temp DB First
+		$migrationDB = 'tempMigration.db';
+		$pathDigest = pathinfo($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
 		if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
-			copy($GLOBALS['dbLocation'] . $migrationDB, $GLOBALS['dbLocation'] . $GLOBALS['dbName']);
-			//unlink($GLOBALS['dbLocation'] . $migrationDB);
+			unlink($GLOBALS['dbLocation'] . $migrationDB);
+		}
+		copy($GLOBALS['dbLocation'] . $GLOBALS['dbName'], $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db');
+		@unlink($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
+		$success = createDB($GLOBALS['dbLocation'], $migrationDB);
+		try {
+			$connectOldDB = new Dibi\Connection([
+				'driver' => 'sqlite3',
+				'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
+			]);
+			$connectNewDB = new Dibi\Connection([
+				'driver' => 'sqlite3',
+				'database' => $GLOBALS['dbLocation'] . $migrationDB,
+			]);
+			$tables = $connectOldDB->fetchAll('SELECT name FROM sqlite_master WHERE type="table"');
+			foreach ($tables as $table) {
+				$data = $connectOldDB->fetchAll('SELECT * FROM ' . $table['name']);
+				foreach ($data as $row) {
+					$connectNewDB->query('INSERT into ' . $table['name'], $row);
+				}
+			}
+			$connectOldDB->disconnect();
+			$connectNewDB->disconnect();
+			// Remove Current Database
+			if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
+				copy($GLOBALS['dbLocation'] . $migrationDB, $GLOBALS['dbLocation'] . $GLOBALS['dbName']);
+				@unlink($GLOBALS['dbLocation'] . $migrationDB);
+			}
+			writeLog('success', 'Update Function -  Migrated Old Info to new Database', 'Database');
+			unlink($tempLock);
+			return true;
+		} catch (Dibi\Exception $e) {
+			writeLog('error', 'Update Function -  Error [' . $e . ']', 'Database');
+			unlink($tempLock);
+			return false;
 		}
-		writeLog('success', 'Update Function -  Migrated Old Info to new Database', 'Database');
-		return true;
-	} catch (Dibi\Exception $e) {
-		writeLog('error', 'Update Function -  Error [' . $e . ']', 'Database');
-		return false;
 	}
 }
 

+ 19 - 0
api/index.php

@@ -1197,6 +1197,25 @@ switch ($function) {
 				break;
 		}
 		break;
+	case 'v1_update_db_manual':
+		switch ($method) {
+			case 'GET':
+				if (qualifyRequest(1)) {
+					$result['status'] = 'success';
+					$result['statusText'] = 'success';
+					$result['data'] = updateDB($GLOBALS['installedVersion']);
+				} else {
+					$result['status'] = 'error';
+					$result['statusText'] = 'API/Token invalid or not set';
+					$result['data'] = null;
+				}
+				break;
+			default:
+				$result['status'] = 'error';
+				$result['statusText'] = 'The function requested is not defined for method: ' . $method;
+				break;
+		}
+		break;
 	default:
 		//No Function Available
 		$result['status'] = 'error';