From b775ec5041fbe05c335c3b7db3c92bf4add92de7 Mon Sep 17 00:00:00 2001 From: Israel Shirk Date: Mon, 6 May 2013 23:13:06 -0600 Subject: [PATCH] Allows use of out-of-order revision numbers in order to allow unique revision IDs with multiple developers, branches, ... --- DBV.php | 76 ++++++++++++++++++++++++++++++++--------- templates/index.php | 2 +- templates/revisions.php | 22 ++++++------ 3 files changed, 73 insertions(+), 27 deletions(-) diff --git a/DBV.php b/DBV.php index d2a9e1d..00986de 100644 --- a/DBV.php +++ b/DBV.php @@ -97,7 +97,7 @@ public function indexAction() if ($this->_getAdapter()) { $this->schema = $this->_getSchema(); $this->revisions = $this->_getRevisions(); - $this->revision = $this->_getCurrentRevision(); + $this->applied_revisions = $this->_getAppliedRevisions(); } $this->_view("index"); @@ -137,7 +137,7 @@ public function schemaAction() public function revisionsAction() { $revisions = isset($_POST['revisions']) ? array_map("intval", $_POST['revisions']) : array(); - $current_revision = $this->_getCurrentRevision(); + $applied_revisions = $this->_getAppliedRevisions(); if (count($revisions)) { sort($revisions); @@ -154,9 +154,8 @@ public function revisionsAction() } } - if ($revision > $current_revision) { - $this->_setCurrentRevision($revision); - } + $this->_markAppliedRevision($revision); + $this->confirm(__("Executed revision #{revision}", array('revision' => "$revision"))); } } @@ -164,7 +163,7 @@ public function revisionsAction() if ($this->_isXMLHttpRequest()) { $return = array( 'messages' => array(), - 'revision' => $this->_getCurrentRevision() + 'revisions' => $this->_getAppliedRevisions() ); foreach ($this->_log as $message) { $return['messages'][$message['type']][] = $message['message']; @@ -176,10 +175,10 @@ public function revisionsAction() } } - public function saveRevisionFileAction() { - $revision = intval($_POST['revision']); + $revision = time(); + if (preg_match('/^[a-z0-9\._]+$/i', $_POST['file'])) { $file = $_POST['file']; } else { @@ -330,21 +329,66 @@ protected function _getRevisions() return $return; } - protected function _getCurrentRevision() + protected function _getAppliedRevisions() { - $file = DBV_META_PATH . DS . 'revision'; - if (file_exists($file)) { - return intval(file_get_contents($file)); + $path = DBV_META_PATH . DS . 'applied_revisions'; + + // Initially try to retrieve revisions from the new-style revisions + // If it's not there, assume we have an old-style revision file + if ( file_exists( $path ) ) { + $content = file_get_contents($path); + } else { + $path = DBV_META_PATH . DS . 'revision'; + + $all_revisions = $this->_getRevisions(); + $current_revision = $this->_getCurrentRevision(); + + $applied_revisions = Array(); + + foreach ( $all_revisions as $revision ) { + if ( $revision <= $current_revision ) + $applied_revisions[] = $revision; + } + + return $applied_revisions; } - return 0; + + if (! $content ) + return Array(); + + if (! $applied_revisions = @unserialize( $content ) ) { + $this->error( + __("Couldn't read file: #{path}
File should contain serialized list of revisions.", array('path' => "$path")) + ); + } + + return $applied_revisions; } - protected function _setCurrentRevision($revision) + protected function _markAppliedRevision( $revision ) + { + $revisions = $this->_getAppliedRevisions(); + + $revisions[] = $revision; + + $revisions = array_unique($revisions); + + $content = serialize($revisions); + + if (!@file_put_contents(DBV_META_PATH . DS . 'applied_revisions', $content)) { + $this->error( + __("Couldn't write file: #{path}
Make sure the user running DBV has adequate permissions.", array('path' => "$path")) + ); + } + } + + protected function _getCurrentRevision() { $file = DBV_META_PATH . DS . 'revision'; - if (!@file_put_contents($file, $revision)) { - $this->error("Cannot write revision file"); + if (file_exists($file)) { + return intval(file_get_contents($file)); } + return 0; } protected function _getRevisionFiles($revision) diff --git a/templates/index.php b/templates/index.php index 8a3f329..477cc9b 100644 --- a/templates/index.php +++ b/templates/index.php @@ -45,7 +45,7 @@ _view('schema'); ?> -
+
diff --git a/templates/revisions.php b/templates/revisions.php index bc89261..dd7e0c5 100644 --- a/templates/revisions.php +++ b/templates/revisions.php @@ -13,7 +13,7 @@ revisions as $revision) { ?> revision >= $revision; + $ran = in_array($revision, $this->applied_revisions); $class = array(); if ($ran) { $class[] = 'ran'; @@ -165,19 +165,21 @@ render_messages('success', 'revisions', response.messages.success, ''); } - var revision = parseInt(response.revision); - if (!isNaN(revision)) { + if ( Object.prototype.toString.call(response.revisions) === '[object Array]' ) { var rows = form.select('tr[data-revision]'); - rows.each(function (row) { - row.removeClassName('ran'); - if (row.getAttribute('data-revision') > revision) { - return; - } - row.addClassName('ran'); + rows.each(function (row) { + row.removeClassName('ran'); + var row_revision_number = row.getAttribute('data-revision') * 1; + + if ( response.revisions.indexOf( row_revision_number ) < 0 ) { + return; + } + + row.addClassName('ran'); row.down('.revision-files').hide(); row.down('input[type="checkbox"]').checked = false; - }); + }); } Effect.ScrollTo('log', {duration: 0.2});