Automated Migration

Hi there Guys,

As the application I’m developing is expanding across multiple sites, each with its own database, I’m having a few issues with database migration. What I’d like to do is to automate the process as much as possible. As I’m sure we all know the main issue with updating manually is that sometimes things get missed.

As such, I was thinking about making the system check for new migrations on page load, and executing these if found. There would be safeguards against the updates being performed multiple times, with the system displaying a simple maintenance message if an update is in progress, and the database upgrades would be tracked using a system similar to that mentioned in this post.

I’ve got unit testing running with Jenkins now, my plan is to sync my test database to a copy of the live database at the start of the job, which over the course of the tests will obviously entail running any migrations.

My question is basically, is this a good idea? Can anyone think of a better way?

Thanks in advance.

When you use migrations, you already use versioning, so no need to use SVN.

Once applied, the migration command won’t apply the same migration again.

You could use a cron job if you want to be sure that it executes on a regular basis.

Hi jacmoe,

Thanks, I just took another look at my setup and face-palmed as I realised that I already have a script that I run when I publish a new version of the code. I should be able to put the yiic command in there. Yes?


Yes. :)

Full path to php-cli and full path to yiic in the app/protected directory.

Then "migrate --interactive=0"

Don’t neglect to clear any schema caching at the same time, if you’re doign it.


I used to define ant task named "migrate" in "build.xml",

then run it before phpunit task.

<target name="migrate" description="Run migrate script">

	<exec executable="${basedir}/protected/yiic" failonerror="true">

		<arg line="migrate"/>

		<arg line="--interactive=0"/>