29 Apr

Client’s Policies or Your productivity? / Version control over FTP

Best Practices

Not too long back my colleague asked community about how they handle their deployments and patches to the production server. We received quality response and various options – I learned many new things about how others were handling it. We are using Subversion for versioning our projects from last 3 years and were very happy to implement it. It reduced a lot of time in release cycle and all that hassle one has to go through, was simply gone.

But here comes the problem. The clients who are not willing to use such tools. Or are simply not giving you authorization to do so, or they are using shared hosting environment. I’ve seen this problem with my 5 out of 10 clients. They have a reason to stop you – they were never asked for this before from their previous development companies or whatsoever. Then, there are clients who say “Use ftp” when you ask them to send SSH access, this limits the productivity of the whole system and the project. However, I consider it as my call to make them aware of the things which can help them – and that is why I’m writing this post today.

So by this post, I actually want to highlight the advantages offered by any version control system over the regular FTP transfer for deployment – I’m sure this will help the buyers who don’t allow much control in hands of their web developers. Ok, here we go, these are some real BIG limitations you know you’ll face using FTP:

  1. Initial setup is always easy, updating and bug fixes and applying patches is difficult using FTP. You spend 10x more time being careful for things that can be handled automatically.
  2. You have to wait for the files to upload/download.
  3. If something goes wrong, it’s really difficult to revert to “last stable” state.

And when you’re using Subversion or any other version control tool for that matter – All these issues are no more your concern, and you can focus on real development rather than finding files and stuff. Here are some advantages offered by a standard version control system:

  1. To apply patches, you simply issue “svn up” command to the system (In case of subversion). It automatically updates the modified files.
  2. In case there’s something wrong, you can go back to any old revision and have your site running as it was. Using Subversion’s advanced features and proper versioning; you can also define a “last stable” version and be safe all the times.
  3. You don’t have to worry about latest back – everything is on a separate server with all revisions.

This is just a start – many things are eventually taken care of while using a version control. However, there are many other automated ways for this purpose, some people like using “rsync”, some rely on application servers and so.

I’m hoping that it’ll help buyers understand our offering better. Thanks for reading.

Abhimanyu Grover

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

 

Hire us

Contact us to get a free quote on your project.