laravel’s database migration and seed

I have not used laravel 4’s migration and seed features much until couple days ago.
Oh boy, it’s a really sexy tool.

It does not matter whether you are in a team or you are all alone doing everything. It’s a feature that every single develop must use if integrating with a database!

Theoretically if you are a disciplined developer and use those features, confidence level of your database changes when deploying will be very high. I promise!

You will end up needing these if you decide to go with it:

*migration* file should be used for any changes that affect db schemas.
*seeding* file should be used to pre-populate data into db.

I thought it’d be beneficial to add some sample codes.

To create migration file, run this at Laravel’s app directory:

The above command line creates a file something like:

And its content looks something like this:

As you can take a guess, up method is where you change db schema. down method is where you rollback the change.

So let's say I want to add column "username" to "users" table.

(If you want to know more about available methods to manipulate db schema, click here.)

Now you want to run against your db? Then don't look further. Just run this:

After you run the command line, Laravel adds that particular code to migrations db table and that's how it keeps track of history.

Now you realize that you made a mistake and want to rollback your last change, run this:

I'll add seeding sample to the blog post later...

Why I am going full JavaScript

Recently I claimed that I would go full FE. Since I’ve been working on couple projects utilizing Backbone/Marionette, I realized that it’s about time for me to go full JavaScript even for BE using nodejs.

To be honest, I did not use nodejs much except running nodejs via forever and some hello world type apps. However, I’m planning to use javascript for everything as much as possible. Reasons are:

  • Community seems to be there to support you
  • One scripting language for FE/BE
  • Data transport over HTTP is done using JSON mostly
  • Many 3rd party Rest APIs support JSON
  • Performance got much much better

So that means, I will dig up build process, deployment process, and CI for Javascript sooner or later, which I’m happy to investigate.

ECMAScript 6 modules

AMD is new standard in my book and I am glad that ECMAScript 6 introduces modules.

This blog has great depth of the new ECMAScript 6 module.

From the spec, importing a module is very flexible in ECMAScript 6, which I like a lot.
Examples are (also found in the blog above):

  • import { square } from ‘lib/calc’;
  • import { square, MY_CONSTANT } from ‘lib/calc’;
  • import ‘lib/calc’ as c;
  • import { square as squ } from ‘lib/calc’;

As you can see there are 4 ways to import.

Aslo it has requirejs like syntax such as:

which is very similar to AMD syntax

Look at your html page as xml data for the sake of SEO

I’ve been working on a script that goes to a URL and scraps some parts of data, which is pretty much a crawler or spider.

If all pages that the crawler landed were valid, my job would have been so easy. However, in reality many many pages are not valid and the script has to use regular expression.

This can be a good or bad thing for those web owners.

However, exposure is very necessary in terms of marketing for the site and valid html page means it has greater chance to get exposed by search engines such as because valid html page will provide what search engine crawler wants more efficiently.

I believe engineers who work on those crawler have overcome many difficulties due to the invalid markup on a page. However, if HTML in a page is not valid (treating it as a xml), those smart engineers would have to come up with a logic to overcome that by using regular expression perhaps. That could be prone to mistakes so lead to scrapping only few from invalid HTML in a page. After all engineers are human and human make mistakes.

Also just for the same reason, if well formed semantic HTML is used, it will have higher chance to get exposed to a certain keyword typed by users.

That’s just my idea of how html page has to be constructed considering SEO and future use.

So my recommendation is this:

1. Treat markup in a page as data. Forget about presentation and such. Just make sure the data is valid.
2. Use CSS to visualize the data (= HTML markup) to appeal users

It’s quite simple after all.

PHP PSR-[0-3]

At my work place, we use PHP and CodeSniffer hooked up in Jenkins.
I liked the fact that PSR-1 and PSR-2 are approved by the committee.
The PSR0 standard is found at:

The PSR1 standard is found at:

The PSR2 standard is found at:

The PSR3 standard is found at:

p.s. Kohana 3.3 has PSR-0 and the migration from 3.2 to 3.3 is quite tough for me. It requires a lot of refactoring on my side…

phantomjs on centos 6.2

Right now I have jenkins to do automation build and static code analysis for php projects. However, I need to look into client side javascript automation unit test suites and selenium server to do client-side functional test.

This post will be a long history of what I am going through (will include steps with failures…)

1. I downloaded phantomjs-1.7.0-linux-x86_64.tar.bz2 binary file from this page.

2. When I executed it, I got this error:
phantomjs: error while loading shared libraries: cannot open shared object file: No such file or directory

3. So I installed
sudo yum install

4. I still get the same phantomjs error, which says is not found.

5. Looks like the was installed on /usr/lib, which is for 32 bit software and phantomjs needs one for 64 bit system.

6. So I created symlinks and placed them in /usr/lib64/

7. I get different error message (progress made!):
phantomjs: error while loading shared libraries: wrong ELF class: ELFCLASS32

8. It turned out that I just needed to install freetype:
yum install freetype

9. Now another dependency issue: cannot open shared object file: No such file or directory

10. “yum install fontconfig”. that installed correct lib files.

11. Got it installed. 🙂

[xxxxx@localhost ~]$ phantomjs –version

UPDATE Dec 6, 2012
Found this blog post for Phantomjs and qunit.

Another very resourceful answers at stackoverflow regarding phantomjs, ant, and jenkins.


Jenkins for php project on centos 6.2

After setting up Jenkins for use in my work place, I had to document how to set up in the past couple days. I thought I would share it here.

OS: CentOS release 6.2 (Final)
[—— ~]$ cat /proc/version
Linux version 2.6.32-220.23.1.el6.x86_64 ( (gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Mon Jun 18 18:58:52 BST 2012
[—— ~]$ uname -a
Linux jenkins.sometrics 2.6.32-220.23.1.el6.x86_64 #1 SMP Mon Jun 18 18:58:52 BST 2012 x86_64 x86_64 x86_64 GNU/Linux

Prerequisites prior to Jenkins

java-1.6.0-openjdk.x86_64 (64bits) - as of Jun 27, 2012
1. sudo yum install java-1.6.0-openjdk(this will pull these dependencies)
- giflib
- jline
- jpackage-utils
- rhino
- tzdata-java
2. actual version is 1:
Apache Ant 1.7.1 - as of Jun 27, 2012
1. sudo yum install ant (this will install these dependencies)
- java-1.5.0-gcj
- java-1.6.0-openjdk-devel
- java_cup
- libgcj
- sinjdoc
- xerces-j2
- xml-commons-api
- xml-commons-resolver
2. actual version is 1.7.1-13.el6

1. sudo rpm -Uvh
2. sudo yum install php54w php54w-cli php54w-common php54-dba php54-devel php54-gd php54-intl php54-mbstring php54-mysql php54-odbc php54-pdo php54-pear php54-process php54-xml (this will install these dependencies)
- autoconf
- automake
- libXpm
- libicu
- unixODBC

1. sudo pear upgrade PEAR
2. sudo pear channel-discover
3. sudo pear channel-discover
4. sudo pear channel-discover
5. sudo pear channel-discover
6. sudo pear channel-discover
7. sudo pear install --alldeps phpunit/PHPUnit
8. sudo pear install phpunit/DbUnit
9. sudo pear install phpunit/phpcpd
10. sudo yum install ImageMagick-devel (This will install these dependencies)
- bzip2-devel
- ghostscript-devel
- jasper-devel
- lcms-devel
- libICE-devel
- libSM-devel
- libX11-devel
- libXau-devel
- libXdmcp-devel
- libXext-devel
- libXt-devel
- libgomp
- libjpeg-devel
- libtiff-devel
- libwmf-lite
- libxcb-devel
- xorg-x11-proto-devel
- zlib-devel
11. sudo pear install --alldeps pdepend/PHP_Depend
12. sudo pear install theseer/phpDox-0.4.0 (note: this can take a while. Be patient)
13. sudo pear install PHP_CodeSniffer
14. sudo pear install --alldeps phpunit/PHP_CodeBrowser
15. pear channel-discover (it’s not working currently...)
16. sudo pear install phpmd/PHP_PMD (this will fail.... maybe server is down as of now and will be back up tomorrow perhaps? For now skip it.)

To install Jenkins through yum
1. sudo wget -O /etc/yum.repos.d/jenkins.repo
2. sudo rpm --import
3. sudo yum install jenkins
4. sudo service jenkins start/stop/restart

Jenkins should be accessible through port 8080
Make sure iptable opens the port 8080 from outside

Install Jenkins Plugins
1. go to http://{}:8080
2. Click on “Manage Jenkins” link from side menu
3. Click on “Manage Plugins” link from the page
4. Click on “Available” Tab
5. Check these plugins (Some might be missing because they could be already installed)
- External Monitor Job Type Plugin
- ant
- Static Code Analysis Plug-ins
- Subversion Plugin
- HTML Publisher plugin
- Jenkins Translation Assistance plugin
- Jenkins SSH Slaves plugin
- PMD Plugin
- Jenkins Clover PHP plugin
- Publish Over SSH
- Green Balls
- Jenkins Violations plugin
- Plot plugin
- Measurement Plots
- xUnit plugin
- Hudson SCP publisher plugin
- Checkstyle Plugin
- DRY Plugin
- Jenkins JDepend Plugin
6. Click on “Download now and install after restart”

There gotta be something I missed, but this instruction should be a good start to set up jenkins on centos 6.2 for any PHP development work.

Happy CI'ing! :)

mysqldump tip

mysqldump is a command line tool for exporting mysql data.
This tool also can be used to generate seed xml files for php dbunit.
And this is how you can achieve that.

mysqldump -u {user} -t –xml –databases {dbname} –tables {tablename} > filename.xml

The above command will generate a xml file that PHP DBUnit understands.

And then inside phpunit class file, you would be using it like this:

protected function getDataSet()

$seed = $this->createMySQLXMLDataSet(dirname(__FILE__).’/filename.xml’);
$datesets = array($seed);

$compositeDs = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet($seed);

return $compositeDs;


note to myself on mongoDB procedure

For PHP MongoDB Driver
sudo apt-get updatesudo apt-get install php5-dev php5-cli php-pearsudo pecl install mongo// if apache2cd /etc/php5/apache2sudo vim php.iniadd "" without double quotes/wq! and entersudo /etc/init.d/apache2 restart

For MongoDB Server
sudo apt-get adv --keyserver --recv 7F0CEB10sudo vim /etc/apt/sources.listadd "deb dist 10gen" without double quotes/wq! and entersudo apt-get updatesudo apt-get install mongodb-10gen

To launch mongodb commandline