Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Allows use of out-of-order revision numbers #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 60 additions & 16 deletions DBV.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -154,17 +154,16 @@ public function revisionsAction()
}
}

if ($revision > $current_revision) {
$this->_setCurrentRevision($revision);
}
$this->_markAppliedRevision($revision);

$this->confirm(__("Executed revision #{revision}", array('revision' => "<strong>$revision</strong>")));
}
}

if ($this->_isXMLHttpRequest()) {
$return = array(
'messages' => array(),
'revision' => $this->_getCurrentRevision()
'revisions' => $this->_getAppliedRevisions()
);
foreach ($this->_log as $message) {
$return['messages'][$message['type']][] = $message['message'];
Expand All @@ -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 {
Expand Down Expand Up @@ -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}<br />File should contain serialized list of revisions.", array('path' => "<strong>$path</strong>"))
);
}

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}<br />Make sure the user running DBV has adequate permissions.", array('path' => "<strong>$path</strong>"))
);
}
}

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)
Expand Down
2 changes: 1 addition & 1 deletion templates/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<?php $this->_view('schema'); ?>
</div>
</div>
<div class="span8">
<div class="span8" style="padding-left: 50px; width: 60%;">
<div id="right">
<?php $this->_view('revisions'); ?>
</div>
Expand Down
22 changes: 12 additions & 10 deletions templates/revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<tbody>
<?php foreach ($this->revisions as $revision) { ?>
<?php
$ran = $this->revision >= $revision;
$ran = in_array($revision, $this->applied_revisions);
$class = array();
if ($ran) {
$class[] = 'ran';
Expand Down Expand Up @@ -165,19 +165,21 @@
render_messages('success', 'revisions', response.messages.success, '<?php echo __('The following actions completed successfuly:'); ?>');
}

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});
Expand Down