diff --git a/cosmogramme/php/_init.php b/cosmogramme/php/_init.php index 1529fdbb1e725a91c49e318bfccf4b55b26512f3..1560906cf45327143d7884fff8c4bb12c18e5eda 100644 --- a/cosmogramme/php/_init.php +++ b/cosmogramme/php/_init.php @@ -54,7 +54,7 @@ require_once($basePath.'/../storm_init.php'); require_once("classe_sql.php"); require_once("fonctions/sql.php"); - +xdebug_break(); // Lire la config chdir($cosmo_path->getBasePath()); $cfg=lireConfig($cfgfile); diff --git a/cosmogramme/php/upgrade_db.php b/cosmogramme/php/upgrade_db.php new file mode 100644 index 0000000000000000000000000000000000000000..b1a00dccf6dc34fe87dd7d8f3bce2fba875ef09b --- /dev/null +++ b/cosmogramme/php/upgrade_db.php @@ -0,0 +1,115 @@ +<?php + +$basePath = dirname(realpath(__FILE__)); +require_once 'classes/classe_cosmopaths.php'; + + +$cosmo_path = new CosmoPaths(); +define('BASE_URL', $cosmo_path->getBaseUrl()); + +require_once($basePath.'/../storm_init.php'); + +$sql = Zend_Registry::get('sql'); +$patch_level = Class_CosmoVar::find("patch_level"); + +function versionForFile($file) { + return (int)substr($file, -7, 3); +} + +function extensionForFile($file) { + return substr($file, -4); +} + +// Recup des patches a executer +$path = realpath(dirname(__FILE__)).'/../sql/patch/'; +$handle = @opendir($path); +if (!$handle) + echo('Impossible d\'ouvrir le dossier : '.$path); + +$scripts = []; +while (false !== ($fic = readdir($handle))) { + if (!in_array(extensionForFile($fic), ['.sql', '.php'])) + continue; + $numero = versionForFile($fic); + if ($numero > $patch_level->getValeur()) + $scripts[] = $path . $fic; +} + +sort($scripts); +closedir($handle); + +foreach($scripts as $script) { + $num_patch = versionForFile($script); + echo('Execution patch n° ' . $num_patch ); + + switch(extensionForFile($script)) { + case '.php': + runPhpUpgrade($script);break; + case '.sql': + runSqlUpgrade($script, $sql);break; + } + + // Ecrire le patch dans la base + $patch_level->setValeur($num_patch)->save(); +} +echo('Mise à niveau de la base effectuée avec succès'); +exit; + +function runSqlUpgrade($script, $sql) { + $num_instruction = 0; + $data = file($script); + + $instructions = []; + $contents = implode(' ', $data); + if (false !== strpos($contents, 'CREATE FUNCTION')) + $instructions[] = $contents; + else { + $instruction = ''; + foreach($data as $ligne) { + $ligne = trim($ligne); + if (!$ligne or substr($ligne,0,2)=="--") continue; + + $instruction.=$ligne.' '; + if(substr($ligne,-1)!=";") + continue; + + $instructions[] = $instruction; + $instruction = ''; + } + } + + + foreach($instructions as $instruction) { + echo($instruction."\n"); + flush(); + // Executer + $num_instruction++; + + $sql->execute($instruction); + + } +} + +function runPhpUpgrade($script) { + + set_error_handler( + function($no, $message, $file, $line, $context) { + echo('Erreur :'.$message + . " \nfile : " . $file + . " \nline : " . $line + ); + die(255); + }); + + try { + include $script; + } catch (Exception $e) { + echo("Code : ".$e->getCode()."\n"); + echo("Erreur :".$e->getMessage()); + + exit; + } +} + + +?> \ No newline at end of file