Skip to content

Commit

Permalink
Added a cli interface, Added a new worflow to manange sql scripts (pr…
Browse files Browse the repository at this point in the history
…e/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).
  • Loading branch information
alex committed Jun 10, 2014
1 parent c929120 commit 0baef68
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 43 deletions.
127 changes: 123 additions & 4 deletions DBV.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => "<strong>$revision</strong>")));
}
Expand Down Expand Up @@ -218,11 +224,95 @@ public function addRevisionFolderAction()
$this->_json(array('ok' => false, 'message' => __("Cannot create revision #{revision}!", array('revision' => "<strong>$revision</strong>"))));
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' => "<strong>$revision</strong>")), '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' => "<strong>$final_revision</strong>")));
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';
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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");
Expand All @@ -493,7 +612,7 @@ protected function _getRevisionFiles($revision)
}
}

sort($return, SORT_REGULAR);
rsort($return, SORT_STRING); /// Modified sort to get pre > post
return $return;
}

Expand Down
39 changes: 39 additions & 0 deletions cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

define('CLI_ROOT_PATH', dirname(__FILE__));

require_once CLI_ROOT_PATH . DIRECTORY_SEPARATOR . 'config.php';
require_once CLI_ROOT_PATH . DIRECTORY_SEPARATOR . 'lib/functions.php';
require_once CLI_ROOT_PATH . DIRECTORY_SEPARATOR . 'DBV.php';

if(empty($argv[1]) || $argv[1] == 'help'){
echo "DBV Command-Line Tool
Usage
php -f cli.php <pre|post|all> [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);
4 changes: 3 additions & 1 deletion templates/revision-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
<div id="revision-file-<?php echo $revision; ?>-<?php echo ++$i; ?>">
<div class="log"></div>
<div class="alert alert-info heading">
<button data-role="editor-save" data-revision="<?php echo $revision; ?>" data-file="<?php echo $file; ?>" type="button" class="btn btn-mini btn-info pull-right" style="margin-top: -1px;"><?php echo __('Save file') ?></button>
<?php if(!$ran): ?>
<button data-role="editor-save" data-revision="<?php echo $revision; ?>" data-file="<?php echo $file; ?>" type="button" class="btn btn-mini btn-info pull-right" style="margin-top: -1px;"><?php echo __('Save file') ?></button>
<?php endif; ?>
<strong class="alert-heading"><?php echo $file; ?></strong>
</div>
<textarea data-role="editor" name="revision_files[<?php echo $revision; ?>][<?php echo $file; ?>]" rows="<?php echo $lines + 1; ?>"><?php echo $content; ?></textarea>
Expand Down
90 changes: 52 additions & 38 deletions templates/revisions.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<h2><?php echo __('Revisions'); ?></h2>
<?php if (isset($this->revisions) && count($this->revisions)) { ?>
<h2 class="pull-left"><?php echo __('Revisions'); ?></h2>

<div class="input-append pull-right">
<input type="text" id="revision_id" value="" class="input-large" placeholder="revision #id" />
<button id="add_revision" class="btn btn-success">New revision</button>
</div>

<div style="clear:both"></div>

<form method="post" action="" class="nomargin" id="revisions">
<div class="log"></div>
<div class="log">
<?php if (!isset($this->revisions) || count($this->revisions)==0): ?>
<div class="alert alert-error"><button class="close pull-right">×</button>
<ul class="unstyled nomargin">
<li class="last"><?php echo __('No revisions in #{path}', array('path' => '<strong>' . DBV_REVISIONS_PATH . '</strong>')) ?></li>
</ul>
</div>
<?php endif; ?>
</div>


<h2>
<input type="submit" class="btn btn-primary" value="Run selected revisions" />
&nbsp;
<div class="input-append pull-right">
<input type="text" id="revision_id" name="revision_id" value="" class="input-large" placeholder="revision #id" />
<button id="add_revision" class="btn btn-success">New revision</button>
</div>
</h2>


Expand All @@ -31,46 +41,40 @@
</table>
<input type="submit" class="btn btn-primary" value="Run selected revisions" />
</form>
<?php } else { ?>
<div class="alert alert-info nomargin">
<?php echo __('No revisions in #{path}', array('path' => '<strong>' . DBV_REVISIONS_PATH . '</strong>')) ?>
</div>
<?php } ?>



<script type="text/javascript">
document.observe('dom:loaded', function () {
var form = $('revisions');
if (!form) {
return;
}

var textareas = form.select('textarea');
textareas.each(function (textarea) {
textarea['data-editor'] = CodeMirror.fromTextArea(textarea, {
mode: "text/x-mysql",
tabMode: "indent",
matchBrackets: true,
autoClearEmptyLines: true,
lineNumbers: true,
theme: 'default'
function reloadCodeMirror(){
var textareas = $j('#revisions').find('textarea');
textareas.each(function(){
this['data-editor'] = CodeMirror.fromTextArea(this, {
mode: "text/x-mysql",
tabMode: "indent",
matchBrackets: true,
autoClearEmptyLines: true,
lineNumbers: true,
theme: 'default'
});
});
});
}

$$('.revision-handle').invoke('observe', 'click', function (event) {
var element = event.findElement('.revision-handle');
var container = element.up('td').down('.revision-files');
if (container) {
container.toggle();
if (!container.visible()) {
return;
}
reloadCodeMirror();

var textareas = container.select('textarea[data-role="editor"]');
if (textareas) {
textareas.each(function (textarea) {
textarea['data-editor'].refresh();
});
}
}
$j(document).on('click', '.revision-handle', function(event){
var files = $j(this).parents('td').children('.revision-files');
files.toggle();

var textareas = $j('textarea[data-role="editor"]');
textareas.each( function(){
this['data-editor'].refresh();
});
});

$$('button[data-role="editor-save"]').invoke('observe', 'click', function (event) {
Expand Down Expand Up @@ -105,6 +109,13 @@


/** Add revision */
$j('#revision_id').on('keyup', function(event){
var key = event.keyCode || event.which;
if(key == 13){
$j('#add_revision').trigger('click');
}
});

$j(document).on('click', '#add_revision', function(event){
event.preventDefault();

Expand All @@ -123,6 +134,8 @@
render_messages('success', 'revisions', response.message);

$j('#body_revisions').prepend(response.html);

reloadCodeMirror();
}
});
});
Expand Down Expand Up @@ -181,6 +194,7 @@
<?php else: ?>
var revisions = [];
response.revision.each(function(value){
value = value.split('/')[0];
revisions[value] = value;
});
var rows = form.select('tr[data-revision]');
Expand Down

0 comments on commit 0baef68

Please sign in to comment.