30 Oct

How to keep your database under version control?

Best Practices, CakePHP

I've been looking for a solution to this problem from quite a few days now, and I did find it quite interesting that there's no standard way to do this. So I decided to ask the CakePHP community, while there are many tools available to do the things, but I'm going to share the best solution of all, which is also Cake based. CakePHP has something known as Schema shell, which helps you solving the problem in efficient way.

Let me describe the problem first briefly. There was this comic I saw few days back (can't find it right now), which inspired me to solve this problem. Here goes the text from that cartoon:

 

Alex: Ok, we're ready, let's sync our work on this project today to show to client.

Rob: Ok sure, let me see the database changes and put it to server.

Alex: Oh yea, let me do it too.

Rob: Hey, I made this change, is it yours? And what about this? Where are you using it?

Alex: Yes-No-Yes-….

Boss: And we're screwed.

 

That was happening all the time with our team too, before I found this way.

Here's how it works:

1. Setup

Assuming that you already have a working project to implement db versioning on, start command line console from your 'app' directory. And then run 'cake schema help' to make sure you have the shell, or for necessary instructions.

 

2. Generate first Schema

After you see it working fine, lets output our whole DB structure into a dump file, it's not SQL dump file, its schema file which is in Cake-friendly format. You can play with other commands like 'schema view' to make sure things works. Ok, let's generate our schema with 'cake schema generate'.

Ok, schema.php generated, it would be inside your app/config/sql

Here's how the file will look:

PHP:
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /* App schema generated on: 2008-10-30 23:10:37 : 1225410517*/
  4. class AppSchema extends CakeSchema {
  5.     var $name = 'App';
  6.  
  7.     function before($event = array()) {
  8.         return true;
  9.     }
  10.  
  11.     function after($event = array()) {
  12.     }
  13.  
  14.     var $users = array(
  15.             'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  16.             'name' => array('type'=>'string', 'null' => false, 'length' => 25),
  17.             'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
  18.         );
  19. }
  20. ?>

Looks good (with the sample db). Now as you have a file for the whole database, you can put it in a repository under version control along with your project.

 

3. Update Schema

Since, there will be changes in database throughout the development of the project, and they will be done by many other developers. We'll need to keep a track of it, to do so, we'll make sure schema files remains updated always – this is something you'll have to instruct your team about.

Let's make a sample change and see how it goes. In my users table, I am going to add a new field called 'password' and then regenerate my schema file.

ALTER TABLE `users` ADD `password` VARCHAR( 20 ) NOT NULL ;

Lets regenerate schema now:

CakePHP's Schema shell had detected the change automatically, and prompted me if I want to over-write the current schema or snapshot it (create a new one). In my case, I want to overwrite it as I already have it in version control. Let's see what new schema looks like:

PHP:
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /* App schema generated on: 2008-10-30 23:10:33 : 1225411053*/
  4. class AppSchema extends CakeSchema {
  5.     var $name = 'App';
  6.  
  7.     function before($event = array()) {
  8.         return true;
  9.     }
  10.  
  11.     function after($event = array()) {
  12.     }
  13.  
  14.     var $users = array(
  15.             'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  16.             'name' => array('type'=>'string', 'null' => false, 'length' => 25),
  17.             'password' => array('type'=>'string', 'null' => false, 'length' => 20),
  18.             'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
  19.         );
  20. }
  21. ?>

You see the 'password' field was reflected here as well. Now you can keep your whole DB structure under this schema file with version controlling. However, we've yet to sync the database across different machines. Let's say after updating (svn up) on my local setup, I got a new schema which I would like to implement on my database. Here's how you'll do that in next step.

 

4. Syncing

Let's assume the other user has added a field 'address' in his database, and regenerated the schema. Now I want same change to reflect on my local database. Here's new updated schema looks like (it's only a part of the schema.php file):

PHP:
  1. var $users = array(
  2.             'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  3.             'name' => array('type'=>'string', 'null' => false, 'length' => 25),
  4.             'password' => array('type'=>'string', 'null' => false, 'length' => 20),
  5.             'address' => array('type'=>'string', 'null' => false, 'length' => 40),
  6.             'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
  7.         );

Now there are normally 2 options to update your database:

  • you drop all tables and then import fresh DB structure from schema.
  • Just update (We need this one)

Here's what you'll do:

And done, our database is now synced. You can run the same process across different machines to never worry about manual syncing. [Please note by 'sync' I mean only the structure not the records.]

Hope you enjoyed it!

 

Abhimanyu Grover

 

26 Jun

The art of keeping in touch with old clients

Best Practices, Business, Ideas

Mouthshut.com is India's first website where customers can freely post reviews for companies. Founded in 2000, till then it has expanded well. I've used this site years back to expose bad service of a web hosting company.

Few days back, I had received an email from CEO of Mouthshut.com, Faisal Farooqui (Here's the full newsletter). That email stated a latest testimonial of a very satisfied client and left a great impression on me. It's a nice technique for any business to follow.

Here's the most impressive part:

First, a story from a MouthShut.com member that shows we are making a positive difference: Pradeep Chopra is an IIT Delhi Graduate who co-founded OmLogic. I am also privileged to count him as a friend. Pradeep shared the following in an email (certain portions underlined for emphasis):

"Recently, I had a terrible experience with Apple MacBook and Apple India. I posted a review at your community and only then Apple listened and actually gave me a new laptop. Thanks to your community and its power...Fortunately, the review is being read by around 6000 people and people have been writing to me whether they should be Apple MacBook."

This was the testimonial included in email. First of all, this testimonial was enough for me to visit their site once again and start using it. It clearly states how successful they are in solving the problems they are meant to – which really impress me, personally.

Secondly, when you have already impressed your customers, you can also announce your new service or special offers. Just like they did!! It has given me an important tip to run my business.

Always keep in touch with all old clients.

- Abhimanyu Grover

07 Jun

Best ways to eliminate worries from your start-up business

Best Practices, Business

Normally, I blog about technical stuff, but as I am currently investing lot of time in making Giga Promoters strong, not only technically but capital wise as well. I also had a single request from a friend to blog about the tips. Here are a few things which I think are crucial for business success:

  1. 80/20 Analysis – Do regular analysis, about which clients are most performing, which core processes help you the most. Pareto's 80-20 principle defines most of things. If you get it correctly, then you can achieve more with much less input.
  2. Learn Cash Management – When you're running a small business, you're working so hard that learning to manage cash would be your last priority. Business owners worry about paying bills on time, sometimes they pay it twice, sometimes they don't even have cash to pay bills. Learning cash management and a little bit of finance can help you in performing well, and eliminating such worries from business life.
  3. Learn power of Assets – When you are in business, you can't be in your own business's production cycle. You got to own the thing – not run it yourself. You don't have to work for earning money but building assets which in long term is going to pay you.
  4. Think Win/Win – In my experience, I've seen that most of the people see all situations as Lose/Win or Win/Lose which means when dealing with others, either they are going to lose or the person they are dealing with. But you can do much better by learning Win/Win. Think about the ways which will make you and your clients win. If you can not come up with a way, simply don't deal.
  5. Master time management – On daily basis, there are some urgent tasks then there are some not-so-urgent tasks. Of course you got to focus on urgent tasks for obvious reasons, and they will keep you busy in a circle each day. Try breaking this cycle, and invest time in the things which are not so urgent but are important as any other thing. Eg- learning from books, reading articles, exploring new technologies, automating tasks.
  6. Innovate – The only way for you to succeed is through innovating, launching a product or service which is unique to others is only way to success. Its because anything which you think is there in market, with tons of companies competing over it. Your mission statement should be 'change-the-world', not 're-launch-old-boring-product'. Have you idea book, and keep recording your ideas, and keep sharpen the saw until you get ready for a new big thing.
  7. Giving it back – Simple. Teach others. Helping others makes a great day. Infact this article is for the same reason.

I learned many things from the following books, and highly recommend to any business owner:

Thanks for reading.

- Abhimanyu Grover

Hire us

Contact us to get a free quote on your project.