Friday, June 20, 2008

Synchronizing two CVS repositories using Mercurial

We're in a situation where we have access to some of our partner's source code. We need to keep in sync with their repo, otherwise our copy would quickly become stale.

Currently, we can do this manually because our changes are still small. A few months from now, however, our changes will eventually drift away from their code and merging it with our tree would become difficult.

A distributed version control system is in order. Here's how I did it:

+-----------------+ +------------+ +------------+ +----------------+
| Upstream | | Immutable | | Working | | Local |
| CVS repository |-------->| Mercurial |---------------->| Mercurial |-------->| CVS repository |
+-----------------+ import | repository | Mercurial pull | repository | export +----------------+
+------------+ +------------+

In theory, exporting it back to CVS is no longer needed. In practice, however, I needed to maintain "compatibility" with my fellow developers who only use CVS. I hope we'll eventually migrate away from CVS though because merging lots of files is really time consuming. Merging/branching is CVS' main weakness.

Thursday, June 19, 2008

MySQL: How to merge two databases

I'm currently managing a couple of databases in a production server. The problem: I need to know what new tables were created from Database 1 and merge them to Database 2.

Here's the solution:

0. Backup the target database.
% mysqldump -h [HOST] -P [PORT] -u [USERNAME] -p[PASSWORD] Database2 > Database2.sql

1. Extract the table names from the source and target databases.
% echo show tables | mysql -h [HOST] -P [PORT] -u [USERNAME] -p[PASSWORD] Database1 | sed '1d' > one.tables
% echo show tables | mysql -h [HOST] -P [PORT] -u [USERNAME] -p[PASSWORD] Database2 | sed '1d' > two.tables

2. Perform the merge.
% mysqldump -h [HOST] -P [PORT] -u [USERNAME] -p[PASSWORD] Database1 \
`diff --unchanged-line-format='' --new-line-format='%l ' two.tables one.tables` | \

mysql [HOST] -P [PORT] -u [USERNAME] -p[PASSWORD] Database2

Wednesday, June 04, 2008

Unix/Linux Tip: Syntax check your PHP files

If you wish to syntax check (lint) all the PHP files in your current directory, simply do:
    for i in `ls *.php`; do
        php -l $i