From 0baef68ed0c3a4ef8e52493ffd6bca78a4da9064 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Jun 2014 12:16:10 +0200 Subject: [PATCH] Added a cli interface, Added a new worflow to manange sql scripts (pre/post), Locking SQL "save file" when revision is run, Fixed the way revisions are saved (revision/xxx.sql, etc ...), Fixed javascript jquery/prototype (mostly moving to jquery). --- DBV.php | 127 ++++++++++++++++++++++++++++++++-- cli.php | 39 +++++++++++ templates/revision-single.php | 4 +- templates/revisions.php | 90 ++++++++++++++---------- 4 files changed, 217 insertions(+), 43 deletions(-) create mode 100644 cli.php diff --git a/DBV.php b/DBV.php index 15f7fe1..4662709 100644 --- a/DBV.php +++ b/DBV.php @@ -35,6 +35,11 @@ class DBV_Exception extends Exception class DBV { + + const CLI_STEP_ALL = 'all'; + const CLI_STEP_PRE = 'pre'; + const CLI_STEP_POST = 'post'; + protected $_action = "index"; protected $_adapter; @@ -155,10 +160,11 @@ public function revisionsAction() if (!$this->_runFile($file)) { break 2; } + else{ + $this->_setRevisionIndex($revision.'/'.basename($file)); + } } } - - $this->_setRevisionIndex($revision); $this->confirm(__("Executed revision #{revision}", array('revision' => "$revision"))); } @@ -218,11 +224,95 @@ public function addRevisionFolderAction() $this->_json(array('ok' => false, 'message' => __("Cannot create revision #{revision}!", array('revision' => "$revision")))); return; } + else{ + file_put_contents($dir.'/pre.sql', '-- auto-generated pre.sql'); + file_put_contents($dir.'/post.sql', '-- auto-generated post.sql'); + + chmod($dir, 0777); + chmod($dir.'/pre.sql', 0777); + chmod($dir.'/post.sql', 0777); + } } $this->_json(array('ok' => true, 'message' => __("Revision #{revision} successfully added!", array('revision' => "$revision")), 'html' => $this->_templateRevision($revision))); } + public function _cliAction($revision = 0, $step = self::CLI_STEP_ALL){ + + $ranRevisions = $this->_getAllRevisions(); /// Toutes les revisions executées + $allRevisions = $this->_getRevisions(); /// Toutes les revisions + $revisionsToRun = array_diff($allRevisions, $ranRevisions); /// Différence (revisions a lancer) + + if( in_array($revision, $revisionsToRun) ){ + echo "Running '$step' single revision [$revision] ...".PHP_EOL; + } + else{ + $revs = implode(', ', $revisionsToRun); + echo "Running '$step' new revisions [$revs] ...".PHP_EOL; + } + + foreach($revisionsToRun as $revision){ + $this->_runFiles($revision, $files, $step); + } + + //echo json_encode($revisionsToRun) ; + //$this->_json($revisionsToRun); + + + /*$final_revision = isset($_POST['revision']) ? intval($_POST['revision']) : 0; + $current_revision = $this->_getCurrentRevision(); + $revisions = $this->_getRevisions(); + + foreach($revisions as $revision){ + + //move forward + if($revision > $current_revision && $revision <= $final_revision){ + + $files = $this->_getRevisionFiles($revision); + if (count($files)) { + foreach ($files as $file) { + $file = DBV_REVISIONS_PATH . DS . $revision . DS . $file; + if (!$this->_runFile($file)) { + break 2; + } + } + } + + //rollback + }elseif($revision <= $current_revision && $revision > $final_revision){ + + $files = $this->_getRevisionRollbackFiles($revision); + + if (count($files)) { + foreach ($files as $file) { + $file = DBV_REVISIONS_PATH . DS . $revision . DS . 'rollback' . DS . $file; + if (!$this->_runFile($file)) { + break 2; + } + } + } + } + } + + $this->_setCurrentRevision($final_revision); + + $this->confirm(__("Jumped to revision #{revision}", array('revision' => "$final_revision"))); + if ($this->_isXMLHttpRequest()) { + $return = array( + 'messages' => array(), + 'revision' => $this->_getCurrentRevision() + ); + foreach ($this->_log as $message) { + $return['messages'][$message['type']][] = $message['message']; + } + $this->_json($return); + + } else { + $this->indexAction(); + }*/ + } + + /** This looks quite barbarian but ... well */ public function _templateRevision($revision){ ob_start(); include DBV_ROOT_PATH.DS.'templates/revision-single.php'; @@ -261,6 +351,29 @@ protected function _exportSchemaObject($item) $this->error(($e->getCode() ? "[{$e->getCode()}] " : '') . $e->getMessage()); } } + + /// CLI specific function (need to run PRE before ...) + protected function _runFiles($revision, $files, $step){ + $result = false; + $files = $this->_getRevisionFiles($revision); + if(count($files)){ + foreach($files as $file){ + switch($step){ + case DBV::CLI_STEP_ALL: + $result = $result && $this->_runFile($file); + echo "Executing $revision/$file ...".PHP_EOL; + break; + case DBV::CLI_STEP_PRE: case DBV::CLI_STEP_POST: + if(preg_match("#^".$step."#", $file)){ + $result = $result && $this->_runFile($file); + echo "Executing $revision/$file ...".PHP_EOL; + } + break; + } + } + } + return $result; + } protected function _runFile($file) { @@ -474,7 +587,13 @@ protected function _isRan($revision) return ($this->_getCurrentRevision() >= $revision); break; case 'ALL': - return in_array($revision, $this->_getAllRevisions()); + $files = $this->_getRevisionFiles($revision); + $allRevisions = $this->_getAllRevisions(); + $isRan = true; + foreach($files as $file){ + $isRan = $isRan && in_array($revision.'/'.$file, $allRevisions); + } + return $isRan; break; default: $this->error("Incorrect revision index specified"); @@ -493,7 +612,7 @@ protected function _getRevisionFiles($revision) } } - sort($return, SORT_REGULAR); + rsort($return, SORT_STRING); /// Modified sort to get pre > post return $return; } diff --git a/cli.php b/cli.php new file mode 100644 index 0000000..7775fae --- /dev/null +++ b/cli.php @@ -0,0 +1,39 @@ + [revision_id] + + pre|post|all Defines the segment to run + revision_id Pass a single revision id to run, otherwise, will run all new revisions + +"; +} + +$dbv = DBV::instance(); + +/// Définition des arguments pour l'action +$revision = 0; +$step = DBV::CLI_STEP_ALL; + +switch($argv[1]){ + case DBV::CLI_STEP_ALL: case DBV::CLI_STEP_PRE: case DBV::CLI_STEP_POST: + $step = $argv[1]; + break; + default: + die('Unknown step'); +} + +if(isset($argv[2])){ + $revision = $argv[2]; +} + +$dbv->_cliAction($revision, $step); diff --git a/templates/revision-single.php b/templates/revision-single.php index e307bcf..9808940 100644 --- a/templates/revision-single.php +++ b/templates/revision-single.php @@ -32,7 +32,9 @@
- + + +
diff --git a/templates/revisions.php b/templates/revisions.php index 32b522a..cc0ea5a 100644 --- a/templates/revisions.php +++ b/templates/revisions.php @@ -1,16 +1,26 @@ -

-revisions) && count($this->revisions)) { ?> +

+ +
+ + +
+ +
+
-
+
+ revisions) || count($this->revisions)==0): ?> +
+
    +
  • '' . DBV_REVISIONS_PATH . '')) ?>
  • +
+
+ +

-   -
- - -

@@ -31,11 +41,9 @@
- -
- '' . DBV_REVISIONS_PATH . '')) ?> -
- + + +