Jan 30, 2013

Mercurial, Fabric, Liquibase, Kohana (part 4)

Today I am focusing on making sure all database related stuff from previous parts can be executed by me and any of my teammates on their local machines using Fabric.

LiquiBase is a Java program, which does not require to be installed on any specific location on a disk. For this kind of tool there is a good practice to keep it together with a project. There are different approaches to that. I am used to having separate repository for tools such as a LiquiBase. I call it  "devtools" and use Mercurial subrepo feature to make sure it is tied with a project.

To be able to talk to a database of our choice LiquiBase requires a database connector (driver). This is another piece of program for our "devtools" repository. Of course there are many other useful tools (in Java, Python, Perl, ...) we can consider using later down the road and store in in our "devtools" repo. For now however, this is layout of "devtools" repo:

devtools
|- liquibase
|  |- liquibase-2.0.5-bin
|
|- mysql-conector-java
|  |- mysql-conector-java-5.1.22

The "devtools" repo can now be cloned into our central Mercurial repository. Then we need to create ".hgsub" file in our project and populate it with a path to "devtools" repo:

devtools=http://server/devtools

Your path will be different of course. Now I need to add ".hgsub" into our repo and make sure I pull contents of "devtools" repo into the project. To do that I issue following sequence of commands in a terminal:

hg add .hgsub
hg ci -m "added subrepo"
hg push
hg pull -u
cd devtools
hg pull -u

Now the project looks like this:

project
|- application
|- db
|- devtools
|- kohana
|- .htaccess
|- .hgignore
|- .hgsub
|- index.php
|- fabfile.py

Now it's time to focus on the "fabfile.py". I want to be able to create database and it's structures by calling "init" target. I changed the "fabfile.py" to have required functionality in a following manner:
from fabric.api import local
 
def init():
    """Prepares local environment for a project."""
    local('mkdir -p application/logs')
    local('mkdir -p application/cache')
    local('chmod 777 application/logs')
    local('chmod 777 application/cache')
    local('mysql -u root -e "CREATE DATABASE IF NOT EXISTS project_db"')
    local('cd db && java -jar ../devtools/liquibase/liquibase-2.0.5-bin/liquibase.jar \
--classpath=../devtools/mysql-conector-java/mysql-connector-java-5.1.22/mysql-connector-java-5.1.22-bin.jar \
--driver=com.mysql.jdbc.Driver \
--changeLogFile=master.xml \
--url="jdbc:mysql://localhost/project_db" \
--username=root \
update')

In following part I'll focus on contents and layouts of "project/db" directory.

No comments: