Commit 232456ab authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-230 Removed zpush_ prefixes of the table names and table crate

constants, throw FatalExceptions on DB errors, option for getDbh() to
rethrow the PDO exception (when checking for tables), removed created_at
and updated_at fields from the users table, removed other methods not
part of the interface (GetUserDevicePermission, GetMappedUsername,
MapUsername and UnmapUsername - code will be attached to the ticket),
renamed and fixed CheckTablesHaveData so it indicates if there is data
in any table, included sql stuff to composer autoload, tweaked the
output of the migration script, don't log GetStateHash() as it logs a
lot.

Released under the Affero GNU General Public License (AGPL) version 3.
parent e5ae20a5
...@@ -43,7 +43,8 @@ ...@@ -43,7 +43,8 @@
************************************************/ ************************************************/
/** /**
* DON'T FORGET TO INSTALL THE PHP-DRIVER PACKAGE!!! * More information about the configuration on https://wiki.z-hub.io/x/xIAa
*
* STATE_SQL_ENGINE: the DB engine * STATE_SQL_ENGINE: the DB engine
* STATE_SQL_SERVER: the DB server URI or IP * STATE_SQL_SERVER: the DB server URI or IP
* STATE_SQL_PORT: the DB server port * STATE_SQL_PORT: the DB server port
...@@ -55,7 +56,8 @@ ...@@ -55,7 +56,8 @@
define('STATE_SQL_ENGINE', 'mysql'); define('STATE_SQL_ENGINE', 'mysql');
define('STATE_SQL_SERVER', 'localhost'); define('STATE_SQL_SERVER', 'localhost');
define('STATE_SQL_PORT', '3306'); define('STATE_SQL_PORT', '3306');
define('STATE_SQL_DATABASE', 'zpushstates'); define('STATE_SQL_DATABASE', 'zpush');
define('STATE_SQL_USER', 'root'); define('STATE_SQL_USER', 'root');
define('STATE_SQL_PASSWORD', ''); define('STATE_SQL_PASSWORD', '');
define('STATE_SQL_OPTIONS', serialize(array(PDO::ATTR_PERSISTENT => true))); define('STATE_SQL_OPTIONS', serialize(array(PDO::ATTR_PERSISTENT => true)));
\ No newline at end of file
...@@ -94,6 +94,7 @@ return array( ...@@ -94,6 +94,7 @@ return array(
'SendMail' => $baseDir . '/lib/request/sendmail.php', 'SendMail' => $baseDir . '/lib/request/sendmail.php',
'Settings' => $baseDir . '/lib/request/settings.php', 'Settings' => $baseDir . '/lib/request/settings.php',
'SimpleMutex' => $baseDir . '/lib/default/simplemutex.php', 'SimpleMutex' => $baseDir . '/lib/default/simplemutex.php',
'SqlStateMachine' => $baseDir . '/backend/sqlstatemachine/sqlstatemachine.php',
'StateInvalidException' => $baseDir . '/lib/exceptions/stateinvalidexception.php', 'StateInvalidException' => $baseDir . '/lib/exceptions/stateinvalidexception.php',
'StateManager' => $baseDir . '/lib/core/statemanager.php', 'StateManager' => $baseDir . '/lib/core/statemanager.php',
'StateNotFoundException' => $baseDir . '/lib/exceptions/statenotfoundexception.php', 'StateNotFoundException' => $baseDir . '/lib/exceptions/statenotfoundexception.php',
......
...@@ -101,6 +101,7 @@ class ComposerStaticInitd6749fc2fb9944bbe86b2b7d79a7852f ...@@ -101,6 +101,7 @@ class ComposerStaticInitd6749fc2fb9944bbe86b2b7d79a7852f
'SendMail' => __DIR__ . '/../..' . '/lib/request/sendmail.php', 'SendMail' => __DIR__ . '/../..' . '/lib/request/sendmail.php',
'Settings' => __DIR__ . '/../..' . '/lib/request/settings.php', 'Settings' => __DIR__ . '/../..' . '/lib/request/settings.php',
'SimpleMutex' => __DIR__ . '/../..' . '/lib/default/simplemutex.php', 'SimpleMutex' => __DIR__ . '/../..' . '/lib/default/simplemutex.php',
'SqlStateMachine' => __DIR__ . '/../..' . '/backend/sqlstatemachine/sqlstatemachine.php',
'StateInvalidException' => __DIR__ . '/../..' . '/lib/exceptions/stateinvalidexception.php', 'StateInvalidException' => __DIR__ . '/../..' . '/lib/exceptions/stateinvalidexception.php',
'StateManager' => __DIR__ . '/../..' . '/lib/core/statemanager.php', 'StateManager' => __DIR__ . '/../..' . '/lib/core/statemanager.php',
'StateNotFoundException' => __DIR__ . '/../..' . '/lib/exceptions/statenotfoundexception.php', 'StateNotFoundException' => __DIR__ . '/../..' . '/lib/exceptions/statenotfoundexception.php',
......
...@@ -62,37 +62,10 @@ try { ...@@ -62,37 +62,10 @@ try {
define('BASE_PATH_CLI', ZPUSH_BASE_PATH ."/"); define('BASE_PATH_CLI', ZPUSH_BASE_PATH ."/");
set_include_path(get_include_path() . PATH_SEPARATOR . ZPUSH_BASE_PATH); set_include_path(get_include_path() . PATH_SEPARATOR . ZPUSH_BASE_PATH);
include_once('lib/core/zpushdefs.php'); require_once 'vendor/autoload.php';
include_once('lib/core/zpush.php');
include_once('lib/core/zlog.php'); if (!defined('ZPUSH_CONFIG')) define('ZPUSH_CONFIG', 'config.php');
include_once('lib/core/statemanager.php'); include_once(ZPUSH_CONFIG);
include_once('lib/core/stateobject.php');
include_once('lib/core/asdevice.php');
include_once('lib/interface/ichanges.php');
include_once('lib/interface/iexportchanges.php');
include_once('lib/interface/iimportchanges.php');
include_once('lib/core/hierarchycache.php');
include_once('lib/core/interprocessdata.php');
include_once('lib/core/syncparameters.php');
include_once('lib/core/bodypreference.php');
include_once('lib/core/contentparameters.php');
include_once('lib/core/changesmemorywrapper.php');
include_once('lib/core/streamer.php');
include_once('lib/exceptions/exceptions.php');
include_once('lib/utils/utils.php');
include_once('lib/request/request.php');
include_once('lib/request/requestprocessor.php');
include_once('lib/interface/ibackend.php');
include_once('lib/interface/ichanges.php');
include_once('lib/interface/iexportchanges.php');
include_once('lib/interface/iimportchanges.php');
include_once('lib/interface/isearchprovider.php');
include_once('lib/interface/istatemachine.php');
include_once('lib/syncobjects/syncobject.php');
include_once('lib/syncobjects/syncfolder.php');
include_once('config.php');
include_once('lib/default/filestatemachine.php');
include_once('backend/sqlstatemachine/sqlstatemachine.php');
if (STATE_MACHINE != 'FILE') { if (STATE_MACHINE != 'FILE') {
...@@ -105,7 +78,6 @@ try { ...@@ -105,7 +78,6 @@ try {
$migrate = new StateMigratorFileToDB(); $migrate = new StateMigratorFileToDB();
if (!$migrate->MigrationNecessary()) { if (!$migrate->MigrationNecessary()) {
print("Migration script was run before and eventually no migration is necessary." . PHP_EOL);
exit(1); exit(1);
} }
...@@ -134,8 +106,8 @@ class StateMigratorFileToDB { ...@@ -134,8 +106,8 @@ class StateMigratorFileToDB {
print("StateMigratorFileToDB->MigrationNecessary(): checking if migration is necessary." . PHP_EOL); print("StateMigratorFileToDB->MigrationNecessary(): checking if migration is necessary." . PHP_EOL);
try { try {
$this->dbsm = new SqlStateMachine(); $this->dbsm = new SqlStateMachine();
if ($this->dbsm->checkTablesHaveData()) { if ($this->dbsm->DoTablesHaveData()) {
print ("All state tables have data already. A migration is not necessary." . PHP_EOL); print ("Tables already have data. Migration aborted. Drop database or truncate tables and try again." . PHP_EOL);
return false; return false;
} }
} }
...@@ -153,6 +125,9 @@ class StateMigratorFileToDB { ...@@ -153,6 +125,9 @@ class StateMigratorFileToDB {
*/ */
public function DoMigration() { public function DoMigration() {
print("StateMigratorFileToDB->DoMigration(): Starting migration routine." . PHP_EOL); print("StateMigratorFileToDB->DoMigration(): Starting migration routine." . PHP_EOL);
$starttime = time();
$deviceCount = 0;
$stateCount = 0;
try { try {
// Fix hierarchy folder data before starting the migration // Fix hierarchy folder data before starting the migration
ZPushAdmin::FixStatesHierarchyFolderData(); ZPushAdmin::FixStatesHierarchyFolderData();
...@@ -166,29 +141,33 @@ class StateMigratorFileToDB { ...@@ -166,29 +141,33 @@ class StateMigratorFileToDB {
// get all state information for all devices // get all state information for all devices
$alldevices = $this->fsm->GetAllDevices(false); $alldevices = $this->fsm->GetAllDevices(false);
foreach ($alldevices as $devid) { foreach ($alldevices as $devid) {
$deviceCount++;
$lowerDevid = strtolower($devid); $lowerDevid = strtolower($devid);
$allStates = $this->fsm->GetAllStatesForDevice($lowerDevid); $allStates = $this->fsm->GetAllStatesForDevice($lowerDevid);
printf("Processing device: %s with %d states\t", $devid, count($allStates)); printf("Processing device: %s with %s states\t", str_pad($devid,35), str_pad(count($allStates), 4, ' ',STR_PAD_LEFT));
$migrated = 0;
foreach ($allStates as $stateInfo) { foreach ($allStates as $stateInfo) {
$state = $this->fsm->GetState($lowerDevid, $stateInfo['type'], $stateInfo['uuid'], (int) $stateInfo['counter'], false); $state = $this->fsm->GetState($lowerDevid, $stateInfo['type'], $stateInfo['uuid'], (int) $stateInfo['counter'], false);
$this->dbsm->SetState($state, $lowerDevid, $stateInfo['type'], (empty($stateInfo['uuid']) ? NULL : $stateInfo['uuid']), (int) $stateInfo['counter']); $this->dbsm->SetState($state, $lowerDevid, $stateInfo['type'], (empty($stateInfo['uuid']) ? NULL : $stateInfo['uuid']), (int) $stateInfo['counter']);
printf("%s\t\tsetting state:%s-%s-%d", PHP_EOL, $stateInfo['uuid'], (empty($stateInfo['type']) ? NULL : $stateInfo['type']), (int) $stateInfo['counter']); $migrated++;
} }
// link devices to users // link devices to users
$devState = $this->fsm->GetState($lowerDevid, IStateMachine::DEVICEDATA); $devState = $this->fsm->GetState($lowerDevid, IStateMachine::DEVICEDATA);
foreach ($devState->devices as $user => $dev) { foreach ($devState->devices as $user => $dev) {
$this->dbsm->LinkUserDevice($user, $dev->deviceid, date("Y-m-d H:i:s", $dev->firstsynctime), date("Y-m-d H:i:s", $dev->lastupdatetime)); $this->dbsm->LinkUserDevice($user, $dev->deviceid);
} }
print("\tcompleted" . PHP_EOL); print(" completed migration of $migrated states" . PHP_EOL);
$stateCount += $migrated;
} }
} }
catch (ZPushException $ex) { catch (ZPushException $ex) {
print (PHP_EOL . "Something went wrong during the migration. The script will now exit." . PHP_EOL); print (PHP_EOL . "Something went wrong during the migration. The script will now exit." . PHP_EOL);
die(get_class($ex) . ": ". $ex->getMessage() . PHP_EOL); die(get_class($ex) . ": ". $ex->getMessage() . PHP_EOL);
} }
print("StateMigratorFileToDB->DoMigration(): Migration completed successfuly." . PHP_EOL); $timeSpent = gmdate("H:i:s", time() - $starttime);
printf(PHP_EOL ."StateMigratorFileToDB->DoMigration(): Migration completed successfuly. Migrated %d devices with %d states in %s.".PHP_EOL.PHP_EOL, $deviceCount, $stateCount, $timeSpent);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment