Skip to content

Instantly share code, notes, and snippets.

@tejnri
Last active December 22, 2015 13:49
Show Gist options
  • Save tejnri/6482008 to your computer and use it in GitHub Desktop.
Save tejnri/6482008 to your computer and use it in GitHub Desktop.
Dbscripts should be checked into the repository just like we checkin code. This helps us to trackdown what we have done with the database in course of time. Apart from that we also should have the ability to recreate the database from the available dbscripts. Recreating database from dbscripts require that dbscripts should be executed in datewis…
#!/usr/local/lamp_dev_64/php/bin/php
<?php
/**
* db script creator script.
*
* Dbscripts should be checked into the repository just like we
* checkin code. This helps us to trackdown what we have done with
* the database in course of time. Apart from that we also should
* have the ability to recreate the database from the available
* dbscripts. Recreating database from dbscripts require that dbscripts
* should be executed in datewise order in which they have created originally.
* To know which time a dbscript is created, we can ask the
* cms(git or svn etc) or simply, we can prefix the time stamp (YYYY-MM-DD-hh-mm-ss)
* to the name of the dbscript at the time of its creation and get
* the same results.
*
* Rather than doing it manually ie creating a dbscript and renaming it
* to the exact convention, we have wrapped the vim editor with this
* php script. After taking the content of dbscript, we save to the file
* named exactly acccording to the convention. It also looks for the branch
* name of the repository the person currently working in and also append
* it to the filename. So when you fire this script by
*
* $ ./create_dbscript
*
* It will ask you for the tagname.
*
* $ Please give tag name:
*
* Let's say you have given a tagname 'abc' and press enter it will open vim for you.
* Right now it opens vim only. So if you don't have vim, install it by
*
* $ sudo apt-get install vim (for dabian systems like ubuntu,mint etc)
* $ yum install vim (for rpm based systems like redhat, centos, fedora etc.)
*
* After that write the dbsccript into vim and save. The file will be save into
* the folder from which you invoked the command or you can you can also give the
* name of the folder as argument to the command like below.
*
* $ ./create_dbscript relative/path/to/dbscript/folder or /full/path/to/dbscript/folder
*
* In above case the dbscript will be created to the folder specified. In all the
* cases the name of the dbscript folder will be
* "YY-MM-DD-hh-mm-ss-<branch_name>-<tag_name_given>.sql"
*/
# Getting the directory into which the script is to be written
$base_dir = realpath((isset($argv[1]))? trim($argv[1]) : getcwd());
# Getting the name of the branch if its in git
$branch_name=get_branch_name();
# Ask the tagname from the user
$tag = read_input("Please give tag name:" , true);
# Create the name and path of the file into which the content is to be written
# Leave the branch name if its not available.
$date_str = date("Y-m-d-H-i-s");
$file_name = $base_dir . "/$date_str" . (($branch_name)? "-$branch_name":"") . "-$tag.sql";
# Open vim
system("vim $file_name > `tty`", $return_var);
# Figure out if the file is written successfully or not.
/**
* @todo we should check wheather the sql written is valid or not.
* It could be done by commit and rollback mechanism. If the file
* is not a valid dbscript content file. It should be deleted.
*/
if($return_var === 0){
if(is_file($file_name)){
echo "dbscript successfully written into file $file_name \n";
}else{
echo "Nothing written ..\n";
}
}else{
echo "Dbscript creation unsuccessful\n";
}
########################################################################################
# Utility functions #
########################################################################################
/**
* This function will read the input from the STDIN
* and return it to the script.
*
* @param mixed prefix This is the text which will be echoed before asking input
* @param bool required If true it will ask for input again and again if none given.
* @return string User input
*/
function read_input($prefixText , $required=false){
echo "$prefixText";
$input = trim(fgets(STDIN));
if($required && empty($input)){
echo "Entry required!!\n";
$input = read_input($prefixText , $required);
}
return str_replace( "\n", "", $input);
}
/**
* This function will get the name of the branch on which
* the script is being run. Currently it detects only git
* repositories
*
* @return string repository name
*/
function get_branch_name(){
return str_replace( "\n", "", str_replace("* " , "" , shell_exec("git branch 2>/dev/null | grep ^*")));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment